返回 DeepSeek-Reasonix
benchmark_test.go
根目录 / internal / extension / benchmark_test.go
1 package extension
2
3 import (
4 "context"
5 "sort"
6 "testing"
7 "time"
8 )
9
10 // BenchmarkExtensionKernelStartup measures the immutable snapshot assembly
11 // portion of startup with no extensions and with a representative 64-entry
12 // interceptor catalog. Process spawn and sidecar handshake latency are
13 // intentionally excluded and should be measured by extension authors.
14 func BenchmarkExtensionKernelStartup(b *testing.B) {
15 b.Run("NoExtensions", func(b *testing.B) {
16 benchmarkBuildLatency(b, NewBuilder().WithSystemPrompt("stable prompt"))
17 })
18
19 contributions := make([]Contribution, 0, 64)
20 for i := 0; i < 64; i++ {
21 contributions = append(contributions, Contribution{
22 Kind: KindInterceptor,
23 ID: string(PointToolBefore),
24 Source: ContributionSource{
25 Scope: ScopePlugin,
26 PluginID: "plugin-" + benchmarkIndex(i),
27 },
28 Priority: i%21 - 10,
29 })
30 }
31 builder := NewBuilder().WithSystemPrompt("stable prompt").AddContributor(ContributorFunc{
32 ContributorName: "benchmark",
33 Fn: func(context.Context) ([]Contribution, error) {
34 return contributions, nil
35 },
36 })
37 b.Run("64Interceptors", func(b *testing.B) { benchmarkBuildLatency(b, builder) })
38 }
39
40 func benchmarkIndex(i int) string {
41 const digits = "0123456789abcdef"
42 return string([]byte{digits[(i>>4)&15], digits[i&15]})
43 }
44
45 func benchmarkBuildLatency(b *testing.B, builder *Builder) {
46 b.Helper()
47 b.ReportAllocs()
48 const maxSamples = 100_000
49 samples := make([]int64, 0, maxSamples)
50 for b.Loop() {
51 start := time.Now()
52 _, runtimeSet, err := builder.Build(context.Background())
53 if err != nil {
54 b.Fatal(err)
55 }
56 if err := runtimeSet.Close(); err != nil {
57 b.Fatal(err)
58 }
59 if len(samples) < maxSamples {
60 samples = append(samples, time.Since(start).Nanoseconds())
61 }
62 }
63 b.StopTimer()
64 sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] })
65 if len(samples) == 0 {
66 return
67 }
68 b.ReportMetric(float64(samples[(len(samples)-1)*50/100]), "p50-ns/op")
69 b.ReportMetric(float64(samples[(len(samples)-1)*95/100]), "p95-ns/op")
70 }
71
71 lines GO