150
|
1 #include "LibcBenchmark.h"
|
|
2 #include "llvm/ADT/ArrayRef.h"
|
|
3 #include "llvm/ADT/Optional.h"
|
|
4 #include "llvm/ADT/SmallVector.h"
|
|
5 #include "gmock/gmock.h"
|
|
6 #include "gtest/gtest.h"
|
|
7 #include <chrono>
|
|
8 #include <limits>
|
|
9 #include <queue>
|
|
10 #include <vector>
|
|
11
|
|
12 using std::chrono::nanoseconds;
|
|
13 using ::testing::ElementsAre;
|
|
14 using ::testing::Field;
|
|
15 using ::testing::IsEmpty;
|
|
16 using ::testing::SizeIs;
|
|
17
|
|
18 namespace llvm {
|
|
19 namespace libc_benchmarks {
|
|
20 namespace {
|
|
21
|
|
22 // A simple parameter provider returning a zero initialized vector of size
|
|
23 // `iterations`.
|
|
24 struct DummyParameterProvider {
|
|
25 std::vector<char> generateBatch(size_t iterations) {
|
|
26 return std::vector<char>(iterations);
|
|
27 }
|
|
28 };
|
|
29
|
|
30 class LibcBenchmark : public ::testing::Test {
|
|
31 public:
|
|
32 // A Clock interface suitable for testing.
|
|
33 // - Either it returns 0,
|
|
34 // - Or a timepoint coming from the `setMeasurements` call.
|
|
35 Duration now() {
|
|
36 if (!MaybeTimepoints)
|
|
37 return {};
|
|
38 assert(!MaybeTimepoints->empty());
|
|
39 const Duration timepoint = MaybeTimepoints->front();
|
|
40 MaybeTimepoints->pop();
|
|
41 return timepoint;
|
|
42 }
|
|
43
|
|
44 protected:
|
|
45 void SetUp() override { Options.Log = BenchmarkLog::Full; }
|
|
46
|
|
47 void TearDown() override {
|
|
48 // We make sure all the expected measurements were performed.
|
|
49 if (MaybeTimepoints)
|
|
50 EXPECT_THAT(*MaybeTimepoints, IsEmpty());
|
|
51 }
|
|
52
|
|
53 BenchmarkResult run() {
|
|
54 return benchmark(Options, ParameterProvider, DummyFunction, *this);
|
|
55 }
|
|
56
|
|
57 void setMeasurements(llvm::ArrayRef<Duration> Durations) {
|
|
58 MaybeTimepoints.emplace(); // Create the optional value.
|
|
59 Duration CurrentTime = nanoseconds(1);
|
|
60 for (const auto &Duration : Durations) {
|
|
61 MaybeTimepoints->push(CurrentTime);
|
|
62 CurrentTime += Duration;
|
|
63 MaybeTimepoints->push(CurrentTime);
|
|
64 CurrentTime += nanoseconds(1);
|
|
65 }
|
|
66 }
|
|
67
|
|
68 BenchmarkOptions Options;
|
|
69
|
|
70 private:
|
|
71 DummyParameterProvider ParameterProvider;
|
|
72 static char DummyFunction(char Payload) { return Payload; }
|
|
73 llvm::Optional<std::queue<Duration>> MaybeTimepoints;
|
|
74 };
|
|
75
|
|
76 TEST_F(LibcBenchmark, MaxSamplesReached) {
|
|
77 Options.MaxSamples = 1;
|
|
78 const auto Result = run();
|
|
79 EXPECT_THAT(Result.MaybeBenchmarkLog->size(), 1);
|
|
80 EXPECT_THAT(Result.TerminationStatus, BenchmarkStatus::MaxSamplesReached);
|
|
81 }
|
|
82
|
|
83 TEST_F(LibcBenchmark, MaxDurationReached) {
|
|
84 Options.MaxDuration = nanoseconds(10);
|
|
85 setMeasurements({nanoseconds(11)});
|
|
86 const auto Result = run();
|
|
87 EXPECT_THAT(Result.MaybeBenchmarkLog->size(), 1);
|
|
88 EXPECT_THAT(Result.TerminationStatus, BenchmarkStatus::MaxDurationReached);
|
|
89 }
|
|
90
|
|
91 TEST_F(LibcBenchmark, MaxIterationsReached) {
|
|
92 Options.InitialIterations = 1;
|
|
93 Options.MaxIterations = 20;
|
|
94 Options.ScalingFactor = 2;
|
|
95 Options.Epsilon = 0; // unreachable.
|
|
96 const auto Result = run();
|
|
97 EXPECT_THAT(*Result.MaybeBenchmarkLog,
|
|
98 ElementsAre(Field(&BenchmarkState::LastSampleIterations, 1),
|
|
99 Field(&BenchmarkState::LastSampleIterations, 2),
|
|
100 Field(&BenchmarkState::LastSampleIterations, 4),
|
|
101 Field(&BenchmarkState::LastSampleIterations, 8),
|
|
102 Field(&BenchmarkState::LastSampleIterations, 16),
|
|
103 Field(&BenchmarkState::LastSampleIterations, 32)));
|
|
104 EXPECT_THAT(Result.MaybeBenchmarkLog->size(), 6);
|
|
105 EXPECT_THAT(Result.TerminationStatus, BenchmarkStatus::MaxIterationsReached);
|
|
106 }
|
|
107
|
|
108 TEST_F(LibcBenchmark, MinSamples) {
|
|
109 Options.MinSamples = 4;
|
|
110 Options.ScalingFactor = 2;
|
|
111 Options.Epsilon = std::numeric_limits<double>::max(); // always reachable.
|
|
112 setMeasurements(
|
|
113 {nanoseconds(1), nanoseconds(2), nanoseconds(4), nanoseconds(8)});
|
|
114 const auto Result = run();
|
|
115 EXPECT_THAT(*Result.MaybeBenchmarkLog,
|
|
116 ElementsAre(Field(&BenchmarkState::LastSampleIterations, 1),
|
|
117 Field(&BenchmarkState::LastSampleIterations, 2),
|
|
118 Field(&BenchmarkState::LastSampleIterations, 4),
|
|
119 Field(&BenchmarkState::LastSampleIterations, 8)));
|
|
120 EXPECT_THAT(Result.MaybeBenchmarkLog->size(), 4);
|
|
121 EXPECT_THAT(Result.TerminationStatus, BenchmarkStatus::PrecisionReached);
|
|
122 }
|
|
123
|
|
124 TEST_F(LibcBenchmark, Epsilon) {
|
|
125 Options.MinSamples = 4;
|
|
126 Options.ScalingFactor = 2;
|
|
127 Options.Epsilon = std::numeric_limits<double>::max(); // always reachable.
|
|
128 setMeasurements(
|
|
129 {nanoseconds(1), nanoseconds(2), nanoseconds(4), nanoseconds(8)});
|
|
130 const auto Result = run();
|
|
131 EXPECT_THAT(*Result.MaybeBenchmarkLog,
|
|
132 ElementsAre(Field(&BenchmarkState::LastSampleIterations, 1),
|
|
133 Field(&BenchmarkState::LastSampleIterations, 2),
|
|
134 Field(&BenchmarkState::LastSampleIterations, 4),
|
|
135 Field(&BenchmarkState::LastSampleIterations, 8)));
|
|
136 EXPECT_THAT(Result.MaybeBenchmarkLog->size(), 4);
|
|
137 EXPECT_THAT(Result.TerminationStatus, BenchmarkStatus::PrecisionReached);
|
|
138 }
|
|
139
|
|
140 TEST(ArrayRefLoop, Cycle) {
|
|
141 std::array<int, 2> array = {1, 2};
|
|
142 EXPECT_THAT(cycle(array, 0), ElementsAre());
|
|
143 EXPECT_THAT(cycle(array, 1), ElementsAre(1));
|
|
144 EXPECT_THAT(cycle(array, 2), ElementsAre(1, 2));
|
|
145 EXPECT_THAT(cycle(array, 3), ElementsAre(1, 2, 1));
|
|
146 EXPECT_THAT(cycle(array, 4), ElementsAre(1, 2, 1, 2));
|
|
147 EXPECT_THAT(cycle(array, 5), ElementsAre(1, 2, 1, 2, 1));
|
|
148 }
|
|
149
|
|
150 TEST(ByteConstrainedArray, Simple) {
|
|
151 EXPECT_THAT((ByteConstrainedArray<char, 17>()), SizeIs(17));
|
|
152 EXPECT_THAT((ByteConstrainedArray<uint16_t, 17>()), SizeIs(8));
|
|
153 EXPECT_THAT((ByteConstrainedArray<uint32_t, 17>()), SizeIs(4));
|
|
154 EXPECT_THAT((ByteConstrainedArray<uint64_t, 17>()), SizeIs(2));
|
|
155
|
|
156 EXPECT_LE(sizeof(ByteConstrainedArray<char, 17>), 17U);
|
|
157 EXPECT_LE(sizeof(ByteConstrainedArray<uint16_t, 17>), 17U);
|
|
158 EXPECT_LE(sizeof(ByteConstrainedArray<uint32_t, 17>), 17U);
|
|
159 EXPECT_LE(sizeof(ByteConstrainedArray<uint64_t, 17>), 17U);
|
|
160 }
|
|
161
|
|
162 TEST(ByteConstrainedArray, Cycle) {
|
|
163 ByteConstrainedArray<uint64_t, 17> TwoValues{{1UL, 2UL}};
|
|
164 EXPECT_THAT(cycle(TwoValues, 5), ElementsAre(1, 2, 1, 2, 1));
|
|
165 }
|
|
166 } // namespace
|
|
167 } // namespace libc_benchmarks
|
|
168 } // namespace llvm
|