本文整理汇总了Golang中github.com/square/metrics/api.NewTimerange函数的典型用法代码示例。如果您正苦于以下问题:Golang NewTimerange函数的具体用法?Golang NewTimerange怎么用?Golang NewTimerange使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewTimerange函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestApplyNotes
func TestApplyNotes(t *testing.T) {
var testTimerange, err = api.NewTimerange(758400000, 758400000+30000*5, 30000)
if err != nil {
t.Fatalf("invalid timerange used for testcase")
return
}
// epsilon := 1e-10
list := api.SeriesList{
Series: []api.Timeseries{
{
Values: []float64{1, 2, 3, 2, 1, 2},
TagSet: api.TagSet{
"series": "C",
},
},
},
Timerange: testTimerange,
Name: "test",
}
testCases := []struct {
transform transform
parameter []function.Value
expected []string
}{
{
transform: rate,
parameter: []function.Value{},
expected: []string{
"Rate(map[series:C]): The underlying counter reset between 2.000000, 1.000000\n",
},
},
}
for _, test := range testCases {
ctx := function.EvaluationContext{EvaluationNotes: []string{}}
_, err := ApplyTransform(&ctx, list, test.transform, test.parameter)
if err != nil {
t.Error(err)
continue
}
if len(test.expected) != len(ctx.EvaluationNotes) {
t.Errorf("Expected there to be %d notes but there were %d of them", len(test.expected), len(ctx.EvaluationNotes))
}
for i, note := range test.expected {
if i >= len(ctx.EvaluationNotes) {
break
}
if ctx.EvaluationNotes[i] != note {
t.Errorf("The context notes didn't include the evaluation message. Expected: %s Actually found: %s\n", note, ctx.EvaluationNotes[i])
}
}
}
}
示例2: TestSeriesFromMetricPoints
func TestSeriesFromMetricPoints(t *testing.T) {
timerange, err := api.NewTimerange(4000, 4800, 100)
if err != nil {
t.Fatalf("testcase timerange is invalid")
return
}
points := []metricPoint{
{
Timestamp: 4100,
Average: 1,
},
{
Timestamp: 4299, // Test flooring behavior
Average: 2,
},
{
Timestamp: 4403, // Test flooring behavior
Average: 3,
},
{
Timestamp: 4500,
Average: 4,
},
{
Timestamp: 4700,
Average: 5,
},
{
Timestamp: 4749,
Average: 6,
},
}
expected := [][]float64{{}, {1}, {2}, {}, {3}, {4}, {}, {5, 6}, {}}
result := bucketsFromMetricPoints(points, func(point metricPoint) float64 { return point.Average }, timerange)
if len(result) != len(expected) {
t.Fatalf("Expected %+v but got %+v", expected, result)
return
}
for i, expect := range expected {
if len(result[i]) != len(expect) {
t.Fatalf("Exected %+v but got %+v", expected, result)
return
}
for j := range expect {
if result[i][j] != expect[j] {
t.Fatalf("Expected %+v but got %+v", expected, result)
return
}
}
}
}
示例3: TestMovingAverage
func TestMovingAverage(t *testing.T) {
fakeAPI := mocks.NewFakeMetricMetadataAPI()
fakeAPI.AddPairWithoutGraphite(api.TaggedMetric{"series", api.NewTagSet()})
fakeBackend := movingAverageBackend{}
timerange, err := api.NewTimerange(1200, 1500, 100)
if err != nil {
t.Fatalf(err.Error())
}
expression := &functionExpression{
functionName: "transform.moving_average",
groupBy: []string{},
arguments: []function.Expression{
&metricFetchExpression{"series", api.TruePredicate},
durationExpression{"300ms", 300 * time.Millisecond},
},
}
backend := fakeBackend
result, err := evaluateToSeriesList(expression,
&function.EvaluationContext{
MetricMetadataAPI: fakeAPI,
TimeseriesStorageAPI: backend,
Timerange: timerange,
SampleMethod: api.SampleMean,
FetchLimit: function.NewFetchCounter(1000),
Registry: registry.Default(),
Cancellable: api.NewCancellable(),
OptimizationConfiguration: optimize.NewOptimizationConfiguration(),
})
if err != nil {
t.Errorf(err.Error())
}
expected := []float64{4, 3, 11.0 / 3, 5}
if len(result.Series) != 1 {
t.Fatalf("expected exactly 1 returned series")
}
if len(result.Series[0].Values) != len(expected) {
t.Fatalf("expected exactly %d values in returned series, but got %d", len(expected), len(result.Series[0].Values))
}
const eps = 1e-7
for i := range expected {
if math.Abs(result.Series[0].Values[i]-expected[i]) > eps {
t.Fatalf("expected %+v but got %+v", expected, result.Series[0].Values)
}
}
}
示例4: TestMovingAverage
func TestMovingAverage(t *testing.T) {
fakeAPI := mocks.NewFakeApi()
fakeAPI.AddPair(api.TaggedMetric{"series", api.NewTagSet()}, "series")
fakeBackend := movingAverageBackend{}
timerange, err := api.NewTimerange(1200, 1500, 100)
if err != nil {
t.Fatalf(err.Error())
}
expression := &functionExpression{
functionName: "transform.moving_average",
groupBy: []string{},
arguments: []function.Expression{
&metricFetchExpression{"series", api.TruePredicate},
stringExpression{"300ms"},
},
}
result, err := evaluateToSeriesList(expression,
function.EvaluationContext{
API: fakeAPI,
MultiBackend: backend.NewSequentialMultiBackend(fakeBackend),
Timerange: timerange,
SampleMethod: api.SampleMean,
FetchLimit: function.NewFetchCounter(1000),
Registry: registry.Default(),
})
if err != nil {
t.Errorf(err.Error())
}
expected := []float64{4, 3, 11.0 / 3, 5}
if len(result.Series) != 1 {
t.Fatalf("expected exactly 1 returned series")
}
if len(result.Series[0].Values) != len(expected) {
t.Fatalf("expected exactly %d values in returned series", len(expected))
}
const eps = 1e-7
for i := range expected {
if math.Abs(result.Series[0].Values[i]-expected[i]) > eps {
t.Fatalf("expected %+v but got %+v", expected, result.Series[0].Values)
}
}
}
示例5: Test_ScalarExpression
func Test_ScalarExpression(t *testing.T) {
timerangeA, err := api.NewTimerange(0, 10, 2)
if err != nil {
t.Fatalf("invalid timerange used for testcase")
return
}
for _, test := range []struct {
expr scalarExpression
timerange api.Timerange
expectedSeries []api.Timeseries
}{
{
scalarExpression{5},
timerangeA,
[]api.Timeseries{
api.Timeseries{
Values: []float64{5.0, 5.0, 5.0, 5.0, 5.0, 5.0},
TagSet: api.NewTagSet(),
},
},
},
} {
a := assert.New(t).Contextf("%+v", test)
result, err := evaluateToSeriesList(test.expr, &function.EvaluationContext{
TimeseriesStorageAPI: FakeBackend{},
Timerange: test.timerange,
SampleMethod: api.SampleMean,
FetchLimit: function.NewFetchCounter(1000),
Registry: registry.Default(),
})
if err != nil {
t.Fatalf("failed to convert number into serieslist")
}
a.EqInt(len(result.Series), len(test.expectedSeries))
for i := 0; i < len(result.Series); i++ {
a.Eq(result.Series[i].Values, test.expectedSeries[i].Values)
}
}
}
示例6: TestTransformIdentity
// Test that the transforms of the following work as expected:
// - transform.derivative | transform.integral
func TestTransformIdentity(t *testing.T) {
//This is to make sure that the scale of all the data
//is interpreted as 30 seconds (30000 milliseconds)
timerange, _ := api.NewTimerange(0, int64(30000*5), int64(30000))
testCases := []struct {
values []float64
timerange api.Timerange
tests []struct {
expected []float64
transforms []transform
}
}{
{
values: []float64{0, 1, 2, 3, 4, 5},
timerange: timerange,
tests: []struct {
expected []float64
transforms []transform
}{
{
expected: []float64{0, 1, 2, 3, 4},
transforms: []transform{
derivative,
Integral,
},
},
{
expected: []float64{0, 1, 2, 3, 4},
transforms: []transform{
rate,
Integral,
},
},
},
},
{
values: []float64{12, 15, 20, 3, 18, 30},
timerange: timerange,
tests: []struct {
expected []float64
transforms []transform
}{
{
expected: []float64{0, 5, -12, 3, 15},
transforms: []transform{
derivative,
Integral,
},
},
{
// While this is odd, think about it this way:
// We saw 5 increments (15 - 20), then we saw thirty total increments
// (3, 18, 30) over the rest of the time period
expected: []float64{0, 5, 8, 23, 35},
transforms: []transform{
rate,
Integral,
},
},
},
},
}
epsilon := 1e-10
var err error
for _, test := range testCases {
series := api.Timeseries{
Values: test.values,
TagSet: api.TagSet{},
}
for _, transform := range test.tests {
result := series
for _, fun := range transform.transforms {
ctx := function.EvaluationContext{EvaluationNotes: []string{}}
seriesList := api.SeriesList{
Series: []api.Timeseries{result},
Timerange: timerange,
}
params := []function.Value{}
a, err := ApplyTransform(&ctx, seriesList, fun, params)
result = a.Series[0]
if err != nil {
t.Error(err)
break
}
}
if err != nil {
continue
}
if len(result.Values) != len(transform.expected) {
t.Errorf("Expected result to have length %d but has length %d", len(transform.expected), len(result.Values))
continue
}
// Now check that the values are approximately equal
for i := range result.Values {
if math.Abs(result.Values[i]-transform.expected[i]) > epsilon {
//.........这里部分代码省略.........
示例7: TestCommand_Select
func TestCommand_Select(t *testing.T) {
epsilon := 1e-10
fakeApi := mocks.NewFakeApi()
fakeApi.AddPair(api.TaggedMetric{"series_1", api.ParseTagSet("dc=west")}, emptyGraphiteName)
fakeApi.AddPair(api.TaggedMetric{"series_2", api.ParseTagSet("dc=east")}, emptyGraphiteName)
fakeApi.AddPair(api.TaggedMetric{"series_2", api.ParseTagSet("dc=west")}, emptyGraphiteName)
fakeApi.AddPair(api.TaggedMetric{"series_3", api.ParseTagSet("dc=west")}, emptyGraphiteName)
fakeApi.AddPair(api.TaggedMetric{"series_3", api.ParseTagSet("dc=east")}, emptyGraphiteName)
fakeApi.AddPair(api.TaggedMetric{"series_3", api.ParseTagSet("dc=north")}, emptyGraphiteName)
fakeApi.AddPair(api.TaggedMetric{"series_timeout", api.ParseTagSet("dc=west")}, emptyGraphiteName)
var fakeBackend fakeApiBackend
testTimerange, err := api.NewTimerange(0, 120, 30)
if err != nil {
t.Errorf("Invalid test timerange")
return
}
earlyTimerange, err := api.NewTimerange(0, 60, 30)
if err != nil {
t.Errorf("Invalid test timerange")
}
lateTimerange, err := api.NewTimerange(60, 120, 30)
if err != nil {
t.Errorf("Invalid test timerange")
}
for _, test := range []struct {
query string
expectError bool
expected api.SeriesList
}{
{"select does_not_exist from 0 to 120 resolution 30ms", true, api.SeriesList{}},
{"select series_1 from 0 to 120 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{1, 2, 3, 4, 5},
api.ParseTagSet("dc=west"),
}},
Timerange: testTimerange,
Name: "series_1",
}},
{"select series_timeout from 0 to 120 resolution 30ms", true, api.SeriesList{}},
{"select series_1 + 1 from 0 to 120 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{2, 3, 4, 5, 6},
api.ParseTagSet("dc=west"),
}},
Timerange: testTimerange,
Name: "",
}},
{"select series_1 * 2 from 0 to 120 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{2, 4, 6, 8, 10},
api.ParseTagSet("dc=west"),
}},
Timerange: testTimerange,
Name: "",
}},
{"select aggregate.max(series_2) from 0 to 120 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{3, 2, 3, 6, 5},
api.NewTagSet(),
}},
Timerange: testTimerange,
Name: "series_2",
}},
{"select (1 + series_2) | aggregate.max from 0 to 120 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{4, 3, 4, 7, 6},
api.NewTagSet(),
}},
Timerange: testTimerange,
Name: "series_2",
}},
{"select series_1 from 0 to 60 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{1, 2, 3},
api.ParseTagSet("dc=west"),
}},
Timerange: earlyTimerange,
Name: "series_1",
}},
{"select transform.timeshift(series_1,31ms) from 0 to 60 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{2, 3, 4},
api.ParseTagSet("dc=west"),
}},
Timerange: earlyTimerange,
Name: "series_1",
}},
{"select transform.timeshift(series_1,62ms) from 0 to 60 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{3, 4, 5},
api.ParseTagSet("dc=west"),
}},
Timerange: earlyTimerange,
Name: "series_1",
}},
{"select transform.timeshift(series_1,29ms) from 0 to 60 resolution 30ms", false, api.SeriesList{
Series: []api.Timeseries{{
[]float64{2, 3, 4},
api.ParseTagSet("dc=west"),
}},
//.........这里部分代码省略.........
示例8: Test_AggregateBy
func Test_AggregateBy(t *testing.T) {
a := assert.New(t)
timerange, err := api.NewTimerange(42, 270, 6)
if err != nil {
t.Fatalf("Timerange for test is invalid")
return
}
var testList = api.SeriesList{
[]api.Timeseries{
api.Timeseries{
Values: []float64{0, 1, 2},
TagSet: api.TagSet{
"env": "staging",
"dc": "A",
"host": "q77",
},
},
api.Timeseries{
Values: []float64{4, 4, 4},
TagSet: api.TagSet{
"env": "staging",
"dc": "B",
"host": "r53",
},
},
api.Timeseries{
Values: []float64{-1, -1, 2},
TagSet: api.TagSet{
"env": "production",
"dc": "A",
"host": "y1",
},
},
api.Timeseries{
Values: []float64{0, 2, 0},
TagSet: api.TagSet{
"env": "production",
"dc": "A",
"host": "w20",
},
},
api.Timeseries{
Values: []float64{2, 0, 0},
TagSet: api.TagSet{
"env": "production",
"dc": "B",
"host": "t8",
},
},
api.Timeseries{
Values: []float64{0, 0, 1},
TagSet: api.TagSet{
"env": "production",
"dc": "C",
"host": "b38",
},
},
api.Timeseries{
Values: []float64{math.NaN(), math.NaN(), math.NaN()},
TagSet: api.TagSet{
"env": "staging",
"dc": "A",
"host": "n44",
},
},
api.Timeseries{
Values: []float64{math.NaN(), 10, math.NaN()},
TagSet: api.TagSet{
"env": "production",
"dc": "B",
"host": "n10",
},
},
},
timerange,
"Test.List",
"",
}
var aggregatedTests = []struct {
Tags []string
Aggregator func([]float64) float64
Combines bool
Results []api.Timeseries
}{
{
[]string{"env"},
Sum,
false,
[]api.Timeseries{
api.Timeseries{
Values: []float64{1, 11, 3},
TagSet: map[string]string{
"env": "production",
},
},
api.Timeseries{
Values: []float64{4, 5, 6},
//.........这里部分代码省略.........
示例9: TestFilter
func TestFilter(t *testing.T) {
a := assert.New(t)
timerange, err := api.NewTimerange(1300, 1700, 100)
if err != nil {
t.Fatalf("invalid timerange used in testcase")
}
series := map[string]api.Timeseries{
"A": {
Values: []float64{3, 3, 3, 3, 3},
TagSet: api.TagSet{
"name": "A",
},
},
"B": {
Values: []float64{1, 2, 2, 1, 0},
TagSet: api.TagSet{
"name": "B",
},
},
"C": {
Values: []float64{1, 2, 3, 4, 5.1},
TagSet: api.TagSet{
"name": "C",
},
},
"D": {
Values: []float64{4, 4, 3, 4, 3},
TagSet: api.TagSet{
"name": "D",
},
},
}
list := api.SeriesList{
Series: []api.Timeseries{series["A"], series["B"], series["C"], series["D"]},
Timerange: timerange,
Name: "test_series",
}
tests := []struct {
summary func([]float64) float64
lowest bool
count int
expect []string
}{
{
summary: aggregate.Sum,
lowest: true,
count: 6,
expect: []string{"A", "B", "C", "D"},
},
{
summary: aggregate.Sum,
lowest: false,
count: 6,
expect: []string{"A", "B", "C", "D"},
},
{
summary: aggregate.Sum,
lowest: true,
count: 4,
expect: []string{"A", "B", "C", "D"},
},
{
summary: aggregate.Sum,
lowest: true,
count: 3,
expect: []string{"A", "B", "C"},
},
{
summary: aggregate.Sum,
lowest: true,
count: 2,
expect: []string{"A", "B"},
},
{
summary: aggregate.Sum,
lowest: true,
count: 1,
expect: []string{"B"},
},
{
summary: aggregate.Sum,
lowest: false,
count: 4,
expect: []string{"A", "B", "C", "D"},
},
{
summary: aggregate.Sum,
lowest: false,
count: 3,
expect: []string{"A", "C", "D"},
},
{
summary: aggregate.Sum,
lowest: false,
count: 2,
expect: []string{"C", "D"},
//.........这里部分代码省略.........
示例10: Test_Blueflood
func Test_Blueflood(t *testing.T) {
timerange, err := api.NewTimerange(12000, 13000, 1000)
if err != nil {
t.Fatalf("invalid testcase timerange")
return
}
defaultClientConfig := Config{
"https://blueflood.url",
"square",
make(map[string]int64),
time.Millisecond,
0,
}
// Not really MIN1440, but that's what default TTLs will get with the Timerange we use
defaultQueryUrl := "https://blueflood.url/v2.0/square/views/some.key.graphite?from=12000&resolution=MIN1440&select=numPoints%2Caverage&to=14000"
for _, test := range []struct {
name string
metricMap map[api.GraphiteMetric]api.TaggedMetric
queryMetric api.TaggedMetric
sampleMethod api.SampleMethod
timerange api.Timerange
clientConfig Config
queryUrl string
queryResponse string
queryResponseCode int
queryDelay time.Duration
expectedErrorCode api.BackendErrorCode
expectedSeriesList api.Timeseries
}{
{
name: "Success case",
metricMap: map[api.GraphiteMetric]api.TaggedMetric{
api.GraphiteMetric("some.key.graphite"): api.TaggedMetric{
MetricKey: api.MetricKey("some.key"),
TagSet: api.ParseTagSet("tag=value"),
},
},
queryMetric: api.TaggedMetric{
MetricKey: api.MetricKey("some.key"),
TagSet: api.ParseTagSet("tag=value"),
},
sampleMethod: api.SampleMean,
timerange: timerange,
queryUrl: defaultQueryUrl,
clientConfig: defaultClientConfig,
queryResponse: `{
"unit": "unknown",
"values": [
{
"numPoints": 1,
"timestamp": 12000,
"average": 5
},
{
"numPoints": 1,
"timestamp": 13000,
"average": 3
}
],
"metadata": {
"limit": null,
"next_href": null,
"count": 2,
"marker": null
}
}`,
expectedSeriesList: api.Timeseries{
Values: []float64{5, 3},
TagSet: api.ParseTagSet("tag=value"),
},
},
{
name: "Failure case - invalid JSON",
metricMap: map[api.GraphiteMetric]api.TaggedMetric{
api.GraphiteMetric("some.key.graphite"): api.TaggedMetric{
MetricKey: api.MetricKey("some.key"),
TagSet: api.ParseTagSet("tag=value"),
},
},
queryMetric: api.TaggedMetric{
MetricKey: api.MetricKey("some.key"),
TagSet: api.ParseTagSet("tag=value"),
},
sampleMethod: api.SampleMean,
timerange: timerange,
clientConfig: defaultClientConfig,
queryUrl: defaultQueryUrl,
queryResponse: `{invalid}`,
expectedErrorCode: api.FetchIOError,
},
{
name: "Failure case - HTTP error",
metricMap: map[api.GraphiteMetric]api.TaggedMetric{
api.GraphiteMetric("some.key.graphite"): api.TaggedMetric{
MetricKey: api.MetricKey("some.key"),
TagSet: api.ParseTagSet("tag=value"),
},
},
queryMetric: api.TaggedMetric{
//.........这里部分代码省略.........
示例11: TestApplyTransformNaN
func TestApplyTransformNaN(t *testing.T) {
var testTimerange, err = api.NewTimerange(758400000, 758400000+30000*5, 30000)
if err != nil {
t.Fatalf("invalid timerange used for testcase")
return
}
nan := math.NaN()
list := api.SeriesList{
Series: []api.Timeseries{
{
Values: []float64{0, 1, nan, 3, 4, 5},
TagSet: api.TagSet{
"series": "A",
},
},
{
Values: []float64{2, nan, nan, nan, 3, 3},
TagSet: api.TagSet{
"series": "B",
},
},
{
Values: []float64{0, 1, 2, nan, 2, 1},
TagSet: api.TagSet{
"series": "C",
},
},
},
Timerange: testTimerange,
Name: "test",
}
tests := []struct {
transform transform
parameters []function.Value
expected map[string][]float64
}{
{
transform: Derivative,
parameters: []function.Value{},
expected: map[string][]float64{
"A": {0, 1.0 / 30, nan, nan, 1.0 / 30, 1.0 / 30},
"B": {0, nan, nan, nan, nan, 0.0},
"C": {0, 1.0 / 30, 1.0 / 30, nan, nan, -1.0 / 30},
},
},
{
transform: Integral,
parameters: []function.Value{},
expected: map[string][]float64{
"A": {0, 1 * 30, 1 * 30, 4 * 30, 8 * 30, 13 * 30},
"B": {2 * 30, 2 * 30, 2 * 30, 2 * 30, 5 * 30, 8 * 30},
"C": {0, 1 * 30, 3 * 30, 3 * 30, 5 * 30, 6 * 30},
},
},
{
transform: Rate,
parameters: []function.Value{},
expected: map[string][]float64{
"A": {0, 1 / 30.0, nan, nan, 1 / 30.0, 1 / 30.0},
"B": {0, nan, nan, nan, nan, 0},
"C": {0, 1 / 30.0, 1 / 30.0, nan, nan, 0},
},
},
{
transform: Cumulative,
parameters: []function.Value{},
expected: map[string][]float64{
"A": {0, 1, 1, 4, 8, 13},
"B": {2, 2, 2, 2, 5, 8},
"C": {0, 1, 3, 3, 5, 6},
},
},
{
transform: Default,
parameters: []function.Value{function.ScalarValue(17)},
expected: map[string][]float64{
"A": {0, 1, 17, 3, 4, 5},
"B": {2, 17, 17, 17, 3, 3},
"C": {0, 1, 2, 17, 2, 1},
},
},
{
transform: NaNKeepLast,
parameters: []function.Value{},
expected: map[string][]float64{
"A": {0, 1, 1, 3, 4, 5},
"B": {2, 2, 2, 2, 3, 3},
"C": {0, 1, 2, 2, 2, 1},
},
},
}
for _, test := range tests {
result, err := ApplyTransform(list, test.transform, test.parameters)
if err != nil {
t.Fatalf(fmt.Sprintf("error applying transformation %s", err))
return
}
for _, series := range result.Series {
values := series.Values
expected := test.expected[series.TagSet["series"]]
//.........这里部分代码省略.........
示例12: TestFilterRecent
func TestFilterRecent(t *testing.T) {
timerange, err := api.NewTimerange(1300, 2000, 100)
a := assert.New(t)
a.CheckError(err)
series := []api.Timeseries{
{
Values: []float64{0, 1, 1, 0, 8, 8, 9, 8},
TagSet: api.TagSet{"name": "A"},
},
{
Values: []float64{-5, -6, -3, -4, 5, 6, 7, 8},
TagSet: api.TagSet{"name": "B"},
},
{
Values: []float64{7, 7, 6, 7, 3, 2, 1, 1},
TagSet: api.TagSet{"name": "C"},
},
{
Values: []float64{6, 5, 5, 5, 2, 2, 3, 3},
TagSet: api.TagSet{"name": "D"},
},
}
list := api.SeriesList{
Series: series,
Timerange: timerange,
}
seriesMap := map[string]api.Timeseries{"A": series[0], "B": series[1], "C": series[2], "D": series[3]}
tests := []struct {
summary func([]float64) float64
lowest bool
count int
duration time.Duration
expect []string
}{
{
summary: aggregate.Max,
lowest: false,
count: 50,
duration: time.Millisecond * 450, // Four points
expect: []string{"A", "B", "C", "D"},
},
{
summary: aggregate.Min,
lowest: true,
count: 5,
duration: time.Millisecond * 450, // Four points
expect: []string{"A", "B", "C", "D"},
},
{
summary: aggregate.Mean,
lowest: false,
count: 4,
duration: time.Millisecond * 450, // Four points
expect: []string{"A", "B", "C", "D"},
},
{
summary: aggregate.Max,
lowest: false,
count: 2,
duration: time.Millisecond * 450, // Four points
expect: []string{"A", "B"},
},
{
summary: aggregate.Max,
lowest: true,
count: 2,
duration: time.Millisecond * 450, // Four points
expect: []string{"C", "D"},
},
{
summary: aggregate.Sum,
lowest: true,
count: 1,
duration: time.Millisecond * 9000, // All points
expect: []string{"B"},
},
{
summary: aggregate.Sum,
lowest: false,
count: 1,
duration: time.Millisecond * 9000, // All points
expect: []string{"A"},
},
}
for _, test := range tests {
filtered := FilterRecentBy(list, test.count, test.summary, test.lowest, test.duration)
// Verify that they're all unique and expected and unchanged
a.EqInt(len(filtered.Series), len(test.expect))
// Next, verify that the names are the same.
correct := map[string]bool{}
for _, name := range test.expect {
correct[name] = true
}
for _, series := range filtered.Series {
name := series.TagSet["name"]
if !correct[name] {
t.Errorf("Expected %+v but got %+v", test.expect, filtered.Series)
break
}
correct[name] = false // Delete it so that there can be no repeats.
//.........这里部分代码省略.........
示例13: TestApplyTransform
func TestApplyTransform(t *testing.T) {
var testTimerange, err = api.NewTimerange(758400000, 758400000+30000*5, 30000)
if err != nil {
t.Fatalf("invalid timerange used for testcase")
return
}
epsilon := 1e-10
list := api.SeriesList{
Series: []api.Timeseries{
{
Values: []float64{0, 1, 2, 3, 4, 5},
TagSet: api.TagSet{
"series": "A",
},
},
{
Values: []float64{2, 2, 1, 1, 3, 3},
TagSet: api.TagSet{
"series": "B",
},
},
{
Values: []float64{0, 1, 2, 3, 2, 1},
TagSet: api.TagSet{
"series": "C",
},
},
},
Timerange: testTimerange,
Name: "test",
}
testCases := []struct {
transform transform
parameter []function.Value
expected map[string][]float64
}{
{
transform: Derivative,
parameter: []function.Value{},
expected: map[string][]float64{
"A": {0, 1.0 / 30, 1.0 / 30, 1.0 / 30, 1.0 / 30, 1.0 / 30},
"B": {0, 0, -1.0 / 30, 0, 2.0 / 30, 0},
"C": {0, 1.0 / 30, 1.0 / 30, 1.0 / 30, -1.0 / 30, -1.0 / 30},
},
},
{
transform: Integral,
parameter: []function.Value{},
expected: map[string][]float64{
"A": {0, 1 * 30, 3 * 30, 6 * 30, 10 * 30, 15 * 30},
"B": {2 * 30, 4 * 30, 5 * 30, 6 * 30, 9 * 30, 12 * 30},
"C": {0, 1 * 30, 3 * 30, 6 * 30, 8 * 30, 9 * 30},
},
},
{
transform: Cumulative,
parameter: []function.Value{},
expected: map[string][]float64{
"A": {0, 1, 3, 6, 10, 15},
"B": {2, 4, 5, 6, 9, 12},
"C": {0, 1, 3, 6, 8, 9},
},
},
}
for _, test := range testCases {
result, err := ApplyTransform(list, test.transform, test.parameter)
if err != nil {
t.Error(err)
continue
}
alreadyUsed := make(map[string]bool)
for _, series := range result.Series {
name := series.TagSet["series"]
expected, ok := test.expected[name]
if !ok {
t.Errorf("Series not present in testcase (A, B, or C). Is instead [%s]", name)
continue
}
if alreadyUsed[name] {
t.Errorf("Multiple series posing as %s", name)
continue
}
alreadyUsed[name] = true
// Lastly, compare the actual values
if len(series.Values) != len(expected) {
t.Errorf("Expected result to have %d entries but has %d entries; for series %s", len(expected), len(series.Values), name)
continue
}
// Check that elements are within epsilon
for i := range series.Values {
if math.Abs(series.Values[i]-expected[i]) > epsilon {
t.Errorf("Expected values for series %s to be %+v but are %+v", name, expected, series.Values)
break
}
}
}
}
}
示例14: TestSet
func TestSet(t *testing.T) {
timerange, err := api.NewTimerange(1300, 1600, 100)
if err != nil {
t.Fatal("invalid timerange used in testcase")
}
newValue := "east"
list := api.SeriesList{
Timerange: timerange,
Name: "ExampleTestSeries!",
Series: []api.Timeseries{
{
Values: []float64{1, 2, 3, 4},
TagSet: api.TagSet{
"name": "A",
"host": "q12",
},
},
{
Values: []float64{6, 7, 3, 1},
TagSet: api.TagSet{
"name": "B",
"host": "r2",
},
},
{
Values: []float64{2, 4, 6, 8},
TagSet: api.TagSet{
"name": "C",
"host": "q12",
"dc": "south",
},
},
{
Values: []float64{5, math.NaN(), 2, math.NaN()},
TagSet: api.TagSet{
"name": "D",
"host": "q12",
"dc": "south",
},
},
},
}
result := SetTag(list, "dc", newValue)
expect := api.SeriesList{
Timerange: timerange,
Name: "ExampleTestSeries!",
Series: []api.Timeseries{
{
Values: []float64{1, 2, 3, 4},
TagSet: api.TagSet{
"name": "A",
"host": "q12",
"dc": "east",
},
},
{
Values: []float64{6, 7, 3, 1},
TagSet: api.TagSet{
"name": "B",
"host": "r2",
"dc": "east",
},
},
{
Values: []float64{2, 4, 6, 8},
TagSet: api.TagSet{
"name": "C",
"host": "q12",
"dc": "east",
},
},
{
Values: []float64{5, math.NaN(), 2, math.NaN()},
TagSet: api.TagSet{
"name": "D",
"host": "q12",
"dc": "east",
},
},
},
}
// Verify that result == expect
a := assert.New(t)
a.EqString(result.Name, expect.Name)
a.Eq(result.Timerange, expect.Timerange)
a.EqInt(len(result.Series), len(expect.Series))
for i := range result.Series {
// Verify that the two are equal
seriesResult := result.Series[i]
seriesExpect := expect.Series[i]
a.EqFloatArray(seriesResult.Values, seriesExpect.Values, 1e-7)
if !seriesResult.TagSet.Equals(seriesExpect.TagSet) {
t.Errorf("Expected series %+v, but got %+v", seriesExpect, seriesResult)
}
}
}
示例15: TestApplyBound
func TestApplyBound(t *testing.T) {
a := assert.New(t)
testTimerange, err := api.NewTimerange(758400000, 758400000+30000*5, 30000)
//{2, nan, nan, nan, 3, 3},
if err != nil {
t.Fatal("invalid timerange used for testcase")
return
}
list := api.SeriesList{
Series: []api.Timeseries{
{
Values: []float64{1, 2, 3, 4, 5, 6},
TagSet: api.TagSet{
"name": "A",
},
},
{
Values: []float64{5, 5, 3, -7, math.NaN(), -20},
TagSet: api.TagSet{
"name": "B",
},
},
{
Values: []float64{math.NaN(), 100, 90, 0, 0, 3},
TagSet: api.TagSet{
"name": "C",
},
},
},
Timerange: testTimerange,
Name: "test",
}
tests := []struct {
lower float64
upper float64
expectBound map[string][]float64
expectLower map[string][]float64
expectUpper map[string][]float64
}{
{
lower: 2,
upper: 5,
expectBound: map[string][]float64{
"A": {2, 2, 3, 4, 5, 5},
"B": {5, 5, 3, 2, math.NaN(), 2},
"C": {math.NaN(), 5, 5, 2, 2, 3},
},
expectLower: map[string][]float64{
"A": {2, 2, 3, 4, 5, 6},
"B": {5, 5, 3, 2, math.NaN(), 2},
"C": {math.NaN(), 100, 90, 2, 2, 3},
},
expectUpper: map[string][]float64{
"A": {1, 2, 3, 4, 5, 5},
"B": {5, 5, 3, -7, math.NaN(), -20},
"C": {math.NaN(), 5, 5, 0, 0, 3},
},
},
{
lower: -10,
upper: 40,
expectBound: map[string][]float64{
"A": {1, 2, 3, 4, 5, 6},
"B": {5, 5, 3, -7, math.NaN(), -10},
"C": {math.NaN(), 40, 40, 0, 0, 3},
},
expectLower: map[string][]float64{
"A": {1, 2, 3, 4, 5, 6},
"B": {5, 5, 3, -7, math.NaN(), -10},
"C": {math.NaN(), 100, 90, 0, 0, 3},
},
expectUpper: map[string][]float64{
"A": {1, 2, 3, 4, 5, 6},
"B": {5, 5, 3, -7, math.NaN(), -20},
"C": {math.NaN(), 40, 40, 0, 0, 3},
},
},
}
for _, test := range tests {
bounders := []struct {
bounder func(ctx *function.EvaluationContext, series api.Timeseries, parameters []function.Value, scale float64) ([]float64, error)
params []function.Value
expected map[string][]float64
name string
}{
{bounder: Bound, params: []function.Value{function.ScalarValue(test.lower), function.ScalarValue(test.upper)}, expected: test.expectBound, name: "bound"},
{bounder: LowerBound, params: []function.Value{function.ScalarValue(test.lower)}, expected: test.expectLower, name: "lower"},
{bounder: UpperBound, params: []function.Value{function.ScalarValue(test.upper)}, expected: test.expectUpper, name: "upper"},
}
for _, bounder := range bounders {
ctx := function.EvaluationContext{EvaluationNotes: []string{}}
bounded, err := ApplyTransform(&ctx, list, bounder.bounder, bounder.params)
if err != nil {
t.Errorf(err.Error())
continue
}
if len(bounded.Series) != len(list.Series) {
t.Errorf("Expected to get %d results but got %d in %+v", len(list.Series), len(bounded.Series), bounded)
continue
//.........这里部分代码省略.........