MCPcopy Index your code
hub / github.com/coder/coder / benchmarkRunner

Function benchmarkRunner

coderd/prometheusmetrics/aggregator_test.go:646–698  ·  view source on GitHub ↗
(b *testing.B, aggregateByLabels []string)

Source from the content-addressed store, hash-verified

644}
645
646func benchmarkRunner(b *testing.B, aggregateByLabels []string) {
647 b.ReportAllocs()
648
649 // Number of metrics to generate and send in each iteration.
650 // Hard-coded to 1024 to avoid overflowing the queue in the metrics aggregator.
651 numMetrics := 1024
652
653 // given
654 registry := prometheus.NewRegistry()
655 metricsAggregator := must(prometheusmetrics.NewMetricsAggregator(slogtest.Make(b, &slogtest.Options{IgnoreErrors: true}), registry, time.Hour, aggregateByLabels))
656
657 ctx, cancelFunc := context.WithCancel(context.Background())
658 b.Cleanup(cancelFunc)
659
660 closeFunc := metricsAggregator.Run(ctx)
661 b.Cleanup(closeFunc)
662
663 ch := make(chan prometheus.Metric)
664 go func() {
665 for {
666 select {
667 case <-ctx.Done():
668 return
669 default:
670 metricsAggregator.Collect(ch)
671 }
672 }
673 }()
674
675 for i := 0; i < b.N; i++ {
676 b.StopTimer()
677 b.Logf("N=%d generating %d metrics", b.N, numMetrics)
678 metrics := make([]*agentproto.Stats_Metric, 0, numMetrics)
679 for i := 0; i < numMetrics; i++ {
680 metrics = append(metrics, genAgentMetric(b))
681 }
682
683 b.Logf("N=%d sending %d metrics", b.N, numMetrics)
684 var nGot atomic.Int64
685 b.StartTimer()
686 metricsAggregator.Update(ctx, testLabels, metrics)
687 for i := 0; i < numMetrics; i++ {
688 select {
689 case <-ctx.Done():
690 b.FailNow()
691 case <-ch:
692 nGot.Add(1)
693 }
694 }
695 b.StopTimer()
696 b.Logf("N=%d got %d metrics", b.N, nGot.Load())
697 }
698}
699
700func genAgentMetric(t testing.TB) *agentproto.Stats_Metric {
701 t.Helper()

Calls 12

NewMetricsAggregatorFunction · 0.92
genAgentMetricFunction · 0.85
FailNowMethod · 0.80
mustFunction · 0.70
CleanupMethod · 0.65
RunMethod · 0.65
CollectMethod · 0.65
LogfMethod · 0.65
UpdateMethod · 0.65
AddMethod · 0.65
DoneMethod · 0.45
LoadMethod · 0.45

Tested by

no test coverage detected