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 "MipsInstrInfo.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/MC/TargetRegistry.h"
14 #include "llvm/Support/Error.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 "llvm/Testing/Support/SupportHelpers.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
23 using ::testing::AllOf
;
26 using ::testing::Pointwise
;
27 using ::testing::Property
;
29 using llvm::unittest::TempDir
;
34 static std::string
Dump(const MCInst
&McInst
) {
36 raw_string_ostream
OS(Buffer
);
41 MATCHER(EqMCInst
, "") {
42 const std::string Lhs
= Dump(get
<0>(arg
));
43 const std::string Rhs
= Dump(get
<1>(arg
));
45 *result_listener
<< Lhs
<< " <=> " << Rhs
;
53 class MipsBenchmarkResultTest
: public MipsTestBase
{};
55 TEST_F(MipsBenchmarkResultTest
, WriteToAndReadFromDisk
) {
56 ExitOnError ExitOnErr
;
58 InstructionBenchmark ToDisk
;
60 ToDisk
.Key
.Instructions
.push_back(MCInstBuilder(Mips::XOR
)
64 ToDisk
.Key
.Config
= "config";
65 ToDisk
.Key
.RegisterInitialValues
= {
66 RegisterValue
{Mips::T1
, APInt(8, "123", 10)},
67 RegisterValue
{Mips::T2
, APInt(8, "456", 10)}};
68 ToDisk
.Mode
= InstructionBenchmark::Latency
;
69 ToDisk
.CpuName
= "cpu_name";
70 ToDisk
.LLVMTriple
= "llvm_triple";
71 ToDisk
.NumRepetitions
= 1;
72 ToDisk
.Measurements
.push_back(BenchmarkMeasure
{"a", 1, 1});
73 ToDisk
.Measurements
.push_back(BenchmarkMeasure
{"b", 2, 2});
74 ToDisk
.Error
= "error";
77 TempDir
TestDirectory("BenchmarkResultTestDir", /*Unique*/ true);
78 SmallString
<64> Filename(TestDirectory
.path());
79 sys::path::append(Filename
, "data.yaml");
80 errs() << Filename
<< "-------\n";
81 ExitOnErr(ToDisk
.writeYaml(State
, Filename
));
84 // One-element version.
86 ExitOnErr(InstructionBenchmark::readYaml(State
, Filename
));
88 EXPECT_THAT(FromDisk
.Key
.Instructions
,
89 Pointwise(EqMCInst(), ToDisk
.Key
.Instructions
));
90 EXPECT_EQ(FromDisk
.Key
.Config
, ToDisk
.Key
.Config
);
91 EXPECT_EQ(FromDisk
.Mode
, ToDisk
.Mode
);
92 EXPECT_EQ(FromDisk
.CpuName
, ToDisk
.CpuName
);
93 EXPECT_EQ(FromDisk
.LLVMTriple
, ToDisk
.LLVMTriple
);
94 EXPECT_EQ(FromDisk
.NumRepetitions
, ToDisk
.NumRepetitions
);
95 EXPECT_THAT(FromDisk
.Measurements
, ToDisk
.Measurements
);
96 EXPECT_THAT(FromDisk
.Error
, ToDisk
.Error
);
97 EXPECT_EQ(FromDisk
.Info
, ToDisk
.Info
);
101 const auto FromDiskVector
=
102 ExitOnErr(InstructionBenchmark::readYamls(State
, Filename
));
103 ASSERT_EQ(FromDiskVector
.size(), size_t{1});
104 const auto FromDisk
= FromDiskVector
[0];
105 EXPECT_THAT(FromDisk
.Key
.Instructions
,
106 Pointwise(EqMCInst(), ToDisk
.Key
.Instructions
));
107 EXPECT_EQ(FromDisk
.Key
.Config
, ToDisk
.Key
.Config
);
108 EXPECT_EQ(FromDisk
.Mode
, ToDisk
.Mode
);
109 EXPECT_EQ(FromDisk
.CpuName
, ToDisk
.CpuName
);
110 EXPECT_EQ(FromDisk
.LLVMTriple
, ToDisk
.LLVMTriple
);
111 EXPECT_EQ(FromDisk
.NumRepetitions
, ToDisk
.NumRepetitions
);
112 EXPECT_THAT(FromDisk
.Measurements
, ToDisk
.Measurements
);
113 EXPECT_THAT(FromDisk
.Error
, ToDisk
.Error
);
114 EXPECT_EQ(FromDisk
.Info
, ToDisk
.Info
);
118 TEST_F(MipsBenchmarkResultTest
, PerInstructionStats
) {
119 PerInstructionStats Stats
;
120 Stats
.push(BenchmarkMeasure
{"a", 0.5, 0.0});
121 Stats
.push(BenchmarkMeasure
{"a", 1.5, 0.0});
122 Stats
.push(BenchmarkMeasure
{"a", -1.0, 0.0});
123 Stats
.push(BenchmarkMeasure
{"a", 0.0, 0.0});
124 EXPECT_EQ(Stats
.min(), -1.0);
125 EXPECT_EQ(Stats
.max(), 1.5);
126 EXPECT_EQ(Stats
.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4
129 } // namespace exegesis