2
0

Benchmark.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Dispatch
  17. protocol Benchmark: AnyObject {
  18. func setUp() throws
  19. func tearDown() throws
  20. func run() throws
  21. }
  22. /// The results of a benchmark.
  23. struct BenchmarkResults {
  24. /// The description of the benchmark.
  25. var desc: String
  26. /// The duration of each run of the benchmark in milliseconds.
  27. var milliseconds: [UInt64]
  28. }
  29. extension BenchmarkResults: CustomStringConvertible {
  30. var description: String {
  31. return "\(self.desc): \(self.milliseconds.map(String.init).joined(separator: ","))"
  32. }
  33. }
  34. /// Runs the benchmark and prints the duration in milliseconds for each run.
  35. ///
  36. /// - Parameters:
  37. /// - description: A description of the benchmark.
  38. /// - benchmark: The benchmark which should be run.
  39. /// - spec: The specification for the test run.
  40. func measureAndPrint(description: String, benchmark: Benchmark, spec: TestSpec) {
  41. switch spec.action {
  42. case .list:
  43. print(description)
  44. case let .run(filter):
  45. guard filter.shouldRun(description) else {
  46. return
  47. }
  48. #if CACHEGRIND
  49. _ = measure(description, benchmark: benchmark, repeats: 1)
  50. #else
  51. print(measure(description, benchmark: benchmark, repeats: spec.repeats))
  52. #endif
  53. }
  54. }
  55. /// Runs the given benchmark multiple times, recording the wall time for each iteration.
  56. ///
  57. /// - Parameters:
  58. /// - description: A description of the benchmark.
  59. /// - benchmark: The benchmark to run.
  60. /// - repeats: the number of times to run the benchmark.
  61. func measure(_ description: String, benchmark: Benchmark, repeats: Int) -> BenchmarkResults {
  62. var milliseconds: [UInt64] = []
  63. for _ in 0 ..< repeats {
  64. do {
  65. try benchmark.setUp()
  66. #if !CACHEGRIND
  67. let start = DispatchTime.now().uptimeNanoseconds
  68. #endif
  69. try benchmark.run()
  70. #if !CACHEGRIND
  71. let end = DispatchTime.now().uptimeNanoseconds
  72. milliseconds.append((end - start) / 1_000_000)
  73. #endif
  74. } catch {
  75. // If tearDown fails now then there's not a lot we can do!
  76. try? benchmark.tearDown()
  77. return BenchmarkResults(desc: description, milliseconds: [])
  78. }
  79. do {
  80. try benchmark.tearDown()
  81. } catch {
  82. return BenchmarkResults(desc: description, milliseconds: [])
  83. }
  84. }
  85. return BenchmarkResults(desc: description, milliseconds: milliseconds)
  86. }