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/MC/TargetRegistry.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "llvm/Support/YAMLTraits.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
22 using ::testing::AllOf
;
25 using ::testing::Pointwise
;
26 using ::testing::Property
;
31 void InitializeX86ExegesisTarget();
33 static std::string
Dump(const MCInst
&McInst
) {
35 raw_string_ostream
OS(Buffer
);
40 MATCHER(EqMCInst
, "") {
41 const std::string Lhs
= Dump(get
<0>(arg
));
42 const std::string Rhs
= Dump(get
<1>(arg
));
44 *result_listener
<< Lhs
<< " <=> " << Rhs
;
52 TEST(BenchmarkResultTest
, WriteToAndReadFromDisk
) {
53 LLVMInitializeX86TargetInfo();
54 LLVMInitializeX86Target();
55 LLVMInitializeX86TargetMC();
56 InitializeX86ExegesisTarget();
59 const LLVMState State
=
60 cantFail(LLVMState::Create("x86_64-unknown-linux", "haswell"));
62 ExitOnError ExitOnErr
;
64 InstructionBenchmark ToDisk
;
66 ToDisk
.Key
.Instructions
.push_back(MCInstBuilder(X86::XOR32rr
)
70 .addDFPImm(bit_cast
<uint64_t>(0.5)));
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