-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
83 lines (72 loc) · 1.74 KB
/
Copy pathbenchmark_test.go
File metadata and controls
83 lines (72 loc) · 1.74 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
package magic
import (
"bytes"
"testing"
)
var benchmarkResult Result
var benchmarkText1MiB = bytes.Repeat([]byte{'x'}, 1<<20)
var benchmarkJSON4KiB = jsonFixture(4096)
var benchmarkCorpus = [][]byte{
padFixture([]byte("package magic\n\nfunc Detect(data []byte) Result { return Result{} }\n")),
padFixture([]byte("\xef\xbb\xbfUnicode text: héllo, 世界\n")),
padFixture([]byte("<?xml version=\"1.0\"?><svg >")),
benchmarkJSON4KiB,
padFixture([]byte("PK\x03\x04")),
padFixture([]byte("\x89PNG\r\n\x1a\n")),
padFixture([]byte{0xff}),
}
func BenchmarkDetect4KiBText(b *testing.B) {
input := benchmarkCorpus[0]
b.ReportAllocs()
b.SetBytes(int64(len(input)))
b.ResetTimer()
for range b.N {
benchmarkResult = Detect(input)
}
}
func BenchmarkDetect4KiBCorpus(b *testing.B) {
b.ReportAllocs()
b.SetBytes(4096)
b.ResetTimer()
index := 0
for range b.N {
benchmarkResult = Detect(benchmarkCorpus[index])
index++
if index == len(benchmarkCorpus) {
index = 0
}
}
}
func BenchmarkDetect4KiBParallel(b *testing.B) {
input := benchmarkCorpus[0]
b.ReportAllocs()
b.SetBytes(int64(len(input)))
b.RunParallel(func(parallel *testing.PB) {
var result Result
for parallel.Next() {
result = Detect(input)
}
benchmarkResult = result
})
}
func BenchmarkDetect1MiBText(b *testing.B) {
input := benchmarkText1MiB
b.ReportAllocs()
b.SetBytes(int64(len(input)))
b.ResetTimer()
for range b.N {
benchmarkResult = Detect(input)
}
}
func padFixture(prefix []byte) []byte {
if len(prefix) >= 4096 {
return prefix[:4096]
}
return append(bytes.Clone(prefix), bytes.Repeat([]byte{'x'}, 4096-len(prefix))...)
}
func jsonFixture(size int) []byte {
data := bytes.Repeat([]byte{'x'}, size)
data[0] = '"'
data[len(data)-1] = '"'
return data
}