blob: 0fac8386e865469575ebcac62636d3ed910fea2e [file] [log] [blame]
#include "logging.hpp"
#include "request_stats.hpp"
#include <memory>
#include <thread>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace managedStore
{
namespace
{
using testing::_;
using testing::DoAll;
using testing::InvokeArgument;
using testing::Return;
TEST(RequestStatsTest, TestStatsStore)
{
auto statsStoreConfig = managedStore::RequestStatsStoreConfig();
statsStoreConfig.enable = true;
auto statsStore = managedStore::RequestStatsStore(statsStoreConfig);
BMCWEB_LOG_INFO << "ping: statsStore: " << statsStore.toJson();
EXPECT_EQ(true, statsStoreConfig.enable);
const auto nowPlus500 =
managedStore::clockNow() + std::chrono::milliseconds(500);
const auto nowPlus800 =
managedStore::clockNow() + std::chrono::milliseconds(800);
const std::string rqBatman = "/batman/was/here";
{
auto rq = managedStore::RequestStatsContext(rqBatman);
rq.setOK(false);
statsStore.logRequestStats(nowPlus500, rq);
}
const std::string rqSuperman = "/superman/was/here";
{
auto rq = managedStore::RequestStatsContext(rqSuperman);
rq.setOK(true);
statsStore.logRequestStats(nowPlus800, rq);
}
auto statsStoreJson = statsStore.toJson();
BMCWEB_LOG_INFO << "statsStoreJson: " << statsStoreJson;
// 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, statsStoreJson["config"]["enable"]);
EXPECT_EQ(1, statsStoreJson["statsMap"]["/batman/was/here"]["countError"]);
EXPECT_EQ(0, statsStoreJson["statsMap"]["/batman/was/here"]["countOK"]);
EXPECT_EQ(1, statsStoreJson["statsMap"]["/batman/was/here"]
["latencyHistogram"][0]["count"]);
EXPECT_EQ(500, statsStoreJson["statsMap"]["/batman/was/here"]
["latencyHistogram"][0]["milliseconds"]);
EXPECT_EQ("/batman/was/here",
statsStoreJson["statsMap"]["/batman/was/here"]["target"]);
EXPECT_EQ(0,
statsStoreJson["statsMap"]["/superman/was/here"]["countError"]);
EXPECT_EQ(1, statsStoreJson["statsMap"]["/superman/was/here"]["countOK"]);
EXPECT_EQ(1, statsStoreJson["statsMap"]["/superman/was/here"]
["latencyHistogram"][0]["count"]);
EXPECT_EQ(800, statsStoreJson["statsMap"]["/superman/was/here"]
["latencyHistogram"][0]["milliseconds"]);
EXPECT_EQ("/superman/was/here",
statsStoreJson["statsMap"]["/superman/was/here"]["target"]);
// clear the store should reset all the stats:
statsStore.clear();
EXPECT_EQ(0, statsStore.queryStats().size());
}
} // namespace
} // namespace managedStore