Remove dependency on std::sort implementation detail.

When adding samples to the counter in the test body, most of them get
added at the same time (rtc::TimeMicros()). This means that the
comparator [2] is not able to sort them explicitly (for the comparator
they are equal).

PerfTest.TestGetPerfResultsHistogramsWithStatsCounter was relying on
std::sort implementation being stable, but it is not. If stability is
needed, std::stable_sort should be used so this CL switches
::webrtc::test:GetSortedSamples() to std::stable_sort.

[1] - https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/numerics/samples_stats_counter.cc;l=31;drc=9d777620236ec76754cfce19f6e82dd18e52d22c
[2] - https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/test/testsupport/perf_test.cc;l=51-55;drc=a2e3d80cf6f8833ec6f2c5f8e69c2a58a52ddd62

Bug: None
Change-Id: I99a44720ce3ad577f0cdb42aa633bd73d62aeaff
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/242961
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35600}
This commit is contained in:
Mirko Bonadei 2021-12-24 11:27:55 +01:00 committed by WebRTC LUCI CQ
parent d77b5924c8
commit d2637a3436
2 changed files with 8 additions and 10 deletions

View File

@ -12,6 +12,7 @@
#include <stdio.h>
#include <algorithm>
#include <fstream>
#include <set>
#include <sstream>
@ -48,11 +49,11 @@ std::vector<SamplesStatsCounter::StatsSample> GetSortedSamples(
rtc::ArrayView<const SamplesStatsCounter::StatsSample> view =
counter.GetTimedSamples();
std::vector<SamplesStatsCounter::StatsSample> out(view.begin(), view.end());
std::sort(out.begin(), out.end(),
[](const SamplesStatsCounter::StatsSample& a,
const SamplesStatsCounter::StatsSample& b) {
return a.time < b.time;
});
std::stable_sort(out.begin(), out.end(),
[](const SamplesStatsCounter::StatsSample& a,
const SamplesStatsCounter::StatsSample& b) {
return a.time < b.time;
});
return out;
}

View File

@ -14,6 +14,7 @@
#include <limits>
#include <string>
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/testsupport/rtc_expect_death.h"
@ -162,11 +163,7 @@ TEST_F(PerfTest, TestGetPerfResultsHistogramsWithStatsCounter) {
// histogram writer itself).
EXPECT_EQ(hist.unit().unit(), proto::MS_BEST_FIT_FORMAT);
EXPECT_EQ(hist.sample_values_size(), 5);
EXPECT_EQ(hist.sample_values(0), 1);
EXPECT_EQ(hist.sample_values(1), 2);
EXPECT_EQ(hist.sample_values(2), 3);
EXPECT_EQ(hist.sample_values(3), 4);
EXPECT_EQ(hist.sample_values(4), 5);
EXPECT_THAT(hist.sample_values(), testing::ElementsAre(1, 2, 3, 4, 5));
EXPECT_EQ(hist.diagnostics().diagnostic_map().count("stories"), 1u);
const proto::Diagnostic& stories =