blob: 961b422e163ea67957e24bfae62047bcc90a271d [file] [log] [blame]
#include "request_stats.hpp"
#include <chrono> // NOLINT
#include <string>
#include <gtest/gtest.h>
#include "logging.hpp"
#include "managed_store_clock.hpp"
namespace managedStore {
namespace {
TEST(RequestStatsTest, TestStatsStore) {
auto stats_store_config = managedStore::RequestStatsStoreConfig();
stats_store_config.enable = true;
auto stats_store = managedStore::RequestStatsStore(stats_store_config);
BMCWEB_LOG_INFO << "ping: stats_store: " << stats_store.toJson();
EXPECT_EQ(true, stats_store_config.enable);
const auto now_plus_500 =
managedStore::clockNow() + std::chrono::milliseconds(500);
const auto now_plus_800 =
managedStore::clockNow() + std::chrono::milliseconds(800);
const std::string rq_batman = "/batman/was/here";
{
auto rq = managedStore::RequestStatsContext(rq_batman);
rq.setOK(false);
stats_store.logRequestStats(now_plus_500, rq);
}
const std::string rq_superman = "/superman/was/here";
{
auto rq = managedStore::RequestStatsContext(rq_superman);
rq.setOK(true);
stats_store.logRequestStats(now_plus_800, rq);
}
auto stats_store_json = stats_store.toJson();
BMCWEB_LOG_INFO << "stats_store_json: " << stats_store_json;
// expected:
// =========
// {
// "config": {
// "enable": true
// },
// "statsMap": {
// "/batman/was/here": {
// "countError": 1,
// "countOK": 0,
// "latencyHistogram": [
// {
// "count": 1,
// "milliseconds": 400
// }
// ],
// "queryURLKey": "/batman/was/here"
// },
// "/superman/was/here": {
// "countError": 0,
// "countOK": 1,
// "latencyHistogram": [
// {
// "count": 1,
// "milliseconds": 700
// }
// ],
// "queryURLKey": "/superman/was/here"
// }
// }
// }
EXPECT_EQ(true, stats_store_json["config"]["enable"]);
EXPECT_EQ(1, stats_store_json["statsMap"]["/batman/was/here"]["countError"]);
EXPECT_EQ(0, stats_store_json["statsMap"]["/batman/was/here"]["countOK"]);
EXPECT_EQ(1, stats_store_json["statsMap"]["/batman/was/here"]
["latencyHistogram"][0]["count"]);
EXPECT_EQ(500, stats_store_json["statsMap"]["/batman/was/here"]
["latencyHistogram"][0]["milliseconds"]);
EXPECT_EQ("/batman/was/here",
stats_store_json["statsMap"]["/batman/was/here"]["target"]);
EXPECT_EQ(0,
stats_store_json["statsMap"]["/superman/was/here"]["countError"]);
EXPECT_EQ(1, stats_store_json["statsMap"]["/superman/was/here"]["countOK"]);
EXPECT_EQ(1, stats_store_json["statsMap"]["/superman/was/here"]
["latencyHistogram"][0]["count"]);
EXPECT_EQ(800, stats_store_json["statsMap"]["/superman/was/here"]
["latencyHistogram"][0]["milliseconds"]);
EXPECT_EQ("/superman/was/here",
stats_store_json["statsMap"]["/superman/was/here"]["target"]);
// clear the store should reset all the stats:
stats_store.clear();
EXPECT_EQ(0, stats_store.queryStats().size());
}
} // namespace
} // namespace managedStore