tor-browser

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

MozGTestBench.cpp (2154B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "MozGTestBench.h"
      7 #include "mozilla/TimeStamp.h"
      8 #include <stdio.h>
      9 #include <string>
     10 #include <vector>
     11 
     12 #define MOZ_GTEST_BENCH_FRAMEWORK "platform_microbench"
     13 #define MOZ_GTEST_NUM_ITERATIONS 5
     14 
     15 namespace mozilla {
     16 void GTestBench(const char* aSuite, const char* aName,
     17                const std::function<void()>& aTest) {
     18 #if defined(DEBUG) || defined(MOZ_ASAN)
     19  // Run the test to make sure that it doesn't fail but don't log
     20  // any measurements since it's not an optimized build.
     21  aTest();
     22 #else
     23  bool shouldAlert = bool(getenv("PERFHERDER_ALERTING_ENABLED"));
     24  std::vector<int> durations;
     25 
     26  int iterations;
     27  if (!getenv("MOZ_GTEST_NUM_ITERATIONS") ||
     28      (iterations = atoi(getenv("MOZ_GTEST_NUM_ITERATIONS"))) == 0)
     29    iterations = MOZ_GTEST_NUM_ITERATIONS;
     30 
     31  for (int i = 0; i < iterations; i++) {
     32    mozilla::TimeStamp start = TimeStamp::Now();
     33 
     34    aTest();
     35 
     36    durations.push_back((TimeStamp::Now() - start).ToMicroseconds());
     37  }
     38 
     39  std::string replicatesStr = "[" + std::to_string(durations[0]);
     40  for (int i = 1; i < iterations; i++) {
     41    replicatesStr += "," + std::to_string(durations[i]);
     42  }
     43  replicatesStr += "]";
     44 
     45  // median is at index floor(i/2) if number of replicates is odd,
     46  // (i/2-1) if even
     47  std::sort(durations.begin(), durations.end());
     48  int medianIndex = (iterations / 2) + ((iterations % 2 == 0) ? (-1) : 0);
     49 
     50  // Print the result for each test. Let perfherder aggregate for us
     51  printf(
     52      "PERFHERDER_DATA: {\"framework\": {\"name\": \"%s\"}, "
     53      "\"suites\": [{\"name\": \"%s\", \"subtests\": "
     54      "[{\"name\": \"%s\", \"value\": %i, \"replicates\": %s, "
     55      "\"lowerIsBetter\": true, \"shouldAlert\": %s}]"
     56      "}]}\n",
     57      MOZ_GTEST_BENCH_FRAMEWORK, aSuite, aName, durations[medianIndex],
     58      replicatesStr.c_str(), shouldAlert ? "true" : "false");
     59 #endif
     60 }
     61 
     62 }  // namespace mozilla