/
benchmark_test.go
94 lines (79 loc) · 1.75 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
package main
import (
"strconv"
"sync"
"testing"
)
func benchmarkStrconvComparison(i int, b *testing.B) {
var IDMutex sync.Mutex
highestID := "122222222"
if i == 0 {
for n := 0; n < b.N; n++ {
postIDint, _ := strconv.Atoi("110312919")
IDMutex.Lock()
highestIDint, _ := strconv.Atoi(highestID)
if postIDint >= highestIDint {
highestID = "110312919"
}
IDMutex.Unlock()
}
} else {
b.SetParallelism(i)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
postIDint, _ := strconv.Atoi("110312919")
IDMutex.Lock()
highestIDint, _ := strconv.Atoi(highestID)
if postIDint >= highestIDint {
highestID = "110312919"
}
IDMutex.Unlock()
}
})
}
}
func benchmarkInt64Comparison(i int, b *testing.B) {
var IDMutex sync.Mutex
highestID := int64(122222222)
if i == 0 {
for n := 0; n < b.N; n++ {
idNew := int64(132145174)
IDMutex.Lock()
if highestID < idNew {
highestID = idNew
}
IDMutex.Unlock()
}
} else {
b.SetParallelism(i)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
idNew := int64(132145174)
IDMutex.Lock()
if highestID < idNew {
highestID = idNew
}
IDMutex.Unlock()
}
})
}
}
// === Benchmarks ===
func BenchmarkStrconvComparisonSingle(b *testing.B) {
benchmarkStrconvComparison(0, b)
}
func BenchmarkStrconvComparisonParallel1(b *testing.B) {
benchmarkStrconvComparison(1, b)
}
func BenchmarkStrconvComparisonParallel10(b *testing.B) {
benchmarkStrconvComparison(10, b)
}
func BenchmarkInt64ComparisonSingle(b *testing.B) {
benchmarkInt64Comparison(0, b)
}
func BenchmarkInt64ComparisonParallel1(b *testing.B) {
benchmarkInt64Comparison(1, b)
}
func BenchmarkInt64ComparisonParallel10(b *testing.B) {
benchmarkInt64Comparison(10, b)
}