Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/testing/gtest/mozilla/MozGTestBench.cpp
Line
Count
Source (jump to first uncovered line)
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
0
#define MOZ_GTEST_BENCH_FRAMEWORK "platform_microbench"
13
0
#define MOZ_GTEST_NUM_ITERATIONS 5
14
15
using mozilla::TimeStamp;
16
17
namespace mozilla {
18
void GTestBench(const char* aSuite, const char* aName,
19
                const std::function<void()>& aTest)
20
0
{
21
#if defined(DEBUG) || defined(MOZ_ASAN)
22
  // Run the test to make sure that it doesn't fail but don't log
23
  // any measurements since it's not an optimized build.
24
  aTest();
25
#else
26
  bool shouldAlert = bool(getenv("PERFHERDER_ALERTING_ENABLED"));
27
0
  std::vector<int> durations;
28
0
29
0
  for (int i=0; i<MOZ_GTEST_NUM_ITERATIONS; i++) {
30
0
    mozilla::TimeStamp start = TimeStamp::Now();
31
0
32
0
    aTest();
33
0
34
0
    durations.push_back((TimeStamp::Now() - start).ToMicroseconds());
35
0
  }
36
0
37
0
  std::string replicatesStr = "[" + std::to_string(durations[0]);
38
0
  for (int i=1; i<MOZ_GTEST_NUM_ITERATIONS; i++) {
39
0
    replicatesStr += "," + std::to_string(durations[i]);
40
0
  }
41
0
  replicatesStr += "]";
42
0
43
0
  // median is at index floor(i/2) if number of replicates is odd,
44
0
  // (i/2-1) if even
45
0
  std::sort(durations.begin(), durations.end());
46
0
  int medianIndex = (MOZ_GTEST_NUM_ITERATIONS / 2) + ((durations.size() % 2 == 0) ? (-1) : 0);
47
0
48
0
  // Print the result for each test. Let perfherder aggregate for us
49
0
  printf("PERFHERDER_DATA: {\"framework\": {\"name\": \"%s\"}, "
50
0
         "\"suites\": [{\"name\": \"%s\", \"subtests\": "
51
0
         "[{\"name\": \"%s\", \"value\": %i, \"replicates\": %s, "
52
0
         "\"lowerIsBetter\": true, \"shouldAlert\": %s}]"
53
0
         "}]}\n",
54
0
         MOZ_GTEST_BENCH_FRAMEWORK, aSuite, aName, durations[medianIndex],
55
0
         replicatesStr.c_str(), shouldAlert ? "true" : "false");
56
0
#endif
57
0
}
58
59
} // mozilla
60