forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswift-bench.py
378 lines (335 loc) · 11.7 KB
/
swift-bench.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python
##===--- swift-bench.py -------------------------------*- coding: utf-8 -*-===##
##
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See http://swift.org/LICENSE.txt for license information
## See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
##
##===----------------------------------------------------------------------===##
# This file implements a test harness for running Swift performance benchmarks.
#
# Its input is a set of swift files, containing functions named 'bench_*' that
# take no arguments and returns Int. The harness makes a separate test from
# each of these functions, runs all the tests and reports aggregate results.
#
# The workflow of the harness is the following:
# o Basing on the input files, generate 'processed' files. These files contain
# a main function with simple arguments parsing, time measurement utilities
# and a loop in which the bench-functions are called.
# o When all files are processed, the harness begins to compile them, keeping
# track of all compile fails for later results reporting.
# o When all files are compiled, the harness begins to run the tests. The
# harness chooses a number of iterations for each tests to achieve the best
# accuracy in the given time limit (in order to do that, it performs several
# auxiliary test runs). When the iteration number is chosen, the measurent
# of execution time is actually performed.
# o At this point everything is ready, and the harness simply reports the
# results.
#
# Ideas for the harness improvement and development are welcomed here:
# rdar://problem/18072938
import subprocess
import numpy
import time
import re
import os
import sys
import argparse
class SwiftBenchHarness:
sources = []
verboseLevel = 0
compiler = ""
tests = {}
timeLimit = 1000
minSampleTime = 100
minIterTime = 1
optFlags = []
def log(self, str, level):
if self.verboseLevel >= level:
for i in range(1,level):
sys.stdout.write(' ')
print(str)
def runCommand(self, cmd):
self.log(' Executing: ' + ' '.join(cmd), 1)
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
def parseArguments(self):
self.log("Parsing arguments.", 2)
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbosity", help="increase output verbosity", type=int)
parser.add_argument("files", help="input files", nargs='+')
parser.add_argument('-c', '--compiler', help="compiler to use")
parser.add_argument('-t', '--timelimit', help="Time limit for every test", type=int)
parser.add_argument('-s', '--sampletime', help="Minimum time for every sample", type=int)
parser.add_argument('-f', '--flags', help="Compilation flags", nargs='+')
args = parser.parse_args()
if args.verbosity:
self.verboseLevel = args.verbosity
self.sources = args.files
if args.flags:
self.optFlags = args.flags
if args.compiler:
self.compiler = args.compiler
else:
self.compiler = 'swiftc'
if args.timelimit and args.timelimit > 0:
self.timeLimit = args.timelimit
if args.sampletime and args.sampletime > 0:
self.minSampleTime = args.sampletime
self.log("Sources: %s." % ', '.join(self.sources), 3)
self.log("Compiler: %s." % self.compiler, 3)
self.log("Opt flags: %s." % ', '.join(self.optFlags), 3)
self.log("Verbosity: %s." % self.verboseLevel, 3)
self.log("Time limit: %s." % self.timeLimit, 3)
self.log("Min sample time: %s." % self.minSampleTime, 3)
def processSource(self, name):
self.log("Processing source file: %s." % name, 2)
header = """
@_silgen_name("mach_absolute_time") func __mach_absolute_time__() -> UInt64
@_silgen_name("opaqueGetInt32")
func _opaqueGetInt32(x: Int) -> Int
@_silgen_name("opaqueGetInt64")
func _opaqueGetInt64(x: Int) -> Int
@inline(never)
public func getInt(x: Int) -> Int {
#if arch(i386) || arch(arm)
return _opaqueGetInt32(x)
#elseif arch(x86_64) || arch(arm64)
return _opaqueGetInt64(x)
#else
return x
#endif
}
@inline(never)
func False() -> Bool { return getInt(1) == 0 }
@inline(never)
func Consume(x: Int) { if False() { println(x) } }
"""
beforeBench = """
@inline(never)
"""
intoBench = """
if False() { return 0 }
"""
mainBegin = """
func main() {
var N = 1
var name = ""
if Process.arguments.count > 1 {
N = Process.arguments[1].toInt()!
}
"""
mainBody = """
name = "%s"
if Process.arguments.count <= 2 || Process.arguments[2] == name {
let start = __mach_absolute_time__()
for _ in 1...N {
bench_%s()
}
let end = __mach_absolute_time__()
println("\(name),\(N),\(end - start)")
}
"""
mainEnd = """
}
main()
"""
benchRE = re.compile("^\s*func\s\s*bench_([a-zA-Z0-9_]+)\s*\(\s*\)\s*->\s*Int\s*({)?\s*$")
f = open(name)
lines = f.readlines()
f.close()
output = header
lookingForCurlyBrace = False
testNames = []
for l in lines:
if lookingForCurlyBrace:
output += l
if "{" not in l:
continue
lookingForCurlyBrace = False
output += intoBench
continue
m = benchRE.match(l)
if m:
output += beforeBench
output += l
benchName = m.group(1)
# TODO: Keep track of the line number as well
self.log("Benchmark found: %s" % benchName, 3)
self.tests[name+":"+benchName] = Test(benchName, name, "", "")
testNames.append(benchName)
if m.group(2):
output += intoBench
else:
lookingForCurlyBrace = True
else:
output += l
output += mainBegin
for n in testNames:
output += mainBody % (n, n)
processedName = 'processed_' + os.path.basename(name)
output += mainEnd
f = open(processedName, 'w')
f.write(output)
f.close()
for n in testNames:
self.tests[name+":"+n].processedSource = processedName
def processSources(self):
self.log("Processing sources: %s." % self.sources, 2)
for s in self.sources:
self.processSource(s)
def compileOpaqueCFile(self):
self.log("Generating and compiling C file with opaque functions.", 3)
fileBody = """
#include <stdint.h>
extern "C" int32_t opaqueGetInt32(int32_t x) { return x; }
extern "C" int64_t opaqueGetInt64(int64_t x) { return x; }
"""
f = open('opaque.cpp', 'w')
f.write(fileBody)
f.close()
# TODO: Handle subprocess.CalledProcessError for this call:
self.runCommand(['clang++', 'opaque.cpp', '-o', 'opaque.o', '-c', '-O2'])
compiledFiles = {}
def compileSource(self, name):
self.tests[name].binary = "./"+self.tests[name].processedSource.split(os.extsep)[0]
if not self.tests[name].processedSource in self.compiledFiles:
try:
self.runCommand([self.compiler, self.tests[name].processedSource, "-o", self.tests[name].binary + '.o', '-c'] + self.optFlags)
self.runCommand([self.compiler, '-o', self.tests[name].binary, self.tests[name].binary + '.o', 'opaque.o'])
self.compiledFiles[self.tests[name].processedSource] = ('', '')
except subprocess.CalledProcessError as e:
self.compiledFiles[self.tests[name].processedSource] = ('COMPFAIL', e.output)
(status, output) = self.compiledFiles[self.tests[name].processedSource]
self.tests[name].status = status
self.tests[name].output = output
def compileSources(self):
self.log("Compiling processed sources.", 2)
self.compileOpaqueCFile()
for t in self.tests:
self.compileSource(t)
def runBenchmarks(self):
self.log("Running benchmarks.", 2)
for t in self.tests:
self.runBench(t)
def parseBenchmarkOutput(self, res):
# Parse lines like
# TestName,NNN,MMM
# where NNN - performed iterations number, MMM - execution time (in ns)
RESULTS_RE = re.compile(r"(\w+),[ \t]*(\d+),[ \t]*(\d+)")
m = RESULTS_RE.match(res)
if not m:
return ("", 0, 0)
return (m.group(1), m.group(2), m.group(3))
def computeItersNumber(self, name):
scale = 1
spent = 0
# Mesaure time for one iteration
# If it's too small, increase number of iteration until it's measurable
while (spent <= self.minIterTime):
try:
r = self.runCommand([self.tests[name].binary, str(scale),
self.tests[name].name])
(testName, itersComputed, execTime) = self.parseBenchmarkOutput(r)
spent = int(execTime) / 1000000 # Convert ns to ms
if spent <= self.minIterTime:
scale *= 2
if scale > sys.maxint:
return (0, 0)
except subprocess.CalledProcessError as e:
r = e.output
break
if spent == 0:
spent = 1
# Now compute number of samples we can take in the given time limit
mult = int(self.minSampleTime / spent)
if mult == 0:
mult = 1
scale *= mult
spent *= mult
samples = int(self.timeLimit / spent)
if samples == 0:
samples = 1
return (samples, scale)
def runBench(self, name):
if not self.tests[name].status == "":
return
(numSamples, iterScale) = self.computeItersNumber(name)
if (numSamples, iterScale) == (0, 0):
self.tests[name].status = "CAN'T MEASURE"
self.tests[name].output = "Can't find number of iterations for the test to last longer than %d ms." % self.minIterTime
return
samples = []
self.log("Running bench: %s, numsamples: %d" % (name, numSamples), 2)
output = ""
for i in range(0,numSamples):
try:
r = self.runCommand([self.tests[name].binary, str(iterScale),
self.tests[name].name])
(testName, itersComputed, execTime) = self.parseBenchmarkOutput(r)
# TODO: Verify testName and itersComputed
samples.append(int(execTime) / iterScale)
self.tests[name].output = r
except subprocess.CalledProcessError as e:
self.tests[name].status = "RUNFAIL"
self.tests[name].output = e.output
break
res = TestResults(name, samples)
self.tests[name].results = res
def reportResults(self):
self.log("\nReporting results.", 2)
print("==================================================")
for t in self.tests:
self.tests[t].Print()
class Test:
def __init__(self, name, source, processedSource, binary):
self.name = name
self.source = source
self.processedSource = processedSource
self.binary = binary
self.status = ""
def Print(self):
print("NAME: %s" % self.name)
print("SOURCE: %s" % self.source)
if self.status == "":
self.results.Print()
else:
print("STATUS: %s" % self.status)
print("OUTPUT:")
print(self.output)
print("END OF OUTPUT")
print("")
class TestResults:
def __init__(self, name, samples):
self.name = name
self.samples = samples
if len(samples) > 0:
self.Process()
def Process(self):
self.minimum = min(self.samples)
self.maximum = max(self.samples)
self.avg = sum(self.samples)/len(self.samples)
self.std = numpy.std(self.samples)
self.err = self.std/numpy.sqrt(len(self.samples))
self.int_min = self.avg - self.err*1.96
self.int_max = self.avg + self.err*1.96
def Print(self):
print("SAMPLES: %d" % len(self.samples))
print("MIN: %3.2e" % self.minimum)
print("MAX: %3.2e" % self.maximum)
print("AVG: %3.2e" % self.avg)
print("STD: %3.2e" % self.std)
print("ERR: %3.2e (%2.1f%%)" % (self.err, self.err*100/self.avg))
print("CONF INT 0.95: (%3.2e, %3.2e)" % (self.int_min, self.int_max))
print("")
def main():
harness = SwiftBenchHarness()
harness.parseArguments()
harness.processSources()
harness.compileSources()
harness.runBenchmarks()
harness.reportResults()
main()