-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
87 lines (75 loc) · 1.77 KB
/
bench_test.go
File metadata and controls
87 lines (75 loc) · 1.77 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
84
85
86
87
package each_test
import (
"testing"
"github.com/bold-minds/each"
)
var benchUsers = func() []User {
out := make([]User, 0, 1000)
for i := 0; i < 1000; i++ {
role := "editor"
if i%5 == 0 {
role = "admin"
}
out = append(out, User{
ID: i,
Name: "user",
Role: role,
Active: i%3 != 0,
})
}
return out
}()
func BenchmarkFind_Hit(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = each.Find(benchUsers, func(u User) bool { return u.ID == 500 })
}
}
func BenchmarkFind_Miss(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = each.Find(benchUsers, func(u User) bool { return u.ID == -1 })
}
}
func BenchmarkFilter_Half(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = each.Filter(benchUsers, func(u User) bool { return u.Active })
}
}
func BenchmarkGroupBy(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = each.GroupBy(benchUsers, func(u User) string { return u.Role })
}
}
func BenchmarkKeyBy(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = each.KeyBy(benchUsers, func(u User) int { return u.ID })
}
}
func BenchmarkPartition(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = each.Partition(benchUsers, func(u User) bool { return u.Active })
}
}
func BenchmarkCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = each.Count(benchUsers, func(u User) bool { return u.Role == "admin" })
}
}
func BenchmarkEvery_AllTrue(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = each.Every(benchUsers, func(u User) bool { return u.ID >= 0 })
}
}
func BenchmarkEvery_EarlyFail(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = each.Every(benchUsers, func(u User) bool { return u.ID > 500 })
}
}