Improve encapsulation of TimerSmoother

This commit is contained in:
sfc-gh-tclinkenbeard 2022-03-11 07:14:03 -04:00
parent e00820cdd7
commit 3a6568580a
2 changed files with 31 additions and 34 deletions

View File

@ -65,10 +65,20 @@ public:
}
};
// TODO: Improve encapsulation
struct TimerSmoother {
class TimerSmoother {
// Times (t) are expected to be nondecreasing
double eFoldingTime;
double total;
mutable double time, estimate;
void update(double t) const {
double elapsed = t - time;
time = t;
estimate += (total - estimate) * (1 - exp(-elapsed / eFoldingTime));
}
public:
explicit TimerSmoother(double eFoldingTime) : eFoldingTime(eFoldingTime) { reset(0); }
void reset(double value) {
time = 0;
@ -82,24 +92,17 @@ struct TimerSmoother {
total += delta;
}
// smoothTotal() is a continuous (under)estimate of the sum of all addDeltas()
double smoothTotal(double t = timer()) {
double smoothTotal(double t = timer()) const {
update(t);
return estimate;
}
// smoothRate() is d/dt[smoothTotal], and is NOT continuous
double smoothRate(double t = timer()) {
double smoothRate(double t = timer()) const {
update(t);
return (total - estimate) / eFoldingTime;
}
void update(double t) {
double elapsed = t - time;
time = t;
estimate += (total - estimate) * (1 - exp(-elapsed / eFoldingTime));
}
double eFoldingTime;
double time, total, estimate;
double getTotal() const { return total; }
};
#endif

View File

@ -315,29 +315,23 @@ struct StorageWiggleMetrics {
template <class Ar>
void serialize(Ar& ar) {
double step_total, round_total;
if (!ar.isDeserializing) {
step_total = smoothed_wiggle_duration.getTotal();
round_total = smoothed_round_duration.getTotal();
}
serializer(ar,
last_wiggle_start,
last_wiggle_finish,
step_total,
finished_wiggle,
last_round_start,
last_round_finish,
round_total,
finished_round);
if (ar.isDeserializing) {
double step_total, round_total;
serializer(ar,
last_wiggle_start,
last_wiggle_finish,
step_total,
finished_wiggle,
last_round_start,
last_round_finish,
round_total,
finished_round);
smoothed_round_duration.reset(round_total);
smoothed_wiggle_duration.reset(step_total);
} else {
serializer(ar,
last_wiggle_start,
last_wiggle_finish,
smoothed_wiggle_duration.total,
finished_wiggle,
last_round_start,
last_round_finish,
smoothed_round_duration.total,
finished_round);
}
}
@ -375,14 +369,14 @@ struct StorageWiggleMetrics {
result["last_round_finish_datetime"] = timerIntToGmt(last_round_finish);
result["last_round_start_timestamp"] = last_round_start;
result["last_round_finish_timestamp"] = last_round_finish;
result["smoothed_round_seconds"] = smoothed_round_duration.estimate;
result["smoothed_round_seconds"] = smoothed_round_duration.smoothTotal();
result["finished_round"] = finished_round;
result["last_wiggle_start_datetime"] = timerIntToGmt(last_wiggle_start);
result["last_wiggle_finish_datetime"] = timerIntToGmt(last_wiggle_finish);
result["last_wiggle_start_timestamp"] = last_wiggle_start;
result["last_wiggle_finish_timestamp"] = last_wiggle_finish;
result["smoothed_wiggle_seconds"] = smoothed_wiggle_duration.estimate;
result["smoothed_wiggle_seconds"] = smoothed_wiggle_duration.smoothTotal();
result["finished_wiggle"] = finished_wiggle;
return result;
}