-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
146 lines (131 loc) · 2.69 KB
/
Copy pathbench_test.go
File metadata and controls
146 lines (131 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package tok_test
import (
"strings"
"testing"
"github.com/GrayCodeAI/tok"
)
// benchmarkText returns a repeat of the base pattern sized to approximately targetSize bytes.
func benchmarkText(targetSize int) string {
base := "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs. "
repeat := targetSize/len(base) + 1
text := strings.Repeat(base, repeat)
if len(text) > targetSize {
text = text[:targetSize]
}
return text
}
func BenchmarkCountTokens(b *testing.B) {
sizes := []struct {
name string
size int
}{
{"100B", 100},
{"1KB", 1024},
{"10KB", 10 * 1024},
{"100KB", 100 * 1024},
}
for _, tc := range sizes {
text := benchmarkText(tc.size)
b.Run(tc.name, func(b *testing.B) {
b.SetBytes(int64(len(text)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
tok.EstimateTokens(text)
}
})
}
}
func BenchmarkCompress(b *testing.B) {
sizes := []struct {
name string
size int
}{
{"100B", 100},
{"1KB", 1024},
{"10KB", 10 * 1024},
{"100KB", 100 * 1024},
}
modes := []struct {
name string
opt tok.Option
}{
{"Minimal", tok.Minimal},
{"Aggressive", tok.Aggressive},
}
for _, sz := range sizes {
text := benchmarkText(sz.size)
for _, mode := range modes {
b.Run(sz.name+"/"+mode.name, func(b *testing.B) {
b.SetBytes(int64(len(text)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
tok.Compress(text, mode.opt)
}
})
}
}
}
func BenchmarkEstimateCost(b *testing.B) {
models := []string{
"gpt-4o",
"claude-sonnet",
"gemini-pro",
}
// Pre-build stats to isolate cost estimation from compression.
stats := tok.Stats{
OriginalTokens: 10000,
FinalTokens: 6000,
TokensSaved: 4000,
ReductionPercent: 40.0,
}
for _, model := range models {
b.Run(model, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
tok.EstimateCostSavings(stats, model)
}
})
}
}
func BenchmarkBPEEncode(b *testing.B) {
sizes := []struct {
name string
size int
}{
{"100B", 100},
{"1KB", 1024},
{"10KB", 10 * 1024},
{"100KB", 100 * 1024},
}
for _, tc := range sizes {
text := benchmarkText(tc.size)
b.Run(tc.name, func(b *testing.B) {
b.SetBytes(int64(len(text)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
tok.EstimateTokensPrecise(text)
}
})
}
}
func BenchmarkStats(b *testing.B) {
sizes := []struct {
name string
size int
}{
{"100B", 100},
{"1KB", 1024},
{"10KB", 10 * 1024},
{"100KB", 100 * 1024},
}
for _, tc := range sizes {
text := benchmarkText(tc.size)
b.Run(tc.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, stats := tok.Compress(text, tok.Minimal)
_ = stats.ReductionPercent
}
})
}
}