1 //===-- BenchmarkResultTest.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "BenchmarkResult.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/YAMLTraits.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
19 using ::testing::AllOf
;
22 using ::testing::Pointwise
;
23 using ::testing::Property
;
27 bool operator==(const BenchmarkMeasure
&A
, const BenchmarkMeasure
&B
) {
28 return std::tie(A
.Key
, A
.Value
) == std::tie(B
.Key
, B
.Value
);
31 static std::string
Dump(const llvm::MCInst
&McInst
) {
33 llvm::raw_string_ostream
OS(Buffer
);
38 MATCHER(EqMCInst
, "") {
39 const std::string Lhs
= Dump(get
<0>(arg
));
40 const std::string Rhs
= Dump(get
<1>(arg
));
42 *result_listener
<< Lhs
<< " <=> " << Rhs
;
50 static constexpr const unsigned kInstrId
= 5;
51 static constexpr const char kInstrName
[] = "Instruction5";
52 static constexpr const unsigned kReg1Id
= 1;
53 static constexpr const char kReg1Name
[] = "Reg1";
54 static constexpr const unsigned kReg2Id
= 2;
55 static constexpr const char kReg2Name
[] = "Reg2";
57 TEST(BenchmarkResultTest
, WriteToAndReadFromDisk
) {
58 llvm::ExitOnError ExitOnErr
;
59 BenchmarkResultContext Ctx
;
60 Ctx
.addInstrEntry(kInstrId
, kInstrName
);
61 Ctx
.addRegEntry(kReg1Id
, kReg1Name
);
62 Ctx
.addRegEntry(kReg2Id
, kReg2Name
);
64 InstructionBenchmark ToDisk
;
66 ToDisk
.Key
.Instructions
.push_back(llvm::MCInstBuilder(kInstrId
)
71 ToDisk
.Key
.Config
= "config";
72 ToDisk
.Mode
= InstructionBenchmark::Latency
;
73 ToDisk
.CpuName
= "cpu_name";
74 ToDisk
.LLVMTriple
= "llvm_triple";
75 ToDisk
.NumRepetitions
= 1;
76 ToDisk
.Measurements
.push_back(BenchmarkMeasure
{"a", 1, "debug a"});
77 ToDisk
.Measurements
.push_back(BenchmarkMeasure
{"b", 2, ""});
78 ToDisk
.Error
= "error";
81 llvm::SmallString
<64> Filename
;
83 EC
= llvm::sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename
);
85 llvm::sys::path::append(Filename
, "data.yaml");
86 ExitOnErr(ToDisk
.writeYaml(Ctx
, Filename
));
89 // One-element version.
91 ExitOnErr(InstructionBenchmark::readYaml(Ctx
, Filename
));
93 EXPECT_THAT(FromDisk
.Key
.Instructions
,
94 Pointwise(EqMCInst(), ToDisk
.Key
.Instructions
));
95 EXPECT_EQ(FromDisk
.Key
.Config
, ToDisk
.Key
.Config
);
96 EXPECT_EQ(FromDisk
.Mode
, ToDisk
.Mode
);
97 EXPECT_EQ(FromDisk
.CpuName
, ToDisk
.CpuName
);
98 EXPECT_EQ(FromDisk
.LLVMTriple
, ToDisk
.LLVMTriple
);
99 EXPECT_EQ(FromDisk
.NumRepetitions
, ToDisk
.NumRepetitions
);
100 EXPECT_THAT(FromDisk
.Measurements
, ToDisk
.Measurements
);
101 EXPECT_THAT(FromDisk
.Error
, ToDisk
.Error
);
102 EXPECT_EQ(FromDisk
.Info
, ToDisk
.Info
);
106 const auto FromDiskVector
=
107 ExitOnErr(InstructionBenchmark::readYamls(Ctx
, Filename
));
108 ASSERT_EQ(FromDiskVector
.size(), size_t{1});
109 const auto FromDisk
= FromDiskVector
[0];
110 EXPECT_THAT(FromDisk
.Key
.Instructions
,
111 Pointwise(EqMCInst(), ToDisk
.Key
.Instructions
));
112 EXPECT_EQ(FromDisk
.Key
.Config
, ToDisk
.Key
.Config
);
113 EXPECT_EQ(FromDisk
.Mode
, ToDisk
.Mode
);
114 EXPECT_EQ(FromDisk
.CpuName
, ToDisk
.CpuName
);
115 EXPECT_EQ(FromDisk
.LLVMTriple
, ToDisk
.LLVMTriple
);
116 EXPECT_EQ(FromDisk
.NumRepetitions
, ToDisk
.NumRepetitions
);
117 EXPECT_THAT(FromDisk
.Measurements
, ToDisk
.Measurements
);
118 EXPECT_THAT(FromDisk
.Error
, ToDisk
.Error
);
119 EXPECT_EQ(FromDisk
.Info
, ToDisk
.Info
);
123 TEST(BenchmarkResultTest
, BenchmarkMeasureStats
) {
124 BenchmarkMeasureStats Stats
;
125 Stats
.push(BenchmarkMeasure
{"a", 0.5, "debug a"});
126 Stats
.push(BenchmarkMeasure
{"a", 1.5, "debug a"});
127 Stats
.push(BenchmarkMeasure
{"a", -1.0, "debug a"});
128 Stats
.push(BenchmarkMeasure
{"a", 0.0, "debug a"});
129 EXPECT_EQ(Stats
.min(), -1.0);
130 EXPECT_EQ(Stats
.max(), 1.5);
131 EXPECT_EQ(Stats
.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4
134 } // namespace exegesis