Benchmark.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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: class {
  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 .run(let filter):
  45. guard filter.shouldRun(description) else {
  46. return
  47. }
  48. print(measure(description, benchmark: benchmark, repeats: spec.repeats))
  49. }
  50. }
  51. /// Runs the given benchmark multiple times, recording the wall time for each iteration.
  52. ///
  53. /// - Parameters:
  54. /// - description: A description of the benchmark.
  55. /// - benchmark: The benchmark to run.
  56. /// - repeats: the number of times to run the benchmark.
  57. func measure(_ description: String, benchmark: Benchmark, repeats: Int) -> BenchmarkResults {
  58. var milliseconds: [UInt64] = []
  59. for _ in 0..<repeats {
  60. do {
  61. try benchmark.setUp()
  62. let start = DispatchTime.now().uptimeNanoseconds
  63. try benchmark.run()
  64. let end = DispatchTime.now().uptimeNanoseconds
  65. milliseconds.append((end - start) / 1_000_000)
  66. } catch {
  67. // If tearDown fails now then there's not a lot we can do!
  68. try? benchmark.tearDown()
  69. return BenchmarkResults(desc: description, milliseconds: [])
  70. }
  71. do {
  72. try benchmark.tearDown()
  73. } catch {
  74. return BenchmarkResults(desc: description, milliseconds: [])
  75. }
  76. }
  77. return BenchmarkResults(desc: description, milliseconds: milliseconds)
  78. }