-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpr_benchmark_test.go
152 lines (134 loc) · 3.71 KB
/
expr_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
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
147
148
149
150
151
152
// Copyright 2020-2023 The Expr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expr_test
import (
"math"
"testing"
"github.com/muktihari/expr"
)
var exprs = [...]string{
expr.KindBoolean: "((3 + 2 == 5) && (7 * 2 <= 10)) || (!(4 + 1 == 5) && (2 / 1 < 5)) || ((8 >= 10) && (!(6 + 3 > 9))) || (9 % 4 == 1)", // true
expr.KindInt: "((10 + 2 * 5) / (7 - 3)) + ((15 - 2 * 3) * 2)", // int64: 23
expr.KindFloat: "(((12 + 5) * (7 - 3)) / (2 + 1) + (6 * (4 - 2)) - 5) * 2 + (8 / 2.2)", // float64: 62.9696969697
expr.KindImag: "((3 + 2i) * (2 - 4i)) / ((1 + 3i) + (2i - 1))", // complex128: (-1.6-2.8i)
}
func BenchmarkAny(b *testing.B) {
results := [...]interface{}{
expr.KindBoolean: true,
expr.KindInt: int64(23),
expr.KindFloat: float64(62.9696969697),
expr.KindImag: complex(-1.6, -2.8),
}
for i, e := range exprs {
i, e := i, e
kind := expr.Kind(i)
if e == "" {
continue
}
b.Run(kind.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
v, err := expr.Any(e)
if err != nil {
b.Fatalf("expected nil, got: %v", err)
}
// handle float prec
vf, vok := v.(float64)
rf, rok := results[kind].(float64)
if vok && rok {
digit := 1e10
v := math.Round(vf*digit) / digit
r := math.Round(rf*digit) / digit
if v != r {
b.Fatalf("expected value %v, got: %v", r, v)
}
continue
}
if v != results[kind] {
b.Fatalf("expected value %v, got: %v", results[kind], v)
}
}
})
}
}
func BenchmarkBoolean(b *testing.B) {
var (
e string = exprs[expr.KindBoolean]
r bool = true
)
b.Run(expr.KindBoolean.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
v, err := expr.Bool(e)
if err != nil {
b.Fatalf("expected nil, got: %v", err)
}
if v != r {
b.Fatalf("expected %t, got: %t", true, v)
}
}
})
}
func BenchmarkComplex128(b *testing.B) {
var (
e string = exprs[expr.KindImag]
r complex128 = complex(-1.6, -2.8)
)
b.Run(expr.KindImag.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
v, err := expr.Complex128(e)
if err != nil {
b.Fatalf("expected nil, got: %v", err)
}
if v != r {
b.Fatalf("expected value: %f, got: %f", r, v)
}
}
})
}
func BenchmarkFloat64(b *testing.B) {
var (
e string = exprs[expr.KindFloat]
r float64 = 62.9696969697
)
b.Run(expr.KindFloat.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
v, err := expr.Float64(e)
if err != nil {
b.Fatalf("expected nil, got: %v", err)
}
// handle float prec
digit := 1e10
v = math.Round(v*digit) / digit
r = math.Round(r*digit) / digit
if v != r {
b.Fatalf("expected value: %f, got: %f", r, v)
}
}
})
}
func BenchmarkInt64(b *testing.B) {
var (
e string = exprs[expr.KindInt]
r int64 = 23
)
b.Run(expr.KindInt.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
v, err := expr.Int64(e)
if err != nil {
b.Fatalf("expected nil, got: %v", err)
}
if v != r {
b.Fatalf("expected value: %d, got: %d", r, v)
}
}
})
}