tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

stdout_metrics_exporter_test.cc (9224B)


      1 /*
      2 *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 #include "api/test/metrics/stdout_metrics_exporter.h"
     11 
     12 #include <map>
     13 #include <string>
     14 #include <vector>
     15 
     16 #include "absl/flags/flag.h"
     17 #include "api/test/metrics/metric.h"
     18 #include "api/units/timestamp.h"
     19 #include "test/gtest.h"
     20 #include "test/test_flags.h"
     21 
     22 namespace webrtc {
     23 namespace test {
     24 namespace {
     25 
     26 using ::testing::TestWithParam;
     27 
     28 std::map<std::string, std::string> DefaultMetadata() {
     29  return std::map<std::string, std::string>{{"key", "value"}};
     30 }
     31 
     32 Metric::TimeSeries::Sample Sample(double value) {
     33  return Metric::TimeSeries::Sample{.timestamp = Timestamp::Seconds(1),
     34                                    .value = value,
     35                                    .sample_metadata = DefaultMetadata()};
     36 }
     37 
     38 Metric PsnrForTestFoo(double mean, double stddev) {
     39  return Metric{.name = "psnr",
     40                .unit = Unit::kUnitless,
     41                .improvement_direction = ImprovementDirection::kBiggerIsBetter,
     42                .test_case = "foo",
     43                .time_series = Metric::TimeSeries{},
     44                .stats = Metric::Stats{.mean = mean, .stddev = stddev}};
     45 }
     46 
     47 class StdoutMetricsExporterTest : public ::testing::Test {
     48 public:
     49  StdoutMetricsExporterTest() {
     50    original_isolated_script_test_perf_output_ =
     51        absl::GetFlag(FLAGS_isolated_script_test_perf_output);
     52 
     53    metric1_ = {
     54        .name = "test_metric1",
     55        .unit = Unit::kMilliseconds,
     56        .improvement_direction = ImprovementDirection::kBiggerIsBetter,
     57        .test_case = "test_case_name1",
     58        .metric_metadata =
     59            std::map<std::string, std::string>{{"video_stream", "alice_stream"},
     60                                               {"sender", "alice"},
     61                                               {"receiver", "bob"}},
     62        .time_series =
     63            Metric::TimeSeries{.samples = std::vector{Sample(10), Sample(20)}},
     64        .stats = Metric::Stats{
     65            .mean = 15.0, .stddev = 5.0, .min = 10.0, .max = 20.0}};
     66    metric2_ = {
     67        .name = "test_metric2",
     68        .unit = Unit::kKilobitsPerSecond,
     69        .improvement_direction = ImprovementDirection::kSmallerIsBetter,
     70        .test_case = "test_case_name2",
     71        .metric_metadata =
     72            std::map<std::string, std::string>{{"peer", "alice"}},
     73        .time_series =
     74            Metric::TimeSeries{.samples = std::vector{Sample(20), Sample(40)}},
     75        .stats = Metric::Stats{
     76            .mean = 30.0, .stddev = 10.0, .min = 20.0, .max = 40.0}};
     77  }
     78  ~StdoutMetricsExporterTest() override {
     79    absl::SetFlag(&FLAGS_isolated_script_test_perf_output,
     80                  original_isolated_script_test_perf_output_);
     81  }
     82 
     83 protected:
     84  std::string original_isolated_script_test_perf_output_;
     85  Metric metric1_;
     86  Metric metric2_;
     87 };
     88 
     89 TEST_F(StdoutMetricsExporterTest,
     90       ExportMetricFormatCorrectWhenIsolatedScriptTestPerfOutputIsSet) {
     91  absl::SetFlag(&FLAGS_isolated_script_test_perf_output, "/tmp/foo");
     92 
     93  testing::internal::CaptureStdout();
     94  StdoutMetricsExporter exporter;
     95 
     96  std::string expected =
     97      "RESULT: test_case_name1 / test_metric1= "
     98      "{mean=15, stddev=5} Milliseconds (BiggerIsBetter)\n"
     99      "RESULT: test_case_name2 / test_metric2= "
    100      "{mean=30, stddev=10} KilobitsPerSecond (SmallerIsBetter)\n";
    101 
    102  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric1_, metric2_}));
    103  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    104 }
    105 
    106 TEST_F(StdoutMetricsExporterTest,
    107       ExportMetricFormatCorrectWhenIsolatedScriptTestPerfOutputIsNotSet) {
    108  absl::SetFlag(&FLAGS_isolated_script_test_perf_output, "");
    109 
    110  testing::internal::CaptureStdout();
    111  StdoutMetricsExporter exporter;
    112 
    113  std::string expected =
    114      "RESULT: test_case_name1/alice_stream_alice_bob / test_metric1= "
    115      "{mean=15, stddev=5} Milliseconds (BiggerIsBetter)\n"
    116      "RESULT: test_case_name2/alice / test_metric2= "
    117      "{mean=30, stddev=10} KilobitsPerSecond (SmallerIsBetter)\n";
    118 
    119  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric1_, metric2_}));
    120  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    121 }
    122 
    123 TEST(StdoutMetricsExporterNumberFormatTest, PositiveNumberMaxPrecision) {
    124  testing::internal::CaptureStdout();
    125  StdoutMetricsExporter exporter;
    126 
    127  Metric metric = PsnrForTestFoo(15.00000001, 0.00000001);
    128  std::string expected =
    129      "RESULT: foo / psnr= "
    130      "{mean=15.00000001, stddev=0.00000001} Unitless (BiggerIsBetter)\n";
    131  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    132  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    133 }
    134 
    135 TEST(StdoutMetricsExporterNumberFormatTest,
    136     PositiveNumberTrailingZeroNotAdded) {
    137  testing::internal::CaptureStdout();
    138  StdoutMetricsExporter exporter;
    139 
    140  Metric metric = PsnrForTestFoo(15.12345, 0.12);
    141  std::string expected =
    142      "RESULT: foo / psnr= "
    143      "{mean=15.12345, stddev=0.12} Unitless (BiggerIsBetter)\n";
    144  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    145  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    146 }
    147 
    148 TEST(StdoutMetricsExporterNumberFormatTest,
    149     PositiveNumberTrailingZeroAreRemoved) {
    150  testing::internal::CaptureStdout();
    151  StdoutMetricsExporter exporter;
    152 
    153  Metric metric = PsnrForTestFoo(15.123450000, 0.120000000);
    154  std::string expected =
    155      "RESULT: foo / psnr= "
    156      "{mean=15.12345, stddev=0.12} Unitless (BiggerIsBetter)\n";
    157  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    158  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    159 }
    160 
    161 TEST(StdoutMetricsExporterNumberFormatTest,
    162     PositiveNumberRoundsUpOnPrecisionCorrectly) {
    163  testing::internal::CaptureStdout();
    164  StdoutMetricsExporter exporter;
    165 
    166  Metric metric = PsnrForTestFoo(15.000000009, 0.999999999);
    167  std::string expected =
    168      "RESULT: foo / psnr= "
    169      "{mean=15.00000001, stddev=1} Unitless (BiggerIsBetter)\n";
    170  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    171  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    172 }
    173 
    174 TEST(StdoutMetricsExporterNumberFormatTest,
    175     PositiveNumberRoundsDownOnPrecisionCorrectly) {
    176  testing::internal::CaptureStdout();
    177  StdoutMetricsExporter exporter;
    178 
    179  Metric metric = PsnrForTestFoo(15.0000000049, 0.9999999949);
    180  std::string expected =
    181      "RESULT: foo / psnr= "
    182      "{mean=15, stddev=0.99999999} Unitless (BiggerIsBetter)\n";
    183  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    184  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    185 }
    186 
    187 TEST(StdoutMetricsExporterNumberFormatTest, NegativeNumberMaxPrecision) {
    188  testing::internal::CaptureStdout();
    189  StdoutMetricsExporter exporter;
    190 
    191  Metric metric = PsnrForTestFoo(-15.00000001, -0.00000001);
    192  std::string expected =
    193      "RESULT: foo / psnr= "
    194      "{mean=-15.00000001, stddev=-0.00000001} Unitless (BiggerIsBetter)\n";
    195  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    196  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    197 }
    198 
    199 TEST(StdoutMetricsExporterNumberFormatTest,
    200     NegativeNumberTrailingZeroNotAdded) {
    201  testing::internal::CaptureStdout();
    202  StdoutMetricsExporter exporter;
    203 
    204  Metric metric = PsnrForTestFoo(-15.12345, -0.12);
    205  std::string expected =
    206      "RESULT: foo / psnr= "
    207      "{mean=-15.12345, stddev=-0.12} Unitless (BiggerIsBetter)\n";
    208  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    209  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    210 }
    211 
    212 TEST(StdoutMetricsExporterNumberFormatTest,
    213     NegativeNumberTrailingZeroAreRemoved) {
    214  testing::internal::CaptureStdout();
    215  StdoutMetricsExporter exporter;
    216 
    217  Metric metric = PsnrForTestFoo(-15.123450000, -0.120000000);
    218  std::string expected =
    219      "RESULT: foo / psnr= "
    220      "{mean=-15.12345, stddev=-0.12} Unitless (BiggerIsBetter)\n";
    221  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    222  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    223 }
    224 
    225 TEST(StdoutMetricsExporterNumberFormatTest,
    226     NegativeNumberRoundsUpOnPrecisionCorrectly) {
    227  testing::internal::CaptureStdout();
    228  StdoutMetricsExporter exporter;
    229 
    230  Metric metric = PsnrForTestFoo(-15.000000009, -0.999999999);
    231  std::string expected =
    232      "RESULT: foo / psnr= "
    233      "{mean=-15.00000001, stddev=-1} Unitless (BiggerIsBetter)\n";
    234  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    235  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    236 }
    237 
    238 TEST(StdoutMetricsExporterNumberFormatTest,
    239     NegativeNumberRoundsDownOnPrecisionCorrectly) {
    240  testing::internal::CaptureStdout();
    241  StdoutMetricsExporter exporter;
    242 
    243  Metric metric = PsnrForTestFoo(-15.0000000049, -0.9999999949);
    244  std::string expected =
    245      "RESULT: foo / psnr= "
    246      "{mean=-15, stddev=-0.99999999} Unitless (BiggerIsBetter)\n";
    247  EXPECT_TRUE(exporter.Export(std::vector<Metric>{metric}));
    248  EXPECT_EQ(expected, testing::internal::GetCapturedStdout());
    249 }
    250 
    251 }  // namespace
    252 }  // namespace test
    253 }  // namespace webrtc