1package semver
2
3import (
4	"testing"
5)
6
7/* Constraint creation benchmarks */
8
9func benchNewConstraint(c string, b *testing.B) {
10	b.ReportAllocs()
11	b.ResetTimer()
12	for i := 0; i < b.N; i++ {
13		_, _ = NewConstraint(c)
14	}
15}
16
17func BenchmarkNewConstraintUnary(b *testing.B) {
18	b.ReportAllocs()
19	b.ResetTimer()
20	benchNewConstraint("=2.0", b)
21}
22
23func BenchmarkNewConstraintTilde(b *testing.B) {
24	b.ReportAllocs()
25	b.ResetTimer()
26	benchNewConstraint("~2.0.0", b)
27}
28
29func BenchmarkNewConstraintCaret(b *testing.B) {
30	b.ReportAllocs()
31	b.ResetTimer()
32	benchNewConstraint("^2.0.0", b)
33}
34
35func BenchmarkNewConstraintWildcard(b *testing.B) {
36	b.ReportAllocs()
37	b.ResetTimer()
38	benchNewConstraint("1.x", b)
39}
40
41func BenchmarkNewConstraintRange(b *testing.B) {
42	b.ReportAllocs()
43	b.ResetTimer()
44	benchNewConstraint(">=2.1.x, <3.1.0", b)
45}
46
47func BenchmarkNewConstraintUnion(b *testing.B) {
48	b.ReportAllocs()
49	b.ResetTimer()
50	benchNewConstraint("~2.0.0 || =3.1.0", b)
51}
52
53/* Check benchmarks */
54
55func benchCheckVersion(c, v string, b *testing.B) {
56	b.ReportAllocs()
57	b.ResetTimer()
58	version, _ := NewVersion(v)
59	constraint, _ := NewConstraint(c)
60
61	for i := 0; i < b.N; i++ {
62		constraint.Check(version)
63	}
64}
65
66func BenchmarkCheckVersionUnary(b *testing.B) {
67	b.ReportAllocs()
68	b.ResetTimer()
69	benchCheckVersion("=2.0", "2.0.0", b)
70}
71
72func BenchmarkCheckVersionTilde(b *testing.B) {
73	b.ReportAllocs()
74	b.ResetTimer()
75	benchCheckVersion("~2.0.0", "2.0.5", b)
76}
77
78func BenchmarkCheckVersionCaret(b *testing.B) {
79	b.ReportAllocs()
80	b.ResetTimer()
81	benchCheckVersion("^2.0.0", "2.1.0", b)
82}
83
84func BenchmarkCheckVersionWildcard(b *testing.B) {
85	b.ReportAllocs()
86	b.ResetTimer()
87	benchCheckVersion("1.x", "1.4.0", b)
88}
89
90func BenchmarkCheckVersionRange(b *testing.B) {
91	b.ReportAllocs()
92	b.ResetTimer()
93	benchCheckVersion(">=2.1.x, <3.1.0", "2.4.5", b)
94}
95
96func BenchmarkCheckVersionUnion(b *testing.B) {
97	b.ReportAllocs()
98	b.ResetTimer()
99	benchCheckVersion("~2.0.0 || =3.1.0", "3.1.0", b)
100}
101
102func benchValidateVersion(c, v string, b *testing.B) {
103	b.ReportAllocs()
104	b.ResetTimer()
105	version, _ := NewVersion(v)
106	constraint, _ := NewConstraint(c)
107
108	for i := 0; i < b.N; i++ {
109		constraint.Validate(version)
110	}
111}
112
113/* Validate benchmarks, including fails */
114
115func BenchmarkValidateVersionUnary(b *testing.B) {
116	b.ReportAllocs()
117	b.ResetTimer()
118	benchValidateVersion("=2.0", "2.0.0", b)
119}
120
121func BenchmarkValidateVersionUnaryFail(b *testing.B) {
122	b.ReportAllocs()
123	b.ResetTimer()
124	benchValidateVersion("=2.0", "2.0.1", b)
125}
126
127func BenchmarkValidateVersionTilde(b *testing.B) {
128	b.ReportAllocs()
129	b.ResetTimer()
130	benchValidateVersion("~2.0.0", "2.0.5", b)
131}
132
133func BenchmarkValidateVersionTildeFail(b *testing.B) {
134	b.ReportAllocs()
135	b.ResetTimer()
136	benchValidateVersion("~2.0.0", "1.0.5", b)
137}
138
139func BenchmarkValidateVersionCaret(b *testing.B) {
140	b.ReportAllocs()
141	b.ResetTimer()
142	benchValidateVersion("^2.0.0", "2.1.0", b)
143}
144
145func BenchmarkValidateVersionCaretFail(b *testing.B) {
146	b.ReportAllocs()
147	b.ResetTimer()
148	benchValidateVersion("^2.0.0", "4.1.0", b)
149}
150
151func BenchmarkValidateVersionWildcard(b *testing.B) {
152	b.ReportAllocs()
153	b.ResetTimer()
154	benchValidateVersion("1.x", "1.4.0", b)
155}
156
157func BenchmarkValidateVersionWildcardFail(b *testing.B) {
158	b.ReportAllocs()
159	b.ResetTimer()
160	benchValidateVersion("1.x", "2.4.0", b)
161}
162
163func BenchmarkValidateVersionRange(b *testing.B) {
164	b.ReportAllocs()
165	b.ResetTimer()
166	benchValidateVersion(">=2.1.x, <3.1.0", "2.4.5", b)
167}
168
169func BenchmarkValidateVersionRangeFail(b *testing.B) {
170	b.ReportAllocs()
171	b.ResetTimer()
172	benchValidateVersion(">=2.1.x, <3.1.0", "1.4.5", b)
173}
174
175func BenchmarkValidateVersionUnion(b *testing.B) {
176	b.ReportAllocs()
177	b.ResetTimer()
178	benchValidateVersion("~2.0.0 || =3.1.0", "3.1.0", b)
179}
180
181func BenchmarkValidateVersionUnionFail(b *testing.B) {
182	b.ReportAllocs()
183	b.ResetTimer()
184	benchValidateVersion("~2.0.0 || =3.1.0", "3.1.1", b)
185}
186
187/* Version creation benchmarks */
188
189func benchNewVersion(v string, b *testing.B) {
190	for i := 0; i < b.N; i++ {
191		_, _ = NewVersion(v)
192	}
193}
194
195func benchStrictNewVersion(v string, b *testing.B) {
196	for i := 0; i < b.N; i++ {
197		_, _ = StrictNewVersion(v)
198	}
199}
200
201func BenchmarkNewVersionSimple(b *testing.B) {
202	b.ReportAllocs()
203	b.ResetTimer()
204	benchNewVersion("1.0.0", b)
205}
206
207func BenchmarkCoerceNewVersionSimple(b *testing.B) {
208	b.ReportAllocs()
209	b.ResetTimer()
210	benchStrictNewVersion("1.0.0", b)
211}
212
213func BenchmarkNewVersionPre(b *testing.B) {
214	b.ReportAllocs()
215	b.ResetTimer()
216	benchNewVersion("1.0.0-alpha", b)
217}
218
219func BenchmarkStrictNewVersionPre(b *testing.B) {
220	b.ReportAllocs()
221	b.ResetTimer()
222	benchStrictNewVersion("1.0.0-alpha", b)
223}
224
225func BenchmarkNewVersionMeta(b *testing.B) {
226	b.ReportAllocs()
227	b.ResetTimer()
228	benchNewVersion("1.0.0+metadata", b)
229}
230
231func BenchmarkStrictNewVersionMeta(b *testing.B) {
232	b.ReportAllocs()
233	b.ResetTimer()
234	benchStrictNewVersion("1.0.0+metadata", b)
235}
236
237func BenchmarkNewVersionMetaDash(b *testing.B) {
238	b.ReportAllocs()
239	b.ResetTimer()
240	benchNewVersion("1.0.0-alpha.1+meta.data", b)
241}
242
243func BenchmarkStrictNewVersionMetaDash(b *testing.B) {
244	b.ReportAllocs()
245	b.ResetTimer()
246	benchStrictNewVersion("1.0.0-alpha.1+meta.data", b)
247}
248