Skip to content

Commit 69a2e9c

Browse files
authored
Merge pull request #15337 from lorentey/wordcount-is-flaky
[benchmark] WordCount: Initialize global variables in setUpFunction.
2 parents 616f067 + 77fa298 commit 69a2e9c

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

benchmark/single-source/WordCount.swift

+31-11
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,39 @@ public let WordCount = [
2626
BenchmarkInfo(
2727
name: "WordSplitASCII",
2828
runFunction: run_WordSplitASCII,
29-
tags: [.validation, .api, .String, .algorithm]),
29+
tags: [.validation, .api, .String, .algorithm],
30+
setUpFunction: { buildWorkload() }
31+
),
3032
BenchmarkInfo(
3133
name: "WordSplitUTF16",
3234
runFunction: run_WordSplitUTF16,
33-
tags: [.validation, .api, .String, .algorithm]),
35+
tags: [.validation, .api, .String, .algorithm],
36+
setUpFunction: { buildWorkload() }
37+
),
3438
BenchmarkInfo(
3539
name: "WordCountUniqueASCII",
3640
runFunction: run_WordCountUniqueASCII,
37-
tags: [.validation, .api, .String, .Dictionary, .algorithm]),
41+
tags: [.validation, .api, .String, .Dictionary, .algorithm],
42+
setUpFunction: { buildWorkload() }
43+
),
3844
BenchmarkInfo(
3945
name: "WordCountUniqueUTF16",
4046
runFunction: run_WordCountUniqueUTF16,
41-
tags: [.validation, .api, .String, .Dictionary, .algorithm]),
47+
tags: [.validation, .api, .String, .Dictionary, .algorithm],
48+
setUpFunction: { buildWorkload() }
49+
),
4250
BenchmarkInfo(
4351
name: "WordCountHistogramASCII",
4452
runFunction: run_WordCountHistogramASCII,
45-
tags: [.validation, .api, .String, .Dictionary, .algorithm]),
53+
tags: [.validation, .api, .String, .Dictionary, .algorithm],
54+
setUpFunction: { buildWorkload() }
55+
),
4656
BenchmarkInfo(
4757
name: "WordCountHistogramUTF16",
4858
runFunction: run_WordCountHistogramUTF16,
49-
tags: [.validation, .api, .String, .Dictionary, .algorithm]),
50-
59+
tags: [.validation, .api, .String, .Dictionary, .algorithm],
60+
setUpFunction: { buildWorkload() }
61+
),
5162
]
5263

5364
let asciiText = """
@@ -129,6 +140,13 @@ source code and over 20 GB of disk space for thé build artifacts. A clean build
129140
can take multiple hours, but incremental builds will finish much faster.
130141
"""
131142

143+
@inline(never)
144+
func buildWorkload() {
145+
blackHole(someAlphanumerics)
146+
blackHole(asciiWords)
147+
blackHole(utf16Words)
148+
}
149+
132150
// A partial set of Unicode alphanumeric characters. (ASCII letters with at most
133151
// one diacritic (of a limited selection), plus ASCII digits.)
134152
let someAlphanumerics: Set<Character> = {
@@ -184,16 +202,18 @@ struct Words: IteratorProtocol, Sequence {
184202
@inline(never)
185203
public func run_WordSplitASCII(_ N: Int) {
186204
for _ in 1...10*N {
187-
let count = Array(Words(identity(asciiText))).count
188-
CheckResults(count == 280)
205+
let words = Array(Words(identity(asciiText)))
206+
CheckResults(words.count == 280)
207+
blackHole(words)
189208
}
190209
}
191210

192211
@inline(never)
193212
public func run_WordSplitUTF16(_ N: Int) {
194213
for _ in 1...10*N {
195-
let count = Array(Words(identity(utf16Text))).count
196-
CheckResults(count == 280)
214+
let words = Array(Words(identity(utf16Text)))
215+
CheckResults(words.count == 280)
216+
blackHole(words)
197217
}
198218
}
199219

0 commit comments

Comments
 (0)