1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
// Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <stats/observation.h>
#include <exceptions/exceptions.h>
#include <util/boost_time_utils.h>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <gtest/gtest.h>
#include <iostream>
#include <sstream>
#include <unistd.h>
using namespace isc;
using namespace isc::stats;
using namespace boost::posix_time;
namespace {
/// @brief Test class for Observation
///
/// This simple fixture class initializes four observations:
/// a (integer), b (float), c(time duration) and d (string).
class ObservationTest : public ::testing::Test {
public:
/// @brief Constructor
/// Initializes four observations.
ObservationTest()
:a("alpha", static_cast<int64_t>(1234)), // integer
b("beta", 12.34), // float
c("gamma", millisec::time_duration(1,2,3,4)), // duration
d("delta", "1234") { // string
}
Observation a;
Observation b;
Observation c;
Observation d;
};
// Basic tests for the Observation constructors. This test checks whether
// parameters passed to the constructor initialize the object properly.
TEST_F(ObservationTest, constructor) {
EXPECT_EQ(Observation::STAT_INTEGER, a.getType());
EXPECT_EQ(Observation::STAT_FLOAT, b.getType());
EXPECT_EQ(Observation::STAT_DURATION, c.getType());
EXPECT_EQ(Observation::STAT_STRING, d.getType());
EXPECT_EQ(1234, a.getInteger().first);
EXPECT_EQ(12.34, b.getFloat().first);
EXPECT_EQ(millisec::time_duration(1,2,3,4),
c.getDuration().first);
EXPECT_EQ("1234", d.getString().first);
// Let's check that attempting to get a different type
// than used will cause an exception.
EXPECT_THROW(a.getFloat(), InvalidStatType);
EXPECT_THROW(a.getDuration(), InvalidStatType);
EXPECT_THROW(a.getString(), InvalidStatType);
EXPECT_THROW(b.getInteger(), InvalidStatType);
EXPECT_THROW(b.getDuration(), InvalidStatType);
EXPECT_THROW(b.getString(), InvalidStatType);
EXPECT_THROW(c.getInteger(), InvalidStatType);
EXPECT_THROW(c.getFloat(), InvalidStatType);
EXPECT_THROW(c.getString(), InvalidStatType);
EXPECT_THROW(d.getInteger(), InvalidStatType);
EXPECT_THROW(d.getFloat(), InvalidStatType);
EXPECT_THROW(d.getDuration(), InvalidStatType);
}
// This test checks whether it is possible to set to an absolute value for all
// given types.
TEST_F(ObservationTest, setValue) {
EXPECT_NO_THROW(a.setValue(static_cast<int64_t>(5678)));
EXPECT_NO_THROW(b.setValue(56e+78));
EXPECT_NO_THROW(c.setValue(millisec::time_duration(5,6,7,8)));
EXPECT_NO_THROW(d.setValue("fiveSixSevenEight"));
EXPECT_EQ(5678, a.getInteger().first);
EXPECT_EQ(56e+78, b.getFloat().first);
EXPECT_EQ(millisec::time_duration(5,6,7,8),
c.getDuration().first);
EXPECT_EQ("fiveSixSevenEight", d.getString().first);
// Now check whether setting value to a different type does
// throw an exception
EXPECT_THROW(a.setValue(56e+78), InvalidStatType);
EXPECT_THROW(a.setValue(millisec::time_duration(5,6,7,8)), InvalidStatType);
EXPECT_THROW(a.setValue("fiveSixSevenEight"), InvalidStatType);
EXPECT_THROW(b.setValue(static_cast<int64_t>(5678)), InvalidStatType);
EXPECT_THROW(b.setValue(millisec::time_duration(5,6,7,8)), InvalidStatType);
EXPECT_THROW(b.setValue("fiveSixSevenEight"), InvalidStatType);
EXPECT_THROW(c.setValue(static_cast<int64_t>(5678)), InvalidStatType);
EXPECT_THROW(c.setValue(56e+78), InvalidStatType);
EXPECT_THROW(c.setValue("fiveSixSevenEight"), InvalidStatType);
EXPECT_THROW(d.setValue(static_cast<int64_t>(5678)), InvalidStatType);
EXPECT_THROW(d.setValue(56e+78), InvalidStatType);
EXPECT_THROW(d.setValue(millisec::time_duration(5,6,7,8)), InvalidStatType);
}
// This test checks whether it is possible to add value to existing
// counter.
TEST_F(ObservationTest, addValue) {
// Note: all Observations were set to 1234,12.34 or similar in
// ObservationTest constructor.
EXPECT_NO_THROW(a.addValue(static_cast<int64_t>(5678)));
EXPECT_NO_THROW(b.addValue(56.78));
EXPECT_NO_THROW(c.addValue(millisec::time_duration(5,6,7,8)));
EXPECT_NO_THROW(d.addValue("fiveSixSevenEight"));
EXPECT_EQ(6912, a.getInteger().first);
EXPECT_EQ(69.12, b.getFloat().first);
EXPECT_EQ(millisec::time_duration(6,8,10,12), c.getDuration().first);
EXPECT_EQ("1234fiveSixSevenEight", d.getString().first);
}
// Test checks whether timing is reported properly.
TEST_F(ObservationTest, timers) {
ptime before = microsec_clock::local_time();
b.setValue(123.0); // Set it to a random value and record the time.
// Allow a bit of imprecision. This test allows 500ms. That should be ok,
// when running on virtual machines.
ptime after = before + milliseconds(500);
// Now wait some time. We want to confirm that the timestamp recorded is the
// time the observation took place, not current time.
sleep(1);
FloatSample sample = b.getFloat();
// Let's check that the timestamp is within (before,after) range:
// before < sample-time < after
EXPECT_TRUE(before <= sample.second);
EXPECT_TRUE(sample.second <= after);
}
// Checks whether an integer statistic can generate proper JSON structures.
// See http://kea.isc.org/wiki/StatsDesign for details.
TEST_F(ObservationTest, integerToJSON) {
a.setValue(static_cast<int64_t>(1234));
std::string exp = "[ [ 1234, \""
+ isc::util::ptimeToText(a.getInteger().second) + "\" ] ]";
std::cout << a.getJSON()->str() << std::endl;
EXPECT_EQ(exp, a.getJSON()->str());
}
// Checks whether a floating point statistic can generate proper JSON
// structures. See http://kea.isc.org/wiki/StatsDesign for details.
TEST_F(ObservationTest, floatToJSON) {
// Let's use a value that converts easily to floating point.
// No need to deal with infinite fractions in binary systems.
b.setValue(1234.5);
std::string exp = "[ [ 1234.5, \""
+ isc::util::ptimeToText(b.getFloat().second) + "\" ] ]";
std::cout << b.getJSON()->str() << std::endl;
EXPECT_EQ(exp, b.getJSON()->str());
}
// Checks whether a time duration statistic can generate proper JSON structures.
// See http://kea.isc.org/wiki/StatsDesign for details.
TEST_F(ObservationTest, durationToJSON) {
// 1 hour 2 minutes 3 seconds and 4 milliseconds
c.setValue(time_duration(1,2,3,4));
std::string exp = "[ [ \"01:02:03.000004\", \""
+ isc::util::ptimeToText(c.getDuration().second) + "\" ] ]";
std::cout << c.getJSON()->str() << std::endl;
EXPECT_EQ(exp, c.getJSON()->str());
}
// Checks whether a string statistic can generate proper JSON structures.
// See http://kea.isc.org/wiki/StatsDesign for details.
TEST_F(ObservationTest, stringToJSON) {
//
d.setValue("Lorem ipsum dolor sit amet");
std::string exp = "[ [ \"Lorem ipsum dolor sit amet\", \""
+ isc::util::ptimeToText(d.getString().second) + "\" ] ]";
std::cout << d.getJSON()->str() << std::endl;
EXPECT_EQ(exp, d.getJSON()->str());
}
// Checks whether reset() resets the statistics properly.
TEST_F(ObservationTest, reset) {
a.reset(); // integer
b.reset(); // float
c.reset(); // duration
d.reset(); // string
EXPECT_EQ(0, a.getInteger().first);
EXPECT_EQ(0.0, b.getFloat().first);
EXPECT_EQ(time_duration(0,0,0,0), c.getDuration().first);
EXPECT_EQ("", d.getString().first);
}
// Checks whether an observation can keep its name.
TEST_F(ObservationTest, names) {
EXPECT_EQ("alpha", a.getName());
EXPECT_EQ("beta", b.getName());
EXPECT_EQ("gamma", c.getName());
EXPECT_EQ("delta", d.getName());
}
};
|