• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===----------------------------------------------------------------------===//
8 
9 #include "BenchmarkResult.h"
10 #include "MipsInstrInfo.h"
11 #include "TestBase.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/TargetRegistry.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "llvm/Support/YAMLTraits.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Testing/Support/SupportHelpers.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 
23 using ::testing::AllOf;
24 using ::testing::Eq;
25 using ::testing::get;
26 using ::testing::Pointwise;
27 using ::testing::Property;
28 
29 using llvm::unittest::TempDir;
30 
31 namespace llvm {
32 namespace exegesis {
33 
operator ==(const BenchmarkMeasure & A,const BenchmarkMeasure & B)34 bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) {
35   return std::tie(A.Key, A.PerInstructionValue, A.PerSnippetValue) ==
36          std::tie(B.Key, B.PerInstructionValue, B.PerSnippetValue);
37 }
38 
Dump(const MCInst & McInst)39 static std::string Dump(const MCInst &McInst) {
40   std::string Buffer;
41   raw_string_ostream OS(Buffer);
42   McInst.print(OS);
43   return Buffer;
44 }
45 
46 MATCHER(EqMCInst, "") {
47   const std::string Lhs = Dump(get<0>(arg));
48   const std::string Rhs = Dump(get<1>(arg));
49   if (Lhs != Rhs) {
50     *result_listener << Lhs << " <=> " << Rhs;
51     return false;
52   }
53   return true;
54 }
55 
56 namespace {
57 
58 class BenchmarkResultTest : public MipsTestBase {};
59 
TEST_F(BenchmarkResultTest,WriteToAndReadFromDisk)60 TEST_F(BenchmarkResultTest, WriteToAndReadFromDisk) {
61   ExitOnError ExitOnErr;
62 
63   InstructionBenchmark ToDisk;
64 
65   ToDisk.Key.Instructions.push_back(MCInstBuilder(Mips::XOR)
66                                         .addReg(Mips::T0)
67                                         .addReg(Mips::T1)
68                                         .addReg(Mips::T2));
69   ToDisk.Key.Config = "config";
70   ToDisk.Key.RegisterInitialValues = {
71       RegisterValue{Mips::T1, APInt(8, "123", 10)},
72       RegisterValue{Mips::T2, APInt(8, "456", 10)}};
73   ToDisk.Mode = InstructionBenchmark::Latency;
74   ToDisk.CpuName = "cpu_name";
75   ToDisk.LLVMTriple = "llvm_triple";
76   ToDisk.NumRepetitions = 1;
77   ToDisk.Measurements.push_back(BenchmarkMeasure{"a", 1, 1});
78   ToDisk.Measurements.push_back(BenchmarkMeasure{"b", 2, 2});
79   ToDisk.Error = "error";
80   ToDisk.Info = "info";
81 
82   TempDir TestDirectory("BenchmarkResultTestDir", /*Unique*/ true);
83   SmallString<64> Filename(TestDirectory.path());
84   sys::path::append(Filename, "data.yaml");
85   errs() << Filename << "-------\n";
86   ExitOnErr(ToDisk.writeYaml(State, Filename));
87 
88   {
89     // One-element version.
90     const auto FromDisk =
91         ExitOnErr(InstructionBenchmark::readYaml(State, 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(State, 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_F(BenchmarkResultTest,PerInstructionStats)123 TEST_F(BenchmarkResultTest, PerInstructionStats) {
124   PerInstructionStats Stats;
125   Stats.push(BenchmarkMeasure{"a", 0.5, 0.0});
126   Stats.push(BenchmarkMeasure{"a", 1.5, 0.0});
127   Stats.push(BenchmarkMeasure{"a", -1.0, 0.0});
128   Stats.push(BenchmarkMeasure{"a", 0.0, 0.0});
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 } // namespace llvm
136