@@ -529,26 +529,83 @@ func BenchmarkSnapshotMapPooling(b *testing.B) {
529529
530530// Benchmark the overall subscope creation with all Phase 4 optimizations
531531func BenchmarkSubscopeCreationPhase4 (b * testing.B ) {
532- root , closer := NewRootScope (ScopeOptions {
533- Prefix : "test" ,
534- Reporter : NullStatsReporter ,
532+ // Test subscope creation with all optimizations enabled
533+ root := newRootScope (ScopeOptions {
534+ Prefix : "test" ,
535+ Tags : map [string ]string {
536+ "service" : "benchmark" ,
537+ },
535538 }, 0 )
536- defer closer .Close ()
537539
538540 tags := map [string ]string {
539- "service " : " test-service " ,
540- "environment " : "production " ,
541- "region " : "us-west-2 " ,
541+ "endpoint " : "/api/v1/ test" ,
542+ "method " : "GET " ,
543+ "status " : "200 " ,
542544 }
543545
544- b .Run ("SubscopeCreationWithPhase4" , func (b * testing.B ) {
545- b .ResetTimer ()
546- for i := 0 ; i < b .N ; i ++ {
547- subscope := root .Tagged (tags ).SubScope ("metrics" )
548- // Create some metrics to trigger slice allocations
549- subscope .Counter ("requests" ).Inc (1 )
550- subscope .Gauge ("cpu_usage" ).Update (50.0 )
551- subscope .Histogram ("latency" , MustMakeLinearValueBuckets (0 , 10 , 10 )).RecordValue (5.0 )
552- }
553- })
546+ b .ResetTimer ()
547+ for i := 0 ; i < b .N ; i ++ {
548+ // Create subscope with tags
549+ _ = root .Tagged (tags )
550+ }
551+ }
552+
553+ func TestOptimizedFlushReportsAllMetrics (t * testing.T ) {
554+ // Enable optimized flush
555+ EnableOptimizedFlush ()
556+ defer DisableOptimizedFlush ()
557+
558+ // Create a test reporter to capture reported metrics
559+ r := newTestStatsReporter ()
560+
561+ // Create root scope with the test reporter
562+ root , closer := NewRootScope (ScopeOptions {
563+ Reporter : r ,
564+ OmitCardinalityMetrics : true , // Disable cardinality metrics to simplify test
565+ }, 50 * time .Millisecond )
566+ defer closer .Close ()
567+
568+ // Create multiple scopes and metrics to simulate high cardinality
569+ numScopes := 10
570+ for i := 0 ; i < numScopes ; i ++ {
571+ scope := root .Tagged (map [string ]string {"instance" : fmt .Sprintf ("instance-%d" , i )})
572+
573+ // Create different types of metrics with unique names
574+ r .cg .Add (1 )
575+ counter := scope .Counter (fmt .Sprintf ("test_counter_%d" , i ))
576+ counter .Inc (int64 (i + 1 ))
577+
578+ r .gg .Add (1 )
579+ gauge := scope .Gauge (fmt .Sprintf ("test_gauge_%d" , i ))
580+ gauge .Update (float64 (i * 2 ))
581+
582+ r .tg .Add (1 )
583+ timer := scope .Timer (fmt .Sprintf ("test_timer_%d" , i ))
584+ timer .Record (time .Duration (i ) * time .Millisecond )
585+
586+ r .hg .Add (1 )
587+ histogram := scope .Histogram (fmt .Sprintf ("test_histogram_%d" , i ), ValueBuckets {1 , 5 , 10 })
588+ histogram .RecordValue (float64 (i ))
589+ }
590+
591+ // Wait for all metrics to be reported
592+ r .WaitAll ()
593+
594+ // Verify all metric types were reported by checking the maps
595+ counters := r .getCounters ()
596+ gauges := r .getGauges ()
597+ timers := r .getTimers ()
598+ histograms := r .getHistograms ()
599+
600+ // Verify all metric types were reported
601+ assert .True (t , len (counters ) > 0 , "Counters should be reported with optimized flush" )
602+ assert .True (t , len (gauges ) > 0 , "Gauges should be reported with optimized flush" )
603+ assert .True (t , len (timers ) > 0 , "Timers should be reported with optimized flush" )
604+ assert .True (t , len (histograms ) > 0 , "Histograms should be reported with optimized flush" )
605+
606+ // Verify we have the expected number of metrics (should be numScopes each)
607+ assert .Equal (t , numScopes , len (counters ), "Should have %d counter metrics" , numScopes )
608+ assert .Equal (t , numScopes , len (gauges ), "Should have %d gauge metrics" , numScopes )
609+ assert .Equal (t , numScopes , len (timers ), "Should have %d timer metrics" , numScopes )
610+ assert .Equal (t , numScopes , len (histograms ), "Should have %d histogram metrics" , numScopes )
554611}
0 commit comments