blob: 9a3cf002c9913a4495f4d4c5d4f13a4ee461035e [file] [log] [blame]
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -07004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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.
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.IO;
22using System.Linq;
23using System.Threading;
24using System.Threading.Tasks;
25using Grpc.Core;
26using Grpc.Core.Utils;
27using Grpc.Testing;
28using NUnit.Framework;
29
30namespace Grpc.IntegrationTesting
31{
32 public class HistogramTest
33 {
34 [Test]
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]
57 public void ExtremeObservations()
58 {
59 var hist = new Histogram(0.01, 60e9);
60 hist.AddObservation(-0.5); // should be in the first bucket
Jan Tattermusch19e3d3b2017-05-17 11:05:06 +020061 hist.AddObservation(1e12); // should be in the last bucket
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070062
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]
Jan Tattermusch19e3d3b2017-05-17 11:05:06 +020069 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]
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070093 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}