Skip to content

Commit 22ff0a5

Browse files
authored
bench: add Realistic (40% quoted) benchmark case (#86)
Add generateRealisticCSV / generateRealisticRecords generators and BenchmarkReadAll_Realistic_100K_{Stdlib,SIMD} to cover the case where prefixXOR / PCLMULQDQ fires on partial data — 4 of 10 fields quoted with embedded commas, 6 plain. Also add BenchmarkReadAll_Quoted_100K to provide a 100K-row baseline for the all-quoted worst case.
1 parent 4aaa4d8 commit 22ff0a5

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

benchmark_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,50 @@ func BenchmarkReadAll_Quoted_10K_SIMD(b *testing.B) {
119119
}
120120
}
121121

122+
func BenchmarkReadAll_Quoted_100K_Stdlib(b *testing.B) {
123+
data := generateQuotedCSV(100000, 10)
124+
b.SetBytes(int64(len(data)))
125+
for b.Loop() {
126+
reader := csv.NewReader(bytes.NewReader(data))
127+
reader.FieldsPerRecord = -1
128+
_, _ = reader.ReadAll()
129+
}
130+
}
131+
132+
func BenchmarkReadAll_Quoted_100K_SIMD(b *testing.B) {
133+
data := generateQuotedCSV(100000, 10)
134+
b.SetBytes(int64(len(data)))
135+
for b.Loop() {
136+
reader := NewReader(bytes.NewReader(data))
137+
reader.FieldsPerRecord = -1
138+
_, _ = reader.ReadAll()
139+
}
140+
}
141+
142+
// =============================================================================
143+
// ReadAll Benchmarks - Realistic CSV (40% quoted)
144+
// =============================================================================
145+
146+
func BenchmarkReadAll_Realistic_100K_Stdlib(b *testing.B) {
147+
data := generateRealisticCSV(100000, 10)
148+
b.SetBytes(int64(len(data)))
149+
for b.Loop() {
150+
reader := csv.NewReader(bytes.NewReader(data))
151+
reader.FieldsPerRecord = -1
152+
_, _ = reader.ReadAll()
153+
}
154+
}
155+
156+
func BenchmarkReadAll_Realistic_100K_SIMD(b *testing.B) {
157+
data := generateRealisticCSV(100000, 10)
158+
b.SetBytes(int64(len(data)))
159+
for b.Loop() {
160+
reader := NewReader(bytes.NewReader(data))
161+
reader.FieldsPerRecord = -1
162+
_, _ = reader.ReadAll()
163+
}
164+
}
165+
122166
// =============================================================================
123167
// ReadAll Benchmarks - Mixed CSV
124168
// =============================================================================

test_helpers_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ func generateEscapedQuotesCSV(numRows, numCols int) []byte {
203203
return buf.Bytes()
204204
}
205205

206+
// generateRealisticCSV generates CSV with 4/10 quoted fields containing commas (~40%).
207+
// Row template: "Alice, Smith",30,"Tokyo, JP",engineer,100,"Japan, Asia",active,42,"New York, US",2024
208+
// Cols 0,2,5,8 are quoted; this exercises the prefixXOR / PCLMULQDQ hot path
209+
// without the all-quoted overhead.
210+
func generateRealisticCSV(numRows, _ int) []byte {
211+
var buf bytes.Buffer
212+
for i := 0; i < numRows; i++ {
213+
buf.WriteString(`"Alice, Smith",30,"Tokyo, JP",engineer,100,"Japan, Asia",active,42,"New York, US",2024`)
214+
buf.WriteByte('\n')
215+
}
216+
return buf.Bytes()
217+
}
218+
206219
// =============================================================================
207220
// Writer Benchmark Record Generators
208221
// =============================================================================
@@ -262,3 +275,18 @@ func generateEscapedQuotesRecords(numRows, numCols int) [][]string {
262275
}
263276
return records
264277
}
278+
279+
// generateRealisticRecords returns records matching generateRealisticCSV.
280+
func generateRealisticRecords(numRows, _ int) [][]string {
281+
template := []string{
282+
"Alice, Smith", "30", "Tokyo, JP", "engineer",
283+
"100", "Japan, Asia", "active", "42", "New York, US", "2024",
284+
}
285+
records := make([][]string, numRows)
286+
for i := range records {
287+
row := make([]string, len(template))
288+
copy(row, template)
289+
records[i] = row
290+
}
291+
return records
292+
}

0 commit comments

Comments
 (0)