| 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/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" |
| 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 |
|
| 34 |
static std::string Dump(const MCInst &McInst) { |
| 35 |
std::string Buffer; |
| 36 |
raw_string_ostream OS(Buffer); |
| 37 |
McInst.print(OS); |
| 38 |
return Buffer; |
| 39 |
} |
| 40 |
|
| 41 |
MATCHER(EqMCInst, "") { |
| 42 |
const std::string Lhs = Dump(get<0>(arg)); |
| 43 |
const std::string Rhs = Dump(get<1>(arg)); |
| 44 |
if (Lhs != Rhs) { |
| 45 |
*result_listener << Lhs << " <=> " << Rhs; |
| 46 |
return false; |
| 47 |
} |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
namespace { |
| 52 |
|
| 53 |
class MipsBenchmarkResultTest : public MipsTestBase {}; |
| 54 |
|
| 55 |
TEST_F(MipsBenchmarkResultTest, WriteToAndReadFromDisk) { |
| 56 |
ExitOnError ExitOnErr; |
| 57 |
|
| 58 |
InstructionBenchmark ToDisk; |
| 59 |
|
| 60 |
ToDisk.Key.Instructions.push_back(MCInstBuilder(Mips::XOR) |
| 61 |
.addReg(Mips::T0) |
| 62 |
.addReg(Mips::T1) |
| 63 |
.addReg(Mips::T2)); |
| 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"; |
| 75 |
ToDisk.Info = "info"; |
| 76 |
|
| 77 |
TempDir TestDirectory("BenchmarkResultTestDir", /*Unique*/ true); |
| 78 |
SmallString<64> Filename(TestDirectory.path()); |
| 79 |
sys::path::append(Filename, "data.yaml"); |
| 80 |
errs() << Filename << "-------\n"; |
| 81 |
{ |
| 82 |
int ResultFD = 0; |
| 83 |
// Create output file or open existing file and truncate it, once. |
| 84 |
ExitOnErr(errorCodeToError(openFileForWrite(Filename, ResultFD, |
| 85 |
sys::fs::CD_CreateAlways, |
| 86 |
sys::fs::OF_TextWithCRLF))); |
| 87 |
raw_fd_ostream FileOstr(ResultFD, true /*shouldClose*/); |
| 88 |
|
| 89 |
ExitOnErr(ToDisk.writeYamlTo(State, FileOstr)); |
| 90 |
} |
| 91 |
const std::unique_ptr<MemoryBuffer> Buffer = |
| 92 |
std::move(*MemoryBuffer::getFile(Filename)); |
| 93 |
|
| 94 |
{ |
| 95 |
// One-element version. |
| 96 |
const auto FromDisk = |
| 97 |
ExitOnErr(InstructionBenchmark::readYaml(State, *Buffer)); |
| 98 |
|
| 99 |
EXPECT_THAT(FromDisk.Key.Instructions, |
| 100 |
Pointwise(EqMCInst(), ToDisk.Key.Instructions)); |
| 101 |
EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config); |
| 102 |
EXPECT_EQ(FromDisk.Mode, ToDisk.Mode); |
| 103 |
EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName); |
| 104 |
EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple); |
| 105 |
EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions); |
| 106 |
EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements); |
| 107 |
EXPECT_THAT(FromDisk.Error, ToDisk.Error); |
| 108 |
EXPECT_EQ(FromDisk.Info, ToDisk.Info); |
| 109 |
} |
| 110 |
{ |
| 111 |
// Vector version. |
| 112 |
const auto FromDiskVector = |
| 113 |
ExitOnErr(InstructionBenchmark::readYamls(State, *Buffer)); |
| 114 |
ASSERT_EQ(FromDiskVector.size(), size_t{1}); |
| 115 |
const auto &FromDisk = FromDiskVector[0]; |
| 116 |
EXPECT_THAT(FromDisk.Key.Instructions, |
| 117 |
Pointwise(EqMCInst(), ToDisk.Key.Instructions)); |
| 118 |
EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config); |
| 119 |
EXPECT_EQ(FromDisk.Mode, ToDisk.Mode); |
| 120 |
EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName); |
| 121 |
EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple); |
| 122 |
EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions); |
| 123 |
EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements); |
| 124 |
EXPECT_THAT(FromDisk.Error, ToDisk.Error); |
| 125 |
EXPECT_EQ(FromDisk.Info, ToDisk.Info); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
TEST_F(MipsBenchmarkResultTest, PerInstructionStats) { |
| 130 |
PerInstructionStats Stats; |
| 131 |
Stats.push(BenchmarkMeasure{"a", 0.5, 0.0}); |
| 132 |
Stats.push(BenchmarkMeasure{"a", 1.5, 0.0}); |
| 133 |
Stats.push(BenchmarkMeasure{"a", -1.0, 0.0}); |
| 134 |
Stats.push(BenchmarkMeasure{"a", 0.0, 0.0}); |
| 135 |
EXPECT_EQ(Stats.min(), -1.0); |
| 136 |
EXPECT_EQ(Stats.max(), 1.5); |
| 137 |
EXPECT_EQ(Stats.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4 |
| 138 |
} |
| 139 |
} // namespace |
| 140 |
} // namespace exegesis |
| 141 |
} // namespace llvm |