1 //===-- BenchmarkResultTest.cpp ---------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "BenchmarkResult.h"
10 #include "X86InstrInfo.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/TargetRegistry.h"
15 #include "llvm/Support/TargetSelect.h"
16 #include "llvm/Support/YAMLTraits.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
21 using ::testing::AllOf
;
24 using ::testing::Pointwise
;
25 using ::testing::Property
;
30 bool operator==(const BenchmarkMeasure
&A
, const BenchmarkMeasure
&B
) {
31 return std::tie(A
.Key
, A
.PerInstructionValue
, A
.PerSnippetValue
) ==
32 std::tie(B
.Key
, B
.PerInstructionValue
, B
.PerSnippetValue
);
35 static std::string
Dump(const MCInst
&McInst
) {
37 raw_string_ostream
OS(Buffer
);
42 MATCHER(EqMCInst
, "") {
43 const std::string Lhs
= Dump(get
<0>(arg
));
44 const std::string Rhs
= Dump(get
<1>(arg
));
46 *result_listener
<< Lhs
<< " <=> " << Rhs
;
54 TEST(BenchmarkResultTest
, WriteToAndReadFromDisk
) {
55 LLVMInitializeX86TargetInfo();
56 LLVMInitializeX86Target();
57 LLVMInitializeX86TargetMC();
60 const LLVMState
State("x86_64-unknown-linux", "haswell");
62 ExitOnError ExitOnErr
;
64 InstructionBenchmark ToDisk
;
66 ToDisk
.Key
.Instructions
.push_back(MCInstBuilder(X86::XOR32rr
)
71 ToDisk
.Key
.Config
= "config";
72 ToDisk
.Key
.RegisterInitialValues
= {
73 RegisterValue
{X86::AL
, APInt(8, "-1", 10)},
74 RegisterValue
{X86::AH
, APInt(8, "123", 10)}};
75 ToDisk
.Mode
= InstructionBenchmark::Latency
;
76 ToDisk
.CpuName
= "cpu_name";
77 ToDisk
.LLVMTriple
= "llvm_triple";
78 ToDisk
.NumRepetitions
= 1;
79 ToDisk
.Measurements
.push_back(BenchmarkMeasure
{"a", 1, 1});
80 ToDisk
.Measurements
.push_back(BenchmarkMeasure
{"b", 2, 2});
81 ToDisk
.Error
= "error";
84 SmallString
<64> Filename
;
86 EC
= sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename
);
88 sys::path::append(Filename
, "data.yaml");
89 errs() << Filename
<< "-------\n";
90 ExitOnErr(ToDisk
.writeYaml(State
, Filename
));
93 // One-element version.
95 ExitOnErr(InstructionBenchmark::readYaml(State
, Filename
));
97 EXPECT_THAT(FromDisk
.Key
.Instructions
,
98 Pointwise(EqMCInst(), ToDisk
.Key
.Instructions
));
99 EXPECT_EQ(FromDisk
.Key
.Config
, ToDisk
.Key
.Config
);
100 EXPECT_EQ(FromDisk
.Mode
, ToDisk
.Mode
);
101 EXPECT_EQ(FromDisk
.CpuName
, ToDisk
.CpuName
);
102 EXPECT_EQ(FromDisk
.LLVMTriple
, ToDisk
.LLVMTriple
);
103 EXPECT_EQ(FromDisk
.NumRepetitions
, ToDisk
.NumRepetitions
);
104 EXPECT_THAT(FromDisk
.Measurements
, ToDisk
.Measurements
);
105 EXPECT_THAT(FromDisk
.Error
, ToDisk
.Error
);
106 EXPECT_EQ(FromDisk
.Info
, ToDisk
.Info
);
110 const auto FromDiskVector
=
111 ExitOnErr(InstructionBenchmark::readYamls(State
, Filename
));
112 ASSERT_EQ(FromDiskVector
.size(), size_t{1});
113 const auto FromDisk
= FromDiskVector
[0];
114 EXPECT_THAT(FromDisk
.Key
.Instructions
,
115 Pointwise(EqMCInst(), ToDisk
.Key
.Instructions
));
116 EXPECT_EQ(FromDisk
.Key
.Config
, ToDisk
.Key
.Config
);
117 EXPECT_EQ(FromDisk
.Mode
, ToDisk
.Mode
);
118 EXPECT_EQ(FromDisk
.CpuName
, ToDisk
.CpuName
);
119 EXPECT_EQ(FromDisk
.LLVMTriple
, ToDisk
.LLVMTriple
);
120 EXPECT_EQ(FromDisk
.NumRepetitions
, ToDisk
.NumRepetitions
);
121 EXPECT_THAT(FromDisk
.Measurements
, ToDisk
.Measurements
);
122 EXPECT_THAT(FromDisk
.Error
, ToDisk
.Error
);
123 EXPECT_EQ(FromDisk
.Info
, ToDisk
.Info
);
127 TEST(BenchmarkResultTest
, PerInstructionStats
) {
128 PerInstructionStats Stats
;
129 Stats
.push(BenchmarkMeasure
{"a", 0.5, 0.0});
130 Stats
.push(BenchmarkMeasure
{"a", 1.5, 0.0});
131 Stats
.push(BenchmarkMeasure
{"a", -1.0, 0.0});
132 Stats
.push(BenchmarkMeasure
{"a", 0.0, 0.0});
133 EXPECT_EQ(Stats
.min(), -1.0);
134 EXPECT_EQ(Stats
.max(), 1.5);
135 EXPECT_EQ(Stats
.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4
138 } // namespace exegesis