HistogramTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  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. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using Grpc.Core;
  23. using Grpc.Core.Utils;
  24. using Grpc.Testing;
  25. using NUnit.Framework;
  26. namespace Grpc.IntegrationTesting
  27. {
  28. public class HistogramTest
  29. {
  30. [Test]
  31. public void Simple()
  32. {
  33. var hist = new Histogram(0.01, 60e9);
  34. hist.AddObservation(10000);
  35. hist.AddObservation(10000);
  36. hist.AddObservation(11000);
  37. hist.AddObservation(11000);
  38. var data = hist.GetSnapshot();
  39. Assert.AreEqual(4, data.Count);
  40. Assert.AreEqual(42000.0, data.Sum, 1e-6);
  41. Assert.AreEqual(10000, data.MinSeen);
  42. Assert.AreEqual(11000, data.MaxSeen);
  43. Assert.AreEqual(2.0*10000*10000 + 2.0*11000*11000, data.SumOfSquares, 1e-6);
  44. // 1.01^925 < 10000 < 1.01^926
  45. Assert.AreEqual(2, data.Bucket[925]);
  46. Assert.AreEqual(2, data.Bucket[935]);
  47. }
  48. [Test]
  49. public void ExtremeObservations()
  50. {
  51. var hist = new Histogram(0.01, 60e9);
  52. hist.AddObservation(-0.5); // should be in the first bucket
  53. hist.AddObservation(1e12); // should be in the last bucket
  54. var data = hist.GetSnapshot();
  55. Assert.AreEqual(1, data.Bucket[0]);
  56. Assert.AreEqual(1, data.Bucket[data.Bucket.Count - 1]);
  57. }
  58. [Test]
  59. public void MergeSnapshots()
  60. {
  61. var data = new HistogramData();
  62. var hist1 = new Histogram(0.01, 60e9);
  63. hist1.AddObservation(-0.5); // should be in the first bucket
  64. hist1.AddObservation(1e12); // should be in the last bucket
  65. hist1.GetSnapshot(data, false);
  66. var hist2 = new Histogram(0.01, 60e9);
  67. hist2.AddObservation(10000);
  68. hist2.AddObservation(11000);
  69. hist2.GetSnapshot(data, false);
  70. Assert.AreEqual(4, data.Count);
  71. Assert.AreEqual(-0.5, data.MinSeen);
  72. Assert.AreEqual(1e12, data.MaxSeen);
  73. Assert.AreEqual(1, data.Bucket[0]);
  74. Assert.AreEqual(1, data.Bucket[925]);
  75. Assert.AreEqual(1, data.Bucket[935]);
  76. Assert.AreEqual(1, data.Bucket[data.Bucket.Count - 1]);
  77. }
  78. [Test]
  79. public void Reset()
  80. {
  81. var hist = new Histogram(0.01, 60e9);
  82. hist.AddObservation(10000);
  83. hist.AddObservation(11000);
  84. var data = hist.GetSnapshot(true); // snapshot contains data before reset
  85. Assert.AreEqual(2, data.Count);
  86. Assert.AreEqual(10000, data.MinSeen);
  87. Assert.AreEqual(11000, data.MaxSeen);
  88. data = hist.GetSnapshot(); // snapshot contains state after reset
  89. Assert.AreEqual(0, data.Count);
  90. Assert.AreEqual(double.PositiveInfinity, data.MinSeen);
  91. Assert.AreEqual(double.NegativeInfinity, data.MaxSeen);
  92. Assert.AreEqual(0, data.Sum);
  93. Assert.AreEqual(0, data.SumOfSquares);
  94. CollectionAssert.AreEqual(new uint[data.Bucket.Count], data.Bucket);
  95. }
  96. }
  97. }