1 //===-- BenchmarkResultTest.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
18 
19 using ::testing::AllOf;
20 using ::testing::Eq;
21 using ::testing::get;
22 using ::testing::Pointwise;
23 using ::testing::Property;
24 
25 namespace exegesis {
26 
operator ==(const BenchmarkMeasure & A,const BenchmarkMeasure & B)27 bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) {
28   return std::tie(A.Key, A.Value) == std::tie(B.Key, B.Value);
29 }
30 
Dump(const llvm::MCInst & McInst)31 static std::string Dump(const llvm::MCInst &McInst) {
32   std::string Buffer;
33   llvm::raw_string_ostream OS(Buffer);
34   McInst.print(OS);
35   return Buffer;
36 }
37 
38 MATCHER(EqMCInst, "") {
39   const std::string Lhs = Dump(get<0>(arg));
40   const std::string Rhs = Dump(get<1>(arg));
41   if (Lhs != Rhs) {
42     *result_listener << Lhs << " <=> " << Rhs;
43     return false;
44   }
45   return true;
46 }
47 
48 namespace {
49 
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";
56 
TEST(BenchmarkResultTest,WriteToAndReadFromDisk)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);
63 
64   InstructionBenchmark ToDisk;
65 
66   ToDisk.Key.Instructions.push_back(llvm::MCInstBuilder(kInstrId)
67                                         .addReg(kReg1Id)
68                                         .addReg(kReg2Id)
69                                         .addImm(123)
70                                         .addFPImm(0.5));
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";
79   ToDisk.Info = "info";
80 
81   llvm::SmallString<64> Filename;
82   std::error_code EC;
83   EC = llvm::sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
84   ASSERT_FALSE(EC);
85   llvm::sys::path::append(Filename, "data.yaml");
86   ExitOnErr(ToDisk.writeYaml(Ctx, Filename));
87 
88   {
89     // One-element version.
90     const auto FromDisk =
91         ExitOnErr(InstructionBenchmark::readYaml(Ctx, Filename));
92 
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);
103   }
104   {
105     // Vector version.
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);
120   }
121 }
122 
TEST(BenchmarkResultTest,BenchmarkMeasureStats)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
132 }
133 } // namespace
134 } // namespace exegesis
135