| 1 | package dispatch |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "slices" |
| 7 | "strconv" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "reasonix/internal/extension" |
| 12 | "reasonix/internal/extension/protocol" |
| 13 | ) |
| 14 | |
| 15 | type benchmarkClient struct{} |
| 16 | |
| 17 | func (benchmarkClient) Intercept(context.Context, protocol.InterceptEvent, json.RawMessage, time.Duration) (protocol.InterceptResult, error) { |
| 18 | return protocol.InterceptResult{Decision: protocol.DecisionContinue}, nil |
| 19 | } |
| 20 | |
| 21 | func (benchmarkClient) TryNotifyEvent(protocol.InterceptEvent, json.RawMessage) error { return nil } |
| 22 | |
| 23 | // BenchmarkDispatchLatency captures both Go's aggregate ns/op and sampled |
| 24 | // p50/p95 latency for the host guard, one no-op sidecar, and four serial |
| 25 | // no-op sidecars on turn and tool hot paths. Real sidecar latency is additive |
| 26 | // on top of these host-only numbers. |
| 27 | func BenchmarkDispatchLatency(b *testing.B) { |
| 28 | b.Run("Turn/NoExtensionsHostGuard", func(b *testing.B) { |
| 29 | var d *Dispatcher |
| 30 | payload := InputPayload{Text: "hello"} |
| 31 | benchmarkLatency(b, func() error { |
| 32 | if d != nil { |
| 33 | _, err := d.Intercept(context.Background(), extension.PointInputReceive, &payload) |
| 34 | return err |
| 35 | } |
| 36 | return nil |
| 37 | }) |
| 38 | }) |
| 39 | for _, count := range []int{1, 4} { |
| 40 | b.Run("Turn/NoopInterceptors"+strconv.Itoa(count), func(b *testing.B) { |
| 41 | d := benchmarkDispatcher(extension.PointInputReceive, count) |
| 42 | payload := InputPayload{Text: "hello"} |
| 43 | benchmarkLatency(b, func() error { |
| 44 | _, err := d.Intercept(context.Background(), extension.PointInputReceive, &payload) |
| 45 | return err |
| 46 | }) |
| 47 | }) |
| 48 | b.Run("Tool/NoopInterceptors"+strconv.Itoa(count), func(b *testing.B) { |
| 49 | d := benchmarkDispatcher(extension.PointToolBefore, count) |
| 50 | payload := ToolBeforePayload{Name: "bash", Arguments: `{"cmd":"pwd"}`} |
| 51 | benchmarkLatency(b, func() error { |
| 52 | _, err := d.Intercept(context.Background(), extension.PointToolBefore, &payload) |
| 53 | return err |
| 54 | }) |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func benchmarkDispatcher(point extension.InterceptorPoint, count int) *Dispatcher { |
| 60 | chain := make([]extension.Contribution, 0, count) |
| 61 | clients := make(map[string]Client, count) |
| 62 | for i := range count { |
| 63 | pluginID := string(rune('a' + i)) |
| 64 | chain = append(chain, extension.Contribution{ |
| 65 | Kind: extension.KindInterceptor, ID: string(point), |
| 66 | Source: extension.ContributionSource{Scope: extension.ScopePlugin, PluginID: pluginID}, |
| 67 | }) |
| 68 | clients[pluginID] = benchmarkClient{} |
| 69 | } |
| 70 | return New(map[extension.InterceptorPoint][]extension.Contribution{point: chain}, nil, |
| 71 | func(pluginID string) Client { return clients[pluginID] }, nil, Options{}) |
| 72 | } |
| 73 | |
| 74 | func benchmarkLatency(b *testing.B, fn func() error) { |
| 75 | b.Helper() |
| 76 | b.ReportAllocs() |
| 77 | const maxSamples = 100_000 |
| 78 | samples := make([]int64, 0, maxSamples) |
| 79 | for b.Loop() { |
| 80 | start := time.Now() |
| 81 | if err := fn(); err != nil { |
| 82 | b.Fatal(err) |
| 83 | } |
| 84 | if len(samples) < maxSamples { |
| 85 | samples = append(samples, time.Since(start).Nanoseconds()) |
| 86 | } |
| 87 | } |
| 88 | b.StopTimer() |
| 89 | slices.Sort(samples) |
| 90 | if len(samples) == 0 { |
| 91 | return |
| 92 | } |
| 93 | b.ReportMetric(float64(samples[(len(samples)-1)*50/100]), "p50-ns/op") |
| 94 | b.ReportMetric(float64(samples[(len(samples)-1)*95/100]), "p95-ns/op") |
| 95 | } |
| 96 |