HistogramTests.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2020, 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. @testable import BenchmarkUtils
  17. import XCTest
  18. class HistogramTests: XCTestCase {
  19. func testStats() {
  20. var histogram = Histogram()
  21. histogram.add(value: 1)
  22. histogram.add(value: 2)
  23. histogram.add(value: 3)
  24. XCTAssertEqual(histogram.countOfValuesSeen, 3)
  25. XCTAssertEqual(histogram.maxSeen, 3)
  26. XCTAssertEqual(histogram.minSeen, 1)
  27. XCTAssertEqual(histogram.sum, 6)
  28. XCTAssertEqual(histogram.sumOfSquares, 14)
  29. }
  30. func testBuckets() {
  31. var histogram = Histogram()
  32. histogram.add(value: 1)
  33. histogram.add(value: 1)
  34. histogram.add(value: 3)
  35. var twoSeen = false
  36. var oneSeen = false
  37. for bucket in histogram.buckets {
  38. switch bucket {
  39. case 0:
  40. break
  41. case 1:
  42. XCTAssertFalse(oneSeen)
  43. oneSeen = true
  44. case 2:
  45. XCTAssertFalse(twoSeen)
  46. twoSeen = true
  47. default:
  48. XCTFail()
  49. }
  50. }
  51. XCTAssertTrue(oneSeen)
  52. XCTAssertTrue(twoSeen)
  53. }
  54. func testMerge() {
  55. var histogram = Histogram()
  56. histogram.add(value: 1)
  57. histogram.add(value: 2)
  58. histogram.add(value: 3)
  59. let histogram2 = Histogram()
  60. histogram.add(value: 1)
  61. histogram.add(value: 1)
  62. histogram.add(value: 3)
  63. XCTAssertNoThrow(try histogram.merge(source: histogram2))
  64. XCTAssertEqual(histogram.countOfValuesSeen, 6)
  65. XCTAssertEqual(histogram.maxSeen, 3)
  66. XCTAssertEqual(histogram.minSeen, 1)
  67. XCTAssertEqual(histogram.sum, 11)
  68. XCTAssertEqual(histogram.sumOfSquares, 25)
  69. var threeSeen = false
  70. var twoSeen = false
  71. var oneSeen = false
  72. for bucket in histogram.buckets {
  73. switch bucket {
  74. case 0:
  75. break
  76. case 1:
  77. XCTAssertFalse(oneSeen)
  78. oneSeen = true
  79. case 2:
  80. XCTAssertFalse(twoSeen)
  81. twoSeen = true
  82. case 3:
  83. XCTAssertFalse(threeSeen)
  84. threeSeen = true
  85. default:
  86. XCTFail()
  87. }
  88. }
  89. XCTAssertTrue(oneSeen)
  90. XCTAssertTrue(twoSeen)
  91. XCTAssertTrue(threeSeen)
  92. }
  93. }