-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseBenchmark.swift
More file actions
96 lines (78 loc) · 2.67 KB
/
BaseBenchmark.swift
File metadata and controls
96 lines (78 loc) · 2.67 KB
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
import Foundation
func getCurrentMillis() -> Int64 {
return Int64(NSDate().timeIntervalSince1970 * 1000)
}
// Preprocessor trick from the internet
func getRandomInt(range: Int) -> Int {
#if os(macOS)
return Int(arc4random_uniform(UInt32(range)))
#elseif os(Linux)
return Int(rand()) % range
#endif
}
func getRandomString(size: Int) -> String {
let chars = Array("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
var result = ""
for _ in 0..<size {
let char = chars[getRandomInt(range: chars.count)]
result += String(char)
}
return result
}
func getRandomStringArray(size: Int, stringSize: Int) -> [String] {
return [String](repeating: "", count: size)
.map{ _ in getRandomString(size: stringSize) }
}
class BaseBenchmark {
let benchmarkName: String,
operationsPerTest: Int
var startTimeMillis: Int64 = 0,
endTimeMillis: Int64 = 0,
results = Dictionary<Int, Array<(Int, Int)>>()
var elapsedMillis: Int {
return Int(endTimeMillis - startTimeMillis)
}
func averageMicrosFromMillis(dataSize: Int, data: [(getTime: Int, setTime: Int)]) -> (Double, Double) {
let getTimes = data.map{ $0.getTime }, // Extract get times
setTimes = data.map{ $0.setTime } // Extract set times
let averageGetMicros = getTimes.map{ (1000 * Double($0)) / Double(dataSize) } // Compute average per test
.reduce(0.0, +) / Double(data.count) // Compute average of all tests
let averageSetMicros = setTimes.map{ (1000 * Double($0)) / Double(dataSize) } // Compute average per test
.reduce(0.0, +) / Double(data.count) // Compute average of all tests
return (averageGetMicros, averageSetMicros)
}
init(benchmarkName: String, operationsPerTest: Int = 1000) {
self.benchmarkName = benchmarkName
self.operationsPerTest = operationsPerTest
}
func startTimer() {
startTimeMillis = getCurrentMillis()
}
func stopTimer() {
endTimeMillis = getCurrentMillis()
}
func runTests(count: Int, dataSizes: [Int]) {
print("Running \(benchmarkName)")
for size in dataSizes {
results[size] = []
for _ in 0..<count {
results[size]!.append(runSingleTest(data: getRandomStringArray(size: size, stringSize: 8)))
}
}
printResults()
}
func printResults() {
// Print data in order of test data size
let sortedResults = results.sorted(by: { $0.key < $1.key })
print("\(benchmarkName) test results:")
for res in sortedResults {
let micros: (getMicros: Double, setMicros: Double) = averageMicrosFromMillis(dataSize: res.key, data: res.value)
print("""
dataSize \(res.key)
get micros \(micros.getMicros)
set micros \(micros.setMicros)
""")
}
}
/* virtual */ func runSingleTest(data: [String]) -> (Int, Int) { return (-1, -1) }
}