Skip to content

Commit

Permalink
Fix event_statistics_example
Browse files Browse the repository at this point in the history
  • Loading branch information
Oipo committed Mar 12, 2023
1 parent 63880db commit 82d6c71
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ run_examples ()
../bin/ichor_http_example || exit 1
../bin/ichor_multithreaded_example || exit 1
../bin/ichor_optional_dependency_example || exit 1
../bin/ichor_event_statistics_example || exit 1
../bin/ichor_serializer_example || exit 1
../bin/ichor_tcp_example || exit 1
../bin/ichor_timer_example || exit 1
Expand Down
2 changes: 1 addition & 1 deletion examples/event_statistics_example/UsingStatisticsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UsingStatisticsService final : public AdvancedService<UsingStatisticsServi
ICHOR_LOG_INFO(_logger, "UsingStatisticsService started");
auto &quitTimer = _timerFactory->createTimer();
auto &bogusTimer = _timerFactory->createTimer();
quitTimer.setChronoInterval(15s);
quitTimer.setChronoInterval(5s);
bogusTimer.setChronoInterval(100ms);

quitTimer.setCallback([this]() {
Expand Down
6 changes: 5 additions & 1 deletion include/ichor/services/metrics/EventStatisticsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Ichor {

class EventStatisticsService final : public IEventStatisticsService, public AdvancedService<EventStatisticsService> {
public:
EventStatisticsService() = default;
EventStatisticsService(DependencyRegister &reg, Properties props);
~EventStatisticsService() final = default;

const unordered_map<uint64_t, std::vector<StatisticEntry>>& getRecentStatistics() const noexcept final;
Expand All @@ -48,6 +48,9 @@ namespace Ichor {
bool preInterceptEvent(Event const &evt);
void postInterceptEvent(Event const &evt, bool processed);

void addDependencyInstance(ILogger &logger, IService &);
void removeDependencyInstance(ILogger &logger, IService&);

void addDependencyInstance(ITimerFactory &factory, IService &);
void removeDependencyInstance(ITimerFactory &factory, IService&);

Expand All @@ -66,6 +69,7 @@ namespace Ichor {
bool _showStatisticsOnStop{false};
uint64_t _averagingIntervalMs{5000};
EventInterceptorRegistration _interceptorRegistration{};
ILogger *_logger{};
ITimerFactory *_timerFactory{};
};
}
15 changes: 14 additions & 1 deletion src/services/metrics/EventStatisticsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <ichor/DependencyManager.h>
#include <numeric>

Ichor::EventStatisticsService::EventStatisticsService(DependencyRegister &reg, Properties props) : AdvancedService<EventStatisticsService>(std::move(props)) {
reg.registerDependency<ITimerFactory>(this, true);
reg.registerDependency<ILogger>(this, true);
}

Ichor::Task<tl::expected<void, Ichor::StartError>> Ichor::EventStatisticsService::start() {
if(getProperties().contains("ShowStatisticsOnStop")) {
_showStatisticsOnStop = Ichor::any_cast<bool>(getProperties()["ShowStatisticsOnStop"]);
Expand Down Expand Up @@ -47,7 +52,7 @@ Ichor::Task<void> Ichor::EventStatisticsService::stop() {
auto avg = std::accumulate(begin(statistics), end(statistics), 0L, [](int64_t i, const AveragedStatisticEntry &entry) -> int64_t { return i + entry.avgProcessingTimeRequired; }) / static_cast<int64_t>(statistics.size());
auto occ = std::accumulate(begin(statistics), end(statistics), 0L, [](int64_t i, const AveragedStatisticEntry &entry){ return i + entry.occurrences; });

ICHOR_LOG_ERROR(GetThreadLocalManager().getLogger(), "Dm {:L} Event type {} occurred {:L} times, min/max/avg processing: {:L}/{:L}/{:L} ns", GetThreadLocalManager().getId(), _eventTypeToNameMapper[key], occ, min, max, avg);
ICHOR_LOG_ERROR(_logger, "Dm {:L} Event type {} occurred {:L} times, min/max/avg processing: {:L}/{:L}/{:L} ns", GetThreadLocalManager().getId(), _eventTypeToNameMapper[key], occ, min, max, avg);
}
}

Expand Down Expand Up @@ -84,6 +89,14 @@ void Ichor::EventStatisticsService::postInterceptEvent(Event const &evt, bool pr
}
}

void Ichor::EventStatisticsService::addDependencyInstance(ILogger &logger, IService &) {
_logger = &logger;
}

void Ichor::EventStatisticsService::removeDependencyInstance(ILogger &, IService&) {
_logger = nullptr;
}

void Ichor::EventStatisticsService::addDependencyInstance(ITimerFactory &factory, IService &) {
_timerFactory = &factory;
}
Expand Down

0 comments on commit 82d6c71

Please sign in to comment.