Mercurial > hg > orthanc
comparison UnitTestsSources/UnitTestsMain.cpp @ 3174:8ea7c4546c3a
primitives to collect metrics in Orthanc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 29 Jan 2019 15:15:48 +0100 |
parents | ce272138f15e |
children | 810772486249 |
comparison
equal
deleted
inserted
replaced
3173:096f4a29f223 | 3174:8ea7c4546c3a |
---|---|
39 #include <ctype.h> | 39 #include <ctype.h> |
40 | 40 |
41 #include "../Core/DicomFormat/DicomTag.h" | 41 #include "../Core/DicomFormat/DicomTag.h" |
42 #include "../Core/HttpServer/HttpToolbox.h" | 42 #include "../Core/HttpServer/HttpToolbox.h" |
43 #include "../Core/Logging.h" | 43 #include "../Core/Logging.h" |
44 #include "../Core/MetricsRegistry.h" | |
44 #include "../Core/OrthancException.h" | 45 #include "../Core/OrthancException.h" |
45 #include "../Core/TemporaryFile.h" | 46 #include "../Core/TemporaryFile.h" |
46 #include "../Core/Toolbox.h" | 47 #include "../Core/Toolbox.h" |
47 #include "../OrthancServer/OrthancInitialization.h" | 48 #include "../OrthancServer/OrthancInitialization.h" |
48 | 49 |
1234 ASSERT_EQ("A" + env["PATH"] + "B", | 1235 ASSERT_EQ("A" + env["PATH"] + "B", |
1235 Toolbox::SubstituteVariables("A${PATH}B", env)); | 1236 Toolbox::SubstituteVariables("A${PATH}B", env)); |
1236 } | 1237 } |
1237 | 1238 |
1238 | 1239 |
1240 TEST(MetricsRegistry, Basic) | |
1241 { | |
1242 { | |
1243 MetricsRegistry m; | |
1244 m.SetEnabled(false); | |
1245 m.SetValue("hello.world", 42.5f); | |
1246 | |
1247 std::string s; | |
1248 m.ExportPrometheusText(s); | |
1249 ASSERT_TRUE(s.empty()); | |
1250 } | |
1251 | |
1252 { | |
1253 MetricsRegistry m; | |
1254 m.Register("hello.world", MetricsType_Default); | |
1255 | |
1256 std::string s; | |
1257 m.ExportPrometheusText(s); | |
1258 ASSERT_TRUE(s.empty()); | |
1259 } | |
1260 | |
1261 { | |
1262 MetricsRegistry m; | |
1263 m.SetValue("hello.world", 42.5f); | |
1264 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.world")); | |
1265 ASSERT_THROW(m.GetMetricsType("nope"), OrthancException); | |
1266 | |
1267 std::string s; | |
1268 m.ExportPrometheusText(s); | |
1269 | |
1270 std::vector<std::string> t; | |
1271 Toolbox::TokenizeString(t, s, '\n'); | |
1272 ASSERT_EQ(2u, t.size()); | |
1273 ASSERT_EQ("hello.world 42.5 ", t[0].substr(0, 17)); | |
1274 ASSERT_TRUE(t[1].empty()); | |
1275 } | |
1276 | |
1277 { | |
1278 MetricsRegistry m; | |
1279 m.Register("hello.max", MetricsType_MaxOver10Seconds); | |
1280 m.SetValue("hello.max", 10); | |
1281 m.SetValue("hello.max", 20); | |
1282 m.SetValue("hello.max", -10); | |
1283 m.SetValue("hello.max", 5); | |
1284 | |
1285 m.Register("hello.min", MetricsType_MinOver10Seconds); | |
1286 m.SetValue("hello.min", 10); | |
1287 m.SetValue("hello.min", 20); | |
1288 m.SetValue("hello.min", -10); | |
1289 m.SetValue("hello.min", 5); | |
1290 | |
1291 m.Register("hello.default", MetricsType_Default); | |
1292 m.SetValue("hello.default", 10); | |
1293 m.SetValue("hello.default", 20); | |
1294 m.SetValue("hello.default", -10); | |
1295 m.SetValue("hello.default", 5); | |
1296 | |
1297 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("hello.max")); | |
1298 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("hello.min")); | |
1299 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.default")); | |
1300 | |
1301 std::string s; | |
1302 m.ExportPrometheusText(s); | |
1303 | |
1304 std::vector<std::string> t; | |
1305 Toolbox::TokenizeString(t, s, '\n'); | |
1306 ASSERT_EQ(4u, t.size()); | |
1307 ASSERT_TRUE(t[3].empty()); | |
1308 | |
1309 std::map<std::string, std::string> u; | |
1310 for (size_t i = 0; i < t.size() - 1; i++) | |
1311 { | |
1312 std::vector<std::string> v; | |
1313 Toolbox::TokenizeString(v, t[i], ' '); | |
1314 u[v[0]] = v[1]; | |
1315 } | |
1316 | |
1317 ASSERT_EQ("20", u["hello.max"]); | |
1318 ASSERT_EQ("-10", u["hello.min"]); | |
1319 ASSERT_EQ("5", u["hello.default"]); | |
1320 } | |
1321 | |
1322 { | |
1323 MetricsRegistry m; | |
1324 | |
1325 m.SetValue("a", 10); | |
1326 m.SetValue("b", 10, MetricsType_MinOver10Seconds); | |
1327 | |
1328 m.Register("c", MetricsType_MaxOver10Seconds); | |
1329 m.SetValue("c", 10, MetricsType_MinOver10Seconds); | |
1330 | |
1331 m.Register("d", MetricsType_MaxOver10Seconds); | |
1332 m.Register("d", MetricsType_Default); | |
1333 | |
1334 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("a")); | |
1335 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b")); | |
1336 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("c")); | |
1337 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("d")); | |
1338 } | |
1339 | |
1340 { | |
1341 MetricsRegistry m; | |
1342 | |
1343 { | |
1344 MetricsRegistry::Timer t1(m, "a"); | |
1345 MetricsRegistry::Timer t2(m, "b", MetricsType_MinOver10Seconds); | |
1346 } | |
1347 | |
1348 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("a")); | |
1349 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b")); | |
1350 } | |
1351 } | |
1352 | |
1353 | |
1239 int main(int argc, char **argv) | 1354 int main(int argc, char **argv) |
1240 { | 1355 { |
1241 Logging::Initialize(); | 1356 Logging::Initialize(); |
1242 Logging::EnableInfoLevel(true); | 1357 Logging::EnableInfoLevel(true); |
1243 Toolbox::DetectEndianness(); | 1358 Toolbox::DetectEndianness(); |