This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
102 lines (79 loc) · 1.41 KB
/
benchmark_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
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
package python_test
import (
"encoding/json"
"os"
"testing"
"github.com/tsavola/go-python"
)
const (
foo = "hello"
bar = "world"
baz = 1234
)
var (
pyModule python.Object
)
func init() {
err := os.Setenv("PYTHONPATH", "")
if err != nil {
panic(err)
}
pyModule, err = python.Import(nil, "benchmark_test")
if err != nil {
panic(err)
}
}
func BenchmarkPythonOnly(b *testing.B) {
f, err := pyModule.Call(nil, "benchmark_python_only_factory", b.N, foo, bar, baz)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
if _, err = f.Invoke(nil); err != nil {
b.Fatal(err)
}
b.StopTimer()
}
func BenchmarkGoPython(b *testing.B) {
f, err := pyModule.Attr(nil, "function")
if err != nil {
panic(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := f.Invoke(nil, foo, bar, baz); err != nil {
b.Fatal(err)
}
}
b.StopTimer()
}
func BenchmarkJSON(b *testing.B) {
args := struct {
Foo string
Bar string
Baz int
}{
Foo: foo,
Bar: bar,
Baz: baz,
}
resultData := []byte("true")
f, err := pyModule.Call(nil, "benchmark_json_factory", b.N, foo, bar, baz)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(&args); err != nil {
b.Fatal(err)
}
var result bool
if err := json.Unmarshal(resultData, &result); err != nil {
b.Fatal(err)
}
}
if _, err = f.Invoke(nil); err != nil {
b.Fatal(err)
}
b.StopTimer()
}