-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
57 lines (48 loc) · 1.12 KB
/
bench_test.go
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
package ffibench_test
import (
"encoding/json"
"io"
"strings"
"testing"
"github.com/iboss-ptk/ffibench"
)
func BenchmarkAddFFI(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
ffibench.Add()
}
})
}
func BenchmarkFibonacciFFI(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
ffibench.Fibonacci(100000)
}
})
}
// use marhalling `1` as a baseline comparison for the overhead of calling CGO
// There are 2 versions of this benchmark:
// - `BenchmarkLowLevelJSONCParseIntBaseline` use lower level primitives to parse the JSON string
// - `BenchmarkJSONMarshalCParseIntBaseline` use `json.Marshal` to parse the JSON string
func BenchmarkLowLevelJSONCParseIntBaseline(b *testing.B) {
msg := `1`
b.RunParallel(func(pb *testing.PB) {
var dst int
r := strings.NewReader(msg)
dec := json.NewDecoder(r)
for pb.Next() {
r.Seek(0, io.SeekStart)
if err := dec.Decode(&dst); err != nil {
panic(err)
}
}
})
}
func BenchmarkJSONMarshalCParseIntBaseline(b *testing.B) {
msg := `1`
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
json.Marshal(msg)
}
})
}