Skip to content

Commit 6af0eab

Browse files
committed
[benchmark] Move loop multipliers to BenchmarkInfo
1 parent e4cdc48 commit 6af0eab

File tree

1 file changed

+63
-58
lines changed

1 file changed

+63
-58
lines changed

benchmark/single-source/DataBenchmarks.swift

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,122 +15,127 @@ import Foundation
1515

1616
let d: [BenchmarkCategory] = [.validation, .api, .Data]
1717

18+
/// Workload scaling: Inner loop multipliers
19+
let M = 10_000 /// default
20+
let I = 100_000 /// Data initialization
21+
let S = 200 /// String conversions
22+
1823
public let DataBenchmarks = [
1924
BenchmarkInfo(name: "DataCreateEmpty",
20-
runFunction: { for _ in 0..<$0*100_000 { blackHole(Data()) } }, tags: d),
25+
runFunction: { for _ in 0..<$0*I { blackHole(Data()) } }, tags: d),
2126
BenchmarkInfo(name: "DataCreateSmall",
22-
runFunction: { for _ in 0..<$0*100_000 { blackHole(sampleData(.small)) } },
27+
runFunction: { for _ in 0..<$0*I { blackHole(sampleData(.small)) } },
2328
tags: d),
2429
BenchmarkInfo(name: "DataCreateMedium",
25-
runFunction: { for _ in 0..<$0*10_000 { blackHole(sampleData(.medium)) } },
30+
runFunction: { for _ in 0..<$0*M { blackHole(sampleData(.medium)) } },
2631
tags: d),
2732

2833
BenchmarkInfo(name: "DataCreateEmptyArray",
29-
runFunction: { for _ in 0..<$0*100_000 { blackHole(Data([])) } }, tags: d),
34+
runFunction: { for _ in 0..<$0*I { blackHole(Data([])) } }, tags: d),
3035
BenchmarkInfo(name: "DataCreateSmallArray",
31-
runFunction: { for _ in 0..<$0*100_000 { blackHole(Data(
36+
runFunction: { for _ in 0..<$0*I { blackHole(Data(
3237
[0, 1, 2, 3, 4, 5, 6])) } }, tags: d),
3338
BenchmarkInfo(name: "DataCreateMediumArray",
34-
runFunction: { for _ in 0..<$0*10_000 { blackHole(Data([
39+
runFunction: { for _ in 0..<$0*M { blackHole(Data([
3540
0, 1, 2, 3, 4, 5, 6,
3641
0, 1, 2, 3, 4, 5, 6,
3742
0, 1, 2, 3, 4, 5, 6,
3843
0, 1, 2, 3, 4, 5, 6,
3944
])) } }, tags: d),
4045

4146
BenchmarkInfo(name: "DataSubscriptSmall",
42-
runFunction: { for _ in 0..<$0*10_000 { blackHole(small[1]) } }, tags: d),
47+
runFunction: { for _ in 0..<$0*M { blackHole(small[1]) } }, tags: d),
4348
BenchmarkInfo(name: "DataSubscriptMedium",
44-
runFunction: { for _ in 0..<$0*10_000 { blackHole(medium[521]) } }, tags: d),
49+
runFunction: { for _ in 0..<$0*M { blackHole(medium[521]) } }, tags: d),
4550

4651
BenchmarkInfo(name: "DataCountSmall",
47-
runFunction: { count($0, data: small) }, tags: d),
52+
runFunction: { count($0*M, data: small) }, tags: d),
4853
BenchmarkInfo(name: "DataCountMedium",
49-
runFunction: { count($0, data: medium) }, tags: d),
54+
runFunction: { count($0*M, data: medium) }, tags: d),
5055

5156
BenchmarkInfo(name: "DataSetCountSmall",
52-
runFunction: { setCount($0, data: small, extra: 3) }, tags: d),
57+
runFunction: { setCount($0*M, data: small, extra: 3) }, tags: d),
5358
BenchmarkInfo(name: "DataSetCountMedium",
54-
runFunction: { setCount($0, data: medium, extra: 100) }, tags: d),
59+
runFunction: { setCount($0*M, data: medium, extra: 100) }, tags: d),
5560

5661
BenchmarkInfo(name: "DataAccessBytesSmall",
57-
runFunction: { withUnsafeBytes($0, data: small) }, tags: d),
62+
runFunction: { withUnsafeBytes($0*M, data: small) }, tags: d),
5863
BenchmarkInfo(name: "DataAccessBytesMedium",
59-
runFunction: { withUnsafeBytes($0, data: medium) }, tags: d),
64+
runFunction: { withUnsafeBytes($0*M, data: medium) }, tags: d),
6065

6166
BenchmarkInfo(name: "DataMutateBytesSmall",
62-
runFunction: { withUnsafeMutableBytes($0, data: small) }, tags: d),
67+
runFunction: { withUnsafeMutableBytes($0*M, data: small) }, tags: d),
6368
BenchmarkInfo(name: "DataMutateBytesMedium",
64-
runFunction: { withUnsafeMutableBytes($0, data: medium) }, tags: d),
69+
runFunction: { withUnsafeMutableBytes($0*M, data: medium) }, tags: d),
6570

6671
BenchmarkInfo(name: "DataCopyBytesSmall",
67-
runFunction: { copyBytes($0, data: small) }, tags: d),
72+
runFunction: { copyBytes($0*M, data: small) }, tags: d),
6873
BenchmarkInfo(name: "DataCopyBytesMedium",
69-
runFunction: { copyBytes($0, data: medium) }, tags: d),
74+
runFunction: { copyBytes($0*M, data: medium) }, tags: d),
7075

7176
BenchmarkInfo(name: "DataAppendBytesSmall",
72-
runFunction: { append($0, bytes: 3, to: small) }, tags: d),
77+
runFunction: { append($0*M, bytes: 3, to: small) }, tags: d),
7378
BenchmarkInfo(name: "DataAppendBytesMedium",
74-
runFunction: { append($0, bytes: 809, to: medium) }, tags: d),
79+
runFunction: { append($0*M, bytes: 809, to: medium) }, tags: d),
7580

7681
BenchmarkInfo(name: "DataAppendArray",
77-
runFunction: { append($0, arraySize: 809, to: medium) }, tags: d),
82+
runFunction: { append($0*M, arraySize: 809, to: medium) }, tags: d),
7883

7984
BenchmarkInfo(name: "DataReset",
80-
runFunction: { resetBytes($0, in: 431..<809, data: medium) }, tags: d),
85+
runFunction: { resetBytes($0*M, in: 431..<809, data: medium) }, tags: d),
8186

8287
BenchmarkInfo(name: "DataReplaceSmall", runFunction: {
83-
replace($0, data: medium, subrange:431..<809, with: small) }, tags: d),
88+
replace($0*M, data: medium, subrange:431..<809, with: small) }, tags: d),
8489
BenchmarkInfo(name: "DataReplaceMedium", runFunction: {
85-
replace($0, data: medium, subrange:431..<809, with: medium) }, tags: d),
90+
replace($0*M, data: medium, subrange:431..<809, with: medium) }, tags: d),
8691
BenchmarkInfo(name: "DataReplaceLarge", runFunction: {
87-
replace($0, data: medium, subrange:431..<809, with: large) }, tags: d),
92+
replace($0*M, data: medium, subrange:431..<809, with: large) }, tags: d),
8893

8994
BenchmarkInfo(name: "DataReplaceSmallBuffer", runFunction: {
90-
replaceBuffer($0, data: medium, subrange:431..<809, with: small) },
95+
replaceBuffer($0*M, data: medium, subrange:431..<809, with: small) },
9196
tags: d),
9297
BenchmarkInfo(name: "DataReplaceMediumBuffer", runFunction: {
93-
replaceBuffer($0, data: medium, subrange:431..<809, with: medium) },
98+
replaceBuffer($0*M, data: medium, subrange:431..<809, with: medium) },
9499
tags: d),
95100
BenchmarkInfo(name: "DataReplaceLargeBuffer", runFunction: {
96-
replaceBuffer($0, data: medium, subrange:431..<809, with: large) },
101+
replaceBuffer($0*M, data: medium, subrange:431..<809, with: large) },
97102
tags: d),
98103

99104
BenchmarkInfo(name: "DataAppendSequence",
100-
runFunction: { append($0, sequenceLength: 809, to: medium) }, tags: d),
105+
runFunction: { append($0*M, sequenceLength: 809, to: medium) }, tags: d),
101106

102107
BenchmarkInfo(name: "DataAppendDataSmallToSmall",
103-
runFunction: { append($0, data: small, to: small) }, tags: d),
108+
runFunction: { append($0*M, data: small, to: small) }, tags: d),
104109
BenchmarkInfo(name: "DataAppendDataSmallToMedium",
105-
runFunction: { append($0, data: small, to: medium) }, tags: d),
110+
runFunction: { append($0*M, data: small, to: medium) }, tags: d),
106111
BenchmarkInfo(name: "DataAppendDataSmallToLarge",
107-
runFunction: { append($0, data: small, to: large) }, tags: d),
112+
runFunction: { append($0*M, data: small, to: large) }, tags: d),
108113
BenchmarkInfo(name: "DataAppendDataMediumToSmall",
109-
runFunction: { append($0, data: medium, to: small) }, tags: d),
114+
runFunction: { append($0*M, data: medium, to: small) }, tags: d),
110115
BenchmarkInfo(name: "DataAppendDataMediumToMedium",
111-
runFunction: { append($0, data: medium, to: medium) }, tags: d),
116+
runFunction: { append($0*M, data: medium, to: medium) }, tags: d),
112117
BenchmarkInfo(name: "DataAppendDataMediumToLarge",
113-
runFunction: { append($0, data: medium, to: large) }, tags: d),
118+
runFunction: { append($0*M, data: medium, to: large) }, tags: d),
114119
BenchmarkInfo(name: "DataAppendDataLargeToSmall",
115-
runFunction: { append($0, data: large, to: small) }, tags: d),
120+
runFunction: { append($0*M, data: large, to: small) }, tags: d),
116121
BenchmarkInfo(name: "DataAppendDataLargeToMedium",
117-
runFunction: { append($0, data: large, to: medium) }, tags: d),
122+
runFunction: { append($0*M, data: large, to: medium) }, tags: d),
118123
BenchmarkInfo(name: "DataAppendDataLargeToLarge",
119-
runFunction: { append($0, data: large, to: large) }, tags: d),
124+
runFunction: { append($0*M, data: large, to: large) }, tags: d),
120125

121126
BenchmarkInfo(name: "DataToStringEmpty",
122-
runFunction: { string($0, from: emptyData) }, tags: d, legacyFactor: 50),
127+
runFunction: { string($0*S, from: emptyData) }, tags: d, legacyFactor: 50),
123128
BenchmarkInfo(name: "DataToStringSmall",
124-
runFunction: { string($0, from: smallData) }, tags: d, legacyFactor: 50),
129+
runFunction: { string($0*S, from: smallData) }, tags: d, legacyFactor: 50),
125130
BenchmarkInfo(name: "DataToStringMedium",
126-
runFunction: { string($0, from: mediumData) }, tags: d, legacyFactor: 50),
131+
runFunction: { string($0*S, from: mediumData) }, tags: d, legacyFactor: 50),
127132

128133
BenchmarkInfo(name: "StringToDataEmpty",
129-
runFunction: { data($0, from: emptyString) }, tags: d, legacyFactor: 50),
134+
runFunction: { data($0*S, from: emptyString) }, tags: d, legacyFactor: 50),
130135
BenchmarkInfo(name: "StringToDataSmall",
131-
runFunction: { data($0, from: smallString) }, tags: d, legacyFactor: 50),
136+
runFunction: { data($0*S, from: smallString) }, tags: d, legacyFactor: 50),
132137
BenchmarkInfo(name: "StringToDataMedium",
133-
runFunction: { data($0, from: mediumString) }, tags: d, legacyFactor: 50),
138+
runFunction: { data($0*S, from: mediumString) }, tags: d, legacyFactor: 50),
134139
]
135140

136141
let emptyString = ""
@@ -191,7 +196,7 @@ func sampleData(_ type: SampleKind) -> Data {
191196

192197
@inline(never)
193198
func withUnsafeBytes(_ N: Int, data: Data) {
194-
for _ in 0..<10000*N {
199+
for _ in 1...N {
195200
data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) in
196201
blackHole(ptr.pointee)
197202
}
@@ -200,7 +205,7 @@ func withUnsafeBytes(_ N: Int, data: Data) {
200205

201206
@inline(never)
202207
func withUnsafeMutableBytes(_ N: Int, data: Data) {
203-
for _ in 0..<10000*N {
208+
for _ in 1...N {
204209
var copy = data
205210
copy.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
206211
// Mutate a byte
@@ -214,7 +219,7 @@ func copyBytes(_ N: Int, data: Data) {
214219
let amount = data.count
215220
var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: amount)
216221
defer { buffer.deallocate() }
217-
for _ in 0..<10000*N {
222+
for _ in 1...N {
218223
data.copyBytes(to: buffer, from: 0..<amount)
219224
}
220225
}
@@ -223,7 +228,7 @@ func copyBytes(_ N: Int, data: Data) {
223228
func append(_ N: Int, bytes count: Int, to data: Data) {
224229
let bytes = malloc(count).assumingMemoryBound(to: UInt8.self)
225230
defer { free(bytes) }
226-
for _ in 0..<10000*N {
231+
for _ in 1...N {
227232
var copy = data
228233
copy.append(bytes, count: count)
229234
}
@@ -235,7 +240,7 @@ func append(_ N: Int, arraySize: Int, to data: Data) {
235240
bytes.withUnsafeMutableBufferPointer {
236241
fillBuffer($0)
237242
}
238-
for _ in 0..<10000*N {
243+
for _ in 1...N {
239244
var copy = data
240245
copy.append(contentsOf: bytes)
241246
}
@@ -244,15 +249,15 @@ func append(_ N: Int, arraySize: Int, to data: Data) {
244249
@inline(never)
245250
func append(_ N: Int, sequenceLength: Int, to data: Data) {
246251
let bytes = repeatElement(UInt8(0xA0), count: sequenceLength)
247-
for _ in 0..<10000*N {
252+
for _ in 1...N {
248253
var copy = data
249254
copy.append(contentsOf: bytes)
250255
}
251256
}
252257

253258
@inline(never)
254259
func resetBytes(_ N: Int, in range: Range<Data.Index>, data: Data) {
255-
for _ in 0..<10000*N {
260+
for _ in 1...N {
256261
var copy = data
257262
copy.resetBytes(in: range)
258263
}
@@ -265,7 +270,7 @@ func replace(
265270
subrange range: Range<Data.Index>,
266271
with replacement: Data
267272
) {
268-
for _ in 0..<10000*N {
273+
for _ in 1...N {
269274
var copy = data
270275
copy.replaceSubrange(range, with: replacement)
271276
}
@@ -281,7 +286,7 @@ func replaceBuffer(
281286
replacement.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in
282287
let buffer = UnsafeBufferPointer(start: bytes, count: replacement.count)
283288

284-
for _ in 0..<10000*N {
289+
for _ in 1...N {
285290
var copy = data
286291
copy.replaceSubrange(range, with: buffer)
287292
}
@@ -291,15 +296,15 @@ func replaceBuffer(
291296
@inline(never)
292297
func append(_ N: Int, data: Data, to target: Data) {
293298
var copy: Data
294-
for _ in 0..<10000*N {
299+
for _ in 1...N {
295300
copy = target
296301
copy.append(data)
297302
}
298303
}
299304

300305
@inline(never)
301306
public func count(_ N: Int, data: Data) {
302-
for _ in 0..<10000*N {
307+
for _ in 1...N {
303308
blackHole(data.count)
304309
}
305310
}
@@ -309,22 +314,22 @@ public func setCount(_ N: Int, data: Data, extra: Int) {
309314
var copy = data
310315
let count = data.count + extra
311316
let orig = data.count
312-
for _ in 0..<10000*N {
317+
for _ in 1...N {
313318
copy.count = count
314319
copy.count = orig
315320
}
316321
}
317322

318323
@inline(never)
319324
public func string(_ N: Int, from data: Data) {
320-
for _ in 0..<200 * N {
325+
for _ in 1...N {
321326
blackHole(String(decoding: data, as: UTF8.self))
322327
}
323328
}
324329

325330
@inline(never)
326331
public func data(_ N: Int, from string: String) {
327-
for _ in 0..<200 * N {
332+
for _ in 1...N {
328333
blackHole(Data(string.utf8))
329334
}
330335
}

0 commit comments

Comments
 (0)