| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metrix |
| 4 | |
| 5 | import "testing" |
| 6 | |
| 7 | // Baseline results (2026-02-13, darwin/arm64, Apple M4 Pro): |
| 8 | // Command: |
| 9 | // |
| 10 | // go test ./pkg/metrix -run '^$' -bench 'RuntimeStore' -benchmem |
| 11 | // |
| 12 | // BenchmarkRuntimeStoreCounterParallelAdd/p1-14 2407790 495.2 ns/op 1040 B/op 9 allocs/op |
| 13 | // BenchmarkRuntimeStoreCounterParallelAdd/p4-14 2488057 503.1 ns/op 1040 B/op 9 allocs/op |
| 14 | // BenchmarkRuntimeStoreCounterParallelAdd/p16-14 2054728 580.1 ns/op 1040 B/op 9 allocs/op |
| 15 | // BenchmarkRuntimeStoreGaugeParallelSet/p1-14 2453990 487.7 ns/op 1040 B/op 9 allocs/op |
| 16 | // BenchmarkRuntimeStoreGaugeParallelSet/p4-14 2452767 492.4 ns/op 1040 B/op 9 allocs/op |
| 17 | // BenchmarkRuntimeStoreGaugeParallelSet/p16-14 1964413 591.8 ns/op 1040 B/op 9 allocs/op |
| 18 | // BenchmarkRuntimeStoreMixedTypedWriteAndReadFlatten-14 60756 18938 ns/op 71350 B/op 237 allocs/op |
| 19 | // |
| 20 | // After runtime lazy reader index optimization (2026-02-13): |
| 21 | // |
| 22 | // BenchmarkRuntimeStoreCounterParallelAdd/p1-14 3417770 344.1 ns/op 560 B/op 4 allocs/op |
| 23 | // BenchmarkRuntimeStoreCounterParallelAdd/p4-14 3289084 358.2 ns/op 560 B/op 4 allocs/op |
| 24 | // BenchmarkRuntimeStoreCounterParallelAdd/p16-14 2758773 434.2 ns/op 560 B/op 4 allocs/op |
| 25 | // BenchmarkRuntimeStoreGaugeParallelSet/p1-14 3440482 347.4 ns/op 560 B/op 4 allocs/op |
| 26 | // BenchmarkRuntimeStoreGaugeParallelSet/p4-14 3268348 372.4 ns/op 560 B/op 4 allocs/op |
| 27 | // BenchmarkRuntimeStoreGaugeParallelSet/p16-14 2722263 436.0 ns/op 560 B/op 4 allocs/op |
| 28 | // BenchmarkRuntimeStoreMixedTypedWriteAndReadFlatten-14 97585 10916 ns/op 13366 B/op 155 allocs/op |
| 29 | // |
| 30 | // After runtime overlay+compaction write path (2026-02-19): |
| 31 | // |
| 32 | // BenchmarkRuntimeStoreCounterParallelAdd/p1-14 3759434 308.4 ns/op 622 B/op 4 allocs/op |
| 33 | // BenchmarkRuntimeStoreCounterParallelAdd/p4-14 3838068 312.3 ns/op 622 B/op 4 allocs/op |
| 34 | // BenchmarkRuntimeStoreCounterParallelAdd/p16-14 3553308 374.8 ns/op 622 B/op 4 allocs/op |
| 35 | // BenchmarkRuntimeStoreGaugeParallelSet/p1-14 3852787 309.8 ns/op 622 B/op 4 allocs/op |
| 36 | // BenchmarkRuntimeStoreGaugeParallelSet/p4-14 3797656 314.0 ns/op 622 B/op 4 allocs/op |
| 37 | // BenchmarkRuntimeStoreGaugeParallelSet/p16-14 3520359 341.7 ns/op 622 B/op 4 allocs/op |
| 38 | // BenchmarkRuntimeStoreMixedTypedWriteAndReadFlatten-14 93160 12230 ns/op 15275 B/op 159 allocs/op |
| 39 | func BenchmarkRuntimeStoreCounterParallelAdd(b *testing.B) { |
| 40 | tests := map[string]struct { |
| 41 | parallelism int |
| 42 | }{ |
| 43 | "p1": {parallelism: 1}, |
| 44 | "p4": {parallelism: 4}, |
| 45 | "p16": {parallelism: 16}, |
| 46 | } |
| 47 | |
| 48 | for name, tc := range tests { |
| 49 | b.Run(name, func(b *testing.B) { |
| 50 | s := NewRuntimeStore() |
| 51 | c := s.Write().StatefulMeter("runtime").Counter("events_total") |
| 52 | |
| 53 | b.SetParallelism(tc.parallelism) |
| 54 | b.ReportAllocs() |
| 55 | b.ResetTimer() |
| 56 | b.RunParallel(func(pb *testing.PB) { |
| 57 | for pb.Next() { |
| 58 | c.Add(1) |
| 59 | } |
| 60 | }) |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func BenchmarkRuntimeStoreGaugeParallelSet(b *testing.B) { |
| 66 | tests := map[string]struct { |
| 67 | parallelism int |
| 68 | }{ |
| 69 | "p1": {parallelism: 1}, |
| 70 | "p4": {parallelism: 4}, |
| 71 | "p16": {parallelism: 16}, |
| 72 | } |
| 73 | |
| 74 | for name, tc := range tests { |
| 75 | b.Run(name, func(b *testing.B) { |
| 76 | s := NewRuntimeStore() |
| 77 | g := s.Write().StatefulMeter("runtime").Gauge("heap_bytes") |
| 78 | |
| 79 | b.SetParallelism(tc.parallelism) |
| 80 | b.ReportAllocs() |
| 81 | b.ResetTimer() |
| 82 | b.RunParallel(func(pb *testing.PB) { |
| 83 | var v SampleValue |
| 84 | for pb.Next() { |
| 85 | g.Set(v) |
| 86 | v += 1 |
| 87 | } |
| 88 | }) |
| 89 | }) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func BenchmarkRuntimeStoreMixedTypedWriteAndReadFlatten(b *testing.B) { |
| 94 | s := NewRuntimeStore() |
| 95 | m := s.Write().StatefulMeter("runtime") |
| 96 | g := m.Gauge("queue_depth") |
| 97 | c := m.Counter("jobs_total") |
| 98 | h := m.Histogram("latency", WithHistogramBounds(1, 2, 5)) |
| 99 | sum := m.Summary("request_time", WithSummaryQuantiles(0.5, 0.9, 0.99)) |
| 100 | ss := m.StateSet("mode", WithStateSetStates("maintenance", "operational"), WithStateSetMode(ModeEnum)) |
| 101 | |
| 102 | b.ReportAllocs() |
| 103 | b.ResetTimer() |
| 104 | for i := 0; i < b.N; i++ { |
| 105 | g.Set(SampleValue(i % 100)) |
| 106 | c.Add(1) |
| 107 | h.Observe(SampleValue((i % 7) + 1)) |
| 108 | sum.Observe(SampleValue((i % 11) + 1)) |
| 109 | ss.Enable("operational") |
| 110 | |
| 111 | // Simulate chart-engine scalar path over runtime metrics. |
| 112 | r := s.Read(ReadFlatten()) |
| 113 | r.ForEachSeries(func(_ string, _ LabelView, _ SampleValue) {}) |
| 114 | } |
| 115 | } |