mirror of
https://github.com/overte-org/overte.git
synced 2025-08-07 12:10:44 +02:00
90 lines
2.6 KiB
C++
90 lines
2.6 KiB
C++
//
|
|
// PerfStat.h
|
|
// libraries/shared/src
|
|
//
|
|
// Created by Brad Hefta-Gaub on 3/29/13.
|
|
// Copyright 2013 High Fidelity, Inc.
|
|
//
|
|
// Poor-man's performance stats collector class. Useful for collecting timing
|
|
// details from various portions of the code.
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
//
|
|
|
|
#ifndef hifi_PerfStat_h
|
|
#define hifi_PerfStat_h
|
|
|
|
#include <stdint.h>
|
|
#include "SharedUtil.h"
|
|
#include "SimpleMovingAverage.h"
|
|
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <map>
|
|
|
|
class PerformanceWarning {
|
|
private:
|
|
quint64 _start;
|
|
const char* _message;
|
|
bool _renderWarningsOn;
|
|
bool _alwaysDisplay;
|
|
quint64* _runningTotal;
|
|
quint64* _totalCalls;
|
|
static bool _suppressShortTimings;
|
|
public:
|
|
|
|
PerformanceWarning(bool renderWarnings, const char* message, bool alwaysDisplay = false,
|
|
quint64* runningTotal = NULL, quint64* totalCalls = NULL) :
|
|
_start(usecTimestampNow()),
|
|
_message(message),
|
|
_renderWarningsOn(renderWarnings),
|
|
_alwaysDisplay(alwaysDisplay),
|
|
_runningTotal(runningTotal),
|
|
_totalCalls(totalCalls) { }
|
|
|
|
quint64 elapsed() const { return (usecTimestampNow() - _start); };
|
|
|
|
~PerformanceWarning();
|
|
|
|
static void setSuppressShortTimings(bool suppressShortTimings) { _suppressShortTimings = suppressShortTimings; }
|
|
};
|
|
|
|
class PerformanceTimerRecord {
|
|
public:
|
|
PerformanceTimerRecord() : _runningTotal(0), _totalCalls(0) {}
|
|
|
|
void recordResult(quint64 elapsed) { _runningTotal += elapsed; _totalCalls++; _movingAverage.updateAverage(elapsed); }
|
|
quint64 getAverage() const { return (_totalCalls == 0) ? 0 : _runningTotal / _totalCalls; }
|
|
quint64 getMovingAverage() const { return (_totalCalls == 0) ? 0 : _movingAverage.getAverage(); }
|
|
quint64 getCount() const { return _totalCalls; }
|
|
|
|
private:
|
|
quint64 _runningTotal;
|
|
quint64 _totalCalls;
|
|
SimpleMovingAverage _movingAverage;
|
|
};
|
|
|
|
class PerformanceTimer {
|
|
public:
|
|
|
|
PerformanceTimer(const QString& name) :
|
|
_start(usecTimestampNow()),
|
|
_name(name) { }
|
|
|
|
quint64 elapsed() const { return (usecTimestampNow() - _start); };
|
|
|
|
~PerformanceTimer();
|
|
|
|
static const PerformanceTimerRecord& getTimerRecord(const QString& name) { return _records[name]; };
|
|
static const QMap<QString, PerformanceTimerRecord>& getAllTimerRecords() { return _records; };
|
|
static void dumpAllTimerRecords();
|
|
|
|
private:
|
|
quint64 _start;
|
|
QString _name;
|
|
static QMap<QString, PerformanceTimerRecord> _records;
|
|
};
|
|
|
|
|
|
#endif // hifi_PerfStat_h
|