[obj2yaml] - Fix a comment. NFC.
[llvm-complete.git] / unittests / tools / llvm-exegesis / X86 / BenchmarkResultTest.cpp
blob517d3ac5acb0b352fb41e51d236986d35b4c823d
1 //===-- BenchmarkResultTest.cpp ---------------------------------*- C++ -*-===//
2 //
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
6 //
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;
22 using ::testing::Eq;
23 using ::testing::get;
24 using ::testing::Pointwise;
25 using ::testing::Property;
27 namespace llvm {
28 namespace exegesis {
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) {
36 std::string Buffer;
37 raw_string_ostream OS(Buffer);
38 McInst.print(OS);
39 return Buffer;
42 MATCHER(EqMCInst, "") {
43 const std::string Lhs = Dump(get<0>(arg));
44 const std::string Rhs = Dump(get<1>(arg));
45 if (Lhs != Rhs) {
46 *result_listener << Lhs << " <=> " << Rhs;
47 return false;
49 return true;
52 namespace {
54 TEST(BenchmarkResultTest, WriteToAndReadFromDisk) {
55 LLVMInitializeX86TargetInfo();
56 LLVMInitializeX86Target();
57 LLVMInitializeX86TargetMC();
59 // Read benchmarks.
60 const LLVMState State("x86_64-unknown-linux", "haswell");
62 ExitOnError ExitOnErr;
64 InstructionBenchmark ToDisk;
66 ToDisk.Key.Instructions.push_back(MCInstBuilder(X86::XOR32rr)
67 .addReg(X86::AL)
68 .addReg(X86::AH)
69 .addImm(123)
70 .addFPImm(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";
82 ToDisk.Info = "info";
84 SmallString<64> Filename;
85 std::error_code EC;
86 EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
87 ASSERT_FALSE(EC);
88 sys::path::append(Filename, "data.yaml");
89 errs() << Filename << "-------\n";
90 ExitOnErr(ToDisk.writeYaml(State, Filename));
93 // One-element version.
94 const auto FromDisk =
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);
109 // Vector version.
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
137 } // namespace
138 } // namespace exegesis
139 } // namespace llvm