-
Notifications
You must be signed in to change notification settings - Fork 4
/
foreign_test.go
131 lines (109 loc) · 3.49 KB
/
foreign_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
// +build ignore
package semver
import (
"fmt"
"math/rand"
"sort"
"testing"
blang "github.com/blang/semver/v4"
hashicorp "github.com/hashicorp/go-version"
)
var benchLibraryOneV, benchLibraryOneErr = hashicorp.NewVersion(strForBenchmarks)
func BenchmarkLibraryOne_NewVersion(b *testing.B) {
var v, e = hashicorp.NewVersion(strForBenchmarks)
lim := len(VersionsFromGentoo)
for n := 0; n < b.N; n++ {
v, e = hashicorp.NewVersion(string(VersionsFromGentoo[n%lim]))
}
benchLibraryOneV, benchLibraryOneErr = v, e
}
var benchLibraryOneR, benchLibraryOneRErr = hashicorp.NewConstraint(">=1.2.3, <=1.3.0")
func BenchmarkLibraryOne_NewConstraint(b *testing.B) {
var r, e = hashicorp.NewConstraint(">=1.2.3, <=1.3.0")
for n := 0; n < b.N; n++ {
r, e = hashicorp.NewConstraint(">=1.2.3, <=1.3.0")
}
benchLibraryOneR, benchLibraryOneRErr = r, e
}
var benchLibraryOneResult = 5
func BenchmarkLibraryOne_Compare(b *testing.B) {
var v, _ = hashicorp.NewVersion(strForBenchmarks)
r := benchLibraryOneV.Compare(v)
for n := 0; n < b.N; n++ {
r = benchLibraryOneV.Compare(v)
}
benchLibraryOneResult = r
}
func BenchmarkLibraryOne_SortPtr(b *testing.B) {
b.StopTimer()
var erroneous int
unsorted := make([]*hashicorp.Version, len(VersionsFromGentoo))
for n, src := range VersionsFromGentoo {
if v, err := hashicorp.NewVersion(string(src)); err == nil {
unsorted[n] = v
} else {
substitute := fmt.Sprintf("%s.%d", strForBenchmarks, rand.Intn(len(VersionsFromGentoo)))
unsorted[n], _ = hashicorp.NewVersion(substitute)
erroneous++
}
}
b.ReportMetric(float64(erroneous)/float64(len(unsorted)), "substitutes/op")
data := make([]*hashicorp.Version, len(unsorted))
for i := 0; i < b.N; i++ {
copy(data, unsorted)
b.StartTimer()
sort.Sort(hashicorp.Collection(data))
b.StopTimer()
}
}
// Blang published their library after mine, and doing so even did imitate parts
// of my first release.
// Yet, as of writing this, their error rate is a staggering 30%.
var benchLibraryTwoV, benchLibraryTwoErr = blang.Make(strForBenchmarks)
func BenchmarkLibraryTwo_Make(b *testing.B) {
var v, e = blang.Make(strForBenchmarks)
lim := len(VersionsFromGentoo)
for n := 0; n < b.N; n++ {
v, e = blang.Make(string(VersionsFromGentoo[n%lim]))
}
benchLibraryTwoV, benchLibraryTwoErr = v, e
}
var benchLibraryTwoR, benchLibraryTwoRErr = blang.ParseRange(">=1.2.3 <=1.3.0")
func BenchmarkLibraryTwo_ParseRange(b *testing.B) {
var r, e = blang.ParseRange(">=1.2.3 <=1.3.0")
for n := 0; n < b.N; n++ {
r, e = blang.ParseRange(">=1.2.3 <=1.3.0")
}
benchLibraryTwoR, benchLibraryTwoRErr = r, e
}
var benchLibraryTwoResult = 5
func BenchmarkLibraryTwo_Compare(b *testing.B) {
var v, _ = blang.Make(strForBenchmarks)
r := benchLibraryTwoV.Compare(v)
for n := 0; n < b.N; n++ {
r = benchLibraryTwoV.Compare(v)
}
benchLibraryTwoResult = r
}
func BenchmarkLibraryTwo_Sort(b *testing.B) {
b.StopTimer()
var erroneous int
unsorted := make([]blang.Version, len(VersionsFromGentoo))
for n, src := range VersionsFromGentoo {
if v, err := blang.ParseTolerant(string(src)); err == nil {
unsorted[n] = v
} else {
substitute := fmt.Sprintf("%s.%d", strForBenchmarks, rand.Intn(len(VersionsFromGentoo)))
unsorted[n], _ = blang.ParseTolerant(substitute)
erroneous++
}
}
b.ReportMetric(float64(erroneous)/float64(len(unsorted)), "substitutes/op")
data := make([]blang.Version, len(unsorted))
for i := 0; i < b.N; i++ {
copy(data, unsorted)
b.StartTimer()
sort.Sort(blang.Versions(data))
b.StopTimer()
}
}