Reland "Refactor AnalyzerConfig to use Timestamps instead of microseconds."

This is a reland of 43fb16921b29ecd3a2d87876dda75c575e05f66a

Original change's description:
> Refactor AnalyzerConfig to use Timestamps instead of microseconds.
>
> Add optional offset-to-UTC parameter to output. This allows aligning
> the x-axis in the generated charts to other UTC-based logs.
>
> Bug: b/215140373
> Change-Id: I65bcd295718acbb8c94e363907c1abc458067bfd
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/250203
> Reviewed-by: Kristoffer Erlandsson <kerl@google.com>
> Commit-Queue: Björn Terelius <terelius@webrtc.org>
> Cr-Commit-Position: refs/heads/main@{#35992}

Bug: b/215140373
Change-Id: Id2b88cc4b8078a97275d49a617581cbbd02d2c6f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/252380
Reviewed-by: Kristoffer Erlandsson <kerl@google.com>
Commit-Queue: Björn Terelius <terelius@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36066}
This commit is contained in:
Björn Terelius
2022-02-24 13:34:45 +01:00
committed by WebRTC LUCI CQ
parent caf2063a9e
commit 7a992e21b9
36 changed files with 203 additions and 129 deletions

View File

@ -22,6 +22,7 @@
namespace webrtc {
constexpr int kNumMicrosecsPerSec = 1000000;
constexpr int kNumMillisecsPerSec = 1000;
constexpr float kLeftMargin = 0.01f;
constexpr float kRightMargin = 0.02f;
constexpr float kBottomMargin = 0.02f;
@ -29,25 +30,38 @@ constexpr float kTopMargin = 0.05f;
class AnalyzerConfig {
public:
float GetCallTimeSec(int64_t timestamp_us) const {
int64_t offset = normalize_time_ ? begin_time_ : 0;
return static_cast<float>(timestamp_us - offset) / 1000000;
float GetCallTimeSec(Timestamp timestamp) const {
Timestamp offset = normalize_time_ ? begin_time_ : Timestamp::Zero();
return static_cast<float>((timestamp - offset).us()) / 1000000;
}
float GetCallTimeSecFromMs(int64_t timestamp_ms) const {
return GetCallTimeSec(Timestamp::Millis(timestamp_ms));
}
float CallBeginTimeSec() const { return GetCallTimeSec(begin_time_); }
float CallEndTimeSec() const { return GetCallTimeSec(end_time_); }
int64_t CallTimeToUtcOffsetMs() {
if (normalize_time_) {
Timestamp utc_begin_time_ = begin_time_ + rtc_to_utc_offset_;
return utc_begin_time_.ms();
}
return rtc_to_utc_offset_.ms();
}
// Window and step size used for calculating moving averages, e.g. bitrate.
// The generated data points will be `step_` microseconds apart.
// Only events occurring at most `window_duration_` microseconds before the
// current data point will be part of the average.
int64_t window_duration_;
int64_t step_;
// The generated data points will be `step_.ms()` milliseconds apart.
// Only events occurring at most `window_duration_.ms()` milliseconds before
// the current data point will be part of the average.
TimeDelta window_duration_ = TimeDelta::Millis(250);
TimeDelta step_ = TimeDelta::Millis(10);
// First and last events of the log.
int64_t begin_time_;
int64_t end_time_;
Timestamp begin_time_ = Timestamp::MinusInfinity();
Timestamp end_time_ = Timestamp::MinusInfinity();
TimeDelta rtc_to_utc_offset_ = TimeDelta::Zero();
bool normalize_time_;
};
@ -152,17 +166,17 @@ void MovingAverage(
size_t window_index_end = 0;
ResultType sum_in_window = 0;
for (int64_t t = config.begin_time_; t < config.end_time_ + config.step_;
for (Timestamp t = config.begin_time_; t < config.end_time_ + config.step_;
t += config.step_) {
while (window_index_end < data_view.size() &&
data_view[window_index_end].log_time_us() < t) {
data_view[window_index_end].log_time() < t) {
absl::optional<ResultType> value = fy(data_view[window_index_end]);
if (value)
sum_in_window += *value;
++window_index_end;
}
while (window_index_begin < data_view.size() &&
data_view[window_index_begin].log_time_us() <
data_view[window_index_begin].log_time() <
t - config.window_duration_) {
absl::optional<ResultType> value = fy(data_view[window_index_begin]);
if (value)
@ -170,7 +184,7 @@ void MovingAverage(
++window_index_begin;
}
float window_duration_s =
static_cast<float>(config.window_duration_) / kNumMicrosecsPerSec;
static_cast<float>(config.window_duration_.us()) / kNumMicrosecsPerSec;
float x = config.GetCallTimeSec(t);
float y = sum_in_window / window_duration_s;
result->points.emplace_back(x, y);