-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark
executable file
·200 lines (154 loc) · 6.17 KB
/
benchmark
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
#!/usr/bin/env python
import sys
import os
import timeit
import shutil
# Benchmark Parameters
methodsPerExtension = 5
extensionsPerFile = 4
buildAttemptsToAverage = 2
libraryCountsToTest = [0,1,2,4,8]
# All type counts should be evenenly divisible by each library count
typeCountsToTest = [
128,
256,
1024,
2048,
4096,
]
# Run benchmarks
buildCommand = "xcodebuild -project Test.xcodeproj -scheme Test"
owd = os.getcwd()
def writeFile(name, libName, withExtensions, extraMethod):
f = open(f"test-package/Sources/{libName}/{name}.swift", "w")
f.write(f"public class {name} {{\n")
f.write(f" public init() {{}}\n")
f.write(f" public func main() {{\n")
for i in range(extensionsPerFile * methodsPerExtension):
f.write(f" method{i + 1}()\n")
if extraMethod:
f.write(f" extraMethod()\n")
f.write(f" }}\n")
for i in range(extensionsPerFile):
if withExtensions:
f.write(f"}}\n")
f.write(f"extension {name} {{\n")
for j in range(methodsPerExtension):
methodNumber = (i * methodsPerExtension) + j + 1
f.write("\n")
f.write(f" func method{methodNumber}() {{\n")
f.write(f" for i in 0 ..< {methodNumber} {{ print(i + 2) }}; print(\"finished\")\n")
f.write(f" }}\n")
if extraMethod:
f.write("\n")
f.write(f" func extraMethod() {{\n")
f.write(f" for i in 0 ..< {methodNumber} {{ print(i + 2) }}; print(\"finished\")\n")
f.write(f" }}\n")
f.write(f"}}\n")
f.close()
def generatePackage(libCount):
if os.path.exists("test-package"):
shutil.rmtree("test-package")
os.mkdir("test-package")
package = open("test-package/Package.swift", "w")
package.write(
"""// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Test",
dependencies: [],
targets: [
"""
)
for i in range(libCount):
package.write(f" .target(name: \"Lib{i + 1}\", dependencies: []),\n")
package.write(f" .target(name: \"Test\", dependencies: [\n")
for i in range(libCount):
package.write(f" \"Lib{i + 1}\",\n")
package.write("""
]),
]
)
""")
def makeChange(typeCount, libCount, withExtensions):
path = ""
if libCount == 0:
writeFile("Class1", "Test", withExtensions, True)
else:
writeFile("Class1", "Lib1", withExtensions, True)
def generate(typeCount, libCount, withExtensions):
generatePackage(libCount)
os.mkdir("test-package/Sources")
os.mkdir("test-package/Sources/Test")
main = open(f"test-package/Sources/Test/main.swift", "w")
if libCount == 0:
for t in range(typeCount):
writeFile(f"Class{t + 1}", f"Test", withExtensions, False)
main.write(f"Class{t + 1}().main()\n")
else:
for l in range(libCount):
os.mkdir(f"test-package/Sources/Lib{l + 1}")
main.write(f"import Lib{l + 1}\n")
typesPerLib = int(typeCount / libCount)
for l in range(libCount):
for t in range(typesPerLib):
typeNum = l * typesPerLib + t + 1
writeFile(f"Class{typeNum}", f"Lib{l + 1}", withExtensions, False)
main.write(f"Class{typeNum}().main()\n")
main.close()
def timeRebuild(typeCount, libCount, withExtensions):
print("Building after adding method")
makeChange(typeCount, libCount, withExtensions)
os.chdir("test-package")
start = timeit.default_timer()
os.system(f"{buildCommand} >/dev/null")
end = timeit.default_timer()
os.chdir(owd)
print(end - start)
print("-----------------------------------------")
return end - start
def time(attempt, typeCount, libCount, withExtensions):
extensionStrategy = "using extensions" if withExtensions else "without extensions"
print(f"Build Attempt {attempt}: {typeCount} types in {libCount} libs {extensionStrategy}...")
print("-----------------------------------------")
generate(typeCount, libCount, withExtensions)
os.chdir("test-package")
os.system("swift package generate-xcodeproj >/dev/null")
os.system(f"{buildCommand} clean >/dev/null")
start = timeit.default_timer()
os.system(f"{buildCommand} >/dev/null")
end = timeit.default_timer()
os.chdir(owd)
print(end - start)
return end - start
def benchmark(typeCount):
print("\n\n")
print(f"Benchmarking {typeCount} types")
print("=============================================")
withExtensionsSums = {}
withoutExtensionsSums = {}
withExtensionsRebuildSums = {}
withoutExtensionsRebuildSums = {}
for libCount in libraryCountsToTest:
withExtensionsSums[libCount] = 0
withoutExtensionsSums[libCount] = 0
withExtensionsRebuildSums[libCount] = 0
withoutExtensionsRebuildSums[libCount] = 0
for attempt in range(buildAttemptsToAverage):
for libCount in libraryCountsToTest:
withoutExtensionsSums[libCount] += time(attempt + 1, typeCount, libCount, False)
withoutExtensionsRebuildSums[libCount] += timeRebuild(typeCount, libCount, False)
withExtensionsSums[libCount] += time(attempt + 1, typeCount, libCount, True)
withExtensionsRebuildSums[libCount] += timeRebuild(typeCount, libCount, True)
print(f"Averages for {typeCount} types")
for libCount in libraryCountsToTest:
print(f"Libs: {libCount} Extensions ( ) Rebuild ( ): {withoutExtensionsSums[libCount] / buildAttemptsToAverage}")
print(f"Libs: {libCount} Extensions (x) Rebuild ( ): {withExtensionsSums[libCount] / buildAttemptsToAverage}")
print(f"Libs: {libCount} Extensions ( ) Rebuild (x): {withoutExtensionsRebuildSums[libCount] / buildAttemptsToAverage}")
print(f"Libs: {libCount} Extensions (x) Rebuild (x): {withExtensionsRebuildSums[libCount] / buildAttemptsToAverage}")
print("=============================================")
#generate(5, 4, False)
#makeChange(5, 4, False)
for typeCount in typeCountsToTest:
benchmark(typeCount)