github.com/go-graphite/carbonapi@v0.17.0/expr/functions/aliasByMetric/function_test.go (about) 1 package aliasByMetric 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/go-graphite/carbonapi/expr/interfaces" 9 "github.com/go-graphite/carbonapi/expr/metadata" 10 "github.com/go-graphite/carbonapi/expr/types" 11 "github.com/go-graphite/carbonapi/pkg/parser" 12 th "github.com/go-graphite/carbonapi/tests" 13 ) 14 15 var ( 16 md []interfaces.FunctionMetadata = New("") 17 ) 18 19 func init() { 20 for _, m := range md { 21 metadata.RegisterFunction(m.Name, m.F) 22 } 23 } 24 25 func TestAliasByMetric(t *testing.T) { 26 now32 := int64(time.Now().Unix()) 27 28 tests := []th.EvalTestItem{ 29 { 30 "aliasByMetric(metric1.foo.bar.baz)", 31 map[parser.MetricRequest][]*types.MetricData{ 32 {Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)}, 33 }, 34 []*types.MetricData{types.MakeMetricData("baz", []float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo.bar.baz")}, 35 }, 36 } 37 38 for _, tt := range tests { 39 testName := tt.Target 40 t.Run(testName, func(t *testing.T) { 41 eval := th.EvaluatorFromFunc(md[0].F) 42 th.TestEvalExpr(t, eval, &tt) 43 }) 44 } 45 46 } 47 48 func BenchmarkAliasByMetric(b *testing.B) { 49 target := "aliasByMetric(metric1.foo.bar.baz)" 50 metrics := map[parser.MetricRequest][]*types.MetricData{ 51 {Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: { 52 types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, 1), 53 }, 54 } 55 56 eval := th.EvaluatorFromFunc(md[0].F) 57 exp, _, err := parser.ParseExpr(target) 58 if err != nil { 59 b.Fatalf("failed to parse %s: %+v", target, err) 60 } 61 62 b.ResetTimer() 63 for n := 0; n < b.N; n++ { 64 g, err := eval.Eval(context.Background(), exp, 0, 1, metrics) 65 if err != nil { 66 b.Fatalf("failed to eval %s: %+v", target, err) 67 } 68 _ = g 69 } 70 }