• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using System.Collections.Generic;
21 using System.IO;
22 using System.Linq;
23 using System.Threading;
24 using System.Threading.Tasks;
25 using Grpc.Core;
26 using Grpc.Core.Utils;
27 using Grpc.Testing;
28 using NUnit.Framework;
29 
30 namespace Grpc.IntegrationTesting
31 {
32     public class HistogramTest
33     {
34         [Test]
Simple()35         public void Simple()
36         {
37             var hist = new Histogram(0.01, 60e9);
38             hist.AddObservation(10000);
39             hist.AddObservation(10000);
40             hist.AddObservation(11000);
41             hist.AddObservation(11000);
42 
43             var data = hist.GetSnapshot();
44 
45             Assert.AreEqual(4, data.Count);
46             Assert.AreEqual(42000.0, data.Sum, 1e-6);
47             Assert.AreEqual(10000, data.MinSeen);
48             Assert.AreEqual(11000, data.MaxSeen);
49             Assert.AreEqual(2.0*10000*10000 + 2.0*11000*11000, data.SumOfSquares, 1e-6);
50 
51             // 1.01^925 < 10000 < 1.01^926
52             Assert.AreEqual(2, data.Bucket[925]);
53             Assert.AreEqual(2, data.Bucket[935]);
54         }
55 
56         [Test]
ExtremeObservations()57         public void ExtremeObservations()
58         {
59             var hist = new Histogram(0.01, 60e9);
60             hist.AddObservation(-0.5);  // should be in the first bucket
61             hist.AddObservation(1e12);  // should be in the last bucket
62 
63             var data = hist.GetSnapshot();
64             Assert.AreEqual(1, data.Bucket[0]);
65             Assert.AreEqual(1, data.Bucket[data.Bucket.Count - 1]);
66         }
67 
68         [Test]
MergeSnapshots()69         public void MergeSnapshots()
70         {
71             var data = new HistogramData();
72 
73             var hist1 = new Histogram(0.01, 60e9);
74             hist1.AddObservation(-0.5);  // should be in the first bucket
75             hist1.AddObservation(1e12);  // should be in the last bucket
76             hist1.GetSnapshot(data, false);
77 
78             var hist2 = new Histogram(0.01, 60e9);
79             hist2.AddObservation(10000);
80             hist2.AddObservation(11000);
81             hist2.GetSnapshot(data, false);
82 
83             Assert.AreEqual(4, data.Count);
84             Assert.AreEqual(-0.5, data.MinSeen);
85             Assert.AreEqual(1e12, data.MaxSeen);
86             Assert.AreEqual(1, data.Bucket[0]);
87             Assert.AreEqual(1, data.Bucket[925]);
88             Assert.AreEqual(1, data.Bucket[935]);
89             Assert.AreEqual(1, data.Bucket[data.Bucket.Count - 1]);
90         }
91 
92         [Test]
Reset()93         public void Reset()
94         {
95             var hist = new Histogram(0.01, 60e9);
96             hist.AddObservation(10000);
97             hist.AddObservation(11000);
98 
99             var data = hist.GetSnapshot(true);  // snapshot contains data before reset
100             Assert.AreEqual(2, data.Count);
101             Assert.AreEqual(10000, data.MinSeen);
102             Assert.AreEqual(11000, data.MaxSeen);
103 
104             data = hist.GetSnapshot();  // snapshot contains state after reset
105             Assert.AreEqual(0, data.Count);
106             Assert.AreEqual(double.PositiveInfinity, data.MinSeen);
107             Assert.AreEqual(double.NegativeInfinity, data.MaxSeen);
108             Assert.AreEqual(0, data.Sum);
109             Assert.AreEqual(0, data.SumOfSquares);
110             CollectionAssert.AreEqual(new uint[data.Bucket.Count], data.Bucket);
111         }
112     }
113 }
114