| 1 | package dispatch |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "sort" |
| 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 | count := count |
| 41 | b.Run("Turn/NoopInterceptors"+strconv.Itoa(count), func(b *testing.B) { |
| 42 | d := benchmarkDispatcher(extension.PointInputReceive, count) |
| 43 | payload := InputPayload{Text: "hello"} |
| 44 | benchmarkLatency(b, func() error { |
| 45 | _, err := d.Intercept(context.Background(), extension.PointInputReceive, &payload) |
| 46 | return err |
| 47 | }) |
| 48 | }) |
| 49 | b.Run("Tool/NoopInterceptors"+strconv.Itoa(count), func(b *testing.B) { |
| 50 | d := benchmarkDispatcher(extension.PointToolBefore, count) |
| 51 | payload := ToolBeforePayload{Name: "bash", Arguments: `{"cmd":"pwd"}`} |
| 52 | benchmarkLatency(b, func() error { |
| 53 | _, err := d.Intercept(context.Background(), extension.PointToolBefore, &payload) |
| 54 | return err |
| 55 | }) |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func benchmarkDispatcher(point extension.InterceptorPoint, count int) *Dispatcher { |
| 61 | chain := make([]extension.Contribution, 0, count) |
| 62 | clients := make(map[string]Client, count) |
| 63 | for i := 0; i < count; i++ { |
| 64 | pluginID := string(rune('a' + i)) |
| 65 | chain = append(chain, extension.Contribution{ |
| 66 | Kind: extension.KindInterceptor, ID: string(point), |
| 67 | Source: extension.ContributionSource{Scope: extension.ScopePlugin, PluginID: pluginID}, |
| 68 | }) |
| 69 | clients[pluginID] = benchmarkClient{} |
| 70 | } |
| 71 | return New(map[extension.InterceptorPoint][]extension.Contribution{point: chain}, nil, |
| 72 | func(pluginID string) Client { return clients[pluginID] }, nil, Options{}) |
| 73 | } |
| 74 | |
| 75 | func benchmarkLatency(b *testing.B, fn func() error) { |
| 76 | b.Helper() |
| 77 | b.ReportAllocs() |
| 78 | const maxSamples = 100_000 |
| 79 | samples := make([]int64, 0, maxSamples) |
| 80 | for b.Loop() { |
| 81 | start := time.Now() |
| 82 | if err := fn(); err != nil { |
| 83 | b.Fatal(err) |
| 84 | } |
| 85 | if len(samples) < maxSamples { |
| 86 | samples = append(samples, time.Since(start).Nanoseconds()) |
| 87 | } |
| 88 | } |
| 89 | b.StopTimer() |
| 90 | sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] }) |
| 91 | if len(samples) == 0 { |
| 92 | return |
| 93 | } |
| 94 | b.ReportMetric(float64(samples[(len(samples)-1)*50/100]), "p50-ns/op") |
| 95 | b.ReportMetric(float64(samples[(len(samples)-1)*95/100]), "p95-ns/op") |
| 96 | } |
| 97 |