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 "gmock/gmock.h" 20 #include "gtest/gtest.h" 21 22 using ::testing::AllOf; 23 using ::testing::Eq; 24 using ::testing::get; 25 using ::testing::Pointwise; 26 using ::testing::Property; 27 28 namespace llvm { 29 namespace exegesis { 30 31 bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) { 32 return std::tie(A.Key, A.PerInstructionValue, A.PerSnippetValue) == 33 std::tie(B.Key, B.PerInstructionValue, B.PerSnippetValue); 34 } 35 36 static std::string Dump(const MCInst &McInst) { 37 std::string Buffer; 38 raw_string_ostream OS(Buffer); 39 McInst.print(OS); 40 return Buffer; 41 } 42 43 MATCHER(EqMCInst, "") { 44 const std::string Lhs = Dump(get<0>(arg)); 45 const std::string Rhs = Dump(get<1>(arg)); 46 if (Lhs != Rhs) { 47 *result_listener << Lhs << " <=> " << Rhs; 48 return false; 49 } 50 return true; 51 } 52 53 namespace { 54 55 class BenchmarkResultTest : public MipsTestBase {}; 56 57 TEST_F(BenchmarkResultTest, WriteToAndReadFromDisk) { 58 ExitOnError ExitOnErr; 59 60 InstructionBenchmark ToDisk; 61 62 ToDisk.Key.Instructions.push_back(MCInstBuilder(Mips::XOR) 63 .addReg(Mips::T0) 64 .addReg(Mips::T1) 65 .addReg(Mips::T2)); 66 ToDisk.Key.Config = "config"; 67 ToDisk.Key.RegisterInitialValues = { 68 RegisterValue{Mips::T1, APInt(8, "123", 10)}, 69 RegisterValue{Mips::T2, APInt(8, "456", 10)}}; 70 ToDisk.Mode = InstructionBenchmark::Latency; 71 ToDisk.CpuName = "cpu_name"; 72 ToDisk.LLVMTriple = "llvm_triple"; 73 ToDisk.NumRepetitions = 1; 74 ToDisk.Measurements.push_back(BenchmarkMeasure{"a", 1, 1}); 75 ToDisk.Measurements.push_back(BenchmarkMeasure{"b", 2, 2}); 76 ToDisk.Error = "error"; 77 ToDisk.Info = "info"; 78 79 SmallString<64> Filename; 80 std::error_code EC; 81 EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename); 82 ASSERT_FALSE(EC); 83 sys::path::append(Filename, "data.yaml"); 84 errs() << Filename << "-------\n"; 85 ExitOnErr(ToDisk.writeYaml(State, Filename)); 86 87 { 88 // One-element version. 89 const auto FromDisk = 90 ExitOnErr(InstructionBenchmark::readYaml(State, Filename)); 91 92 EXPECT_THAT(FromDisk.Key.Instructions, 93 Pointwise(EqMCInst(), ToDisk.Key.Instructions)); 94 EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config); 95 EXPECT_EQ(FromDisk.Mode, ToDisk.Mode); 96 EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName); 97 EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple); 98 EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions); 99 EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements); 100 EXPECT_THAT(FromDisk.Error, ToDisk.Error); 101 EXPECT_EQ(FromDisk.Info, ToDisk.Info); 102 } 103 { 104 // Vector version. 105 const auto FromDiskVector = 106 ExitOnErr(InstructionBenchmark::readYamls(State, Filename)); 107 ASSERT_EQ(FromDiskVector.size(), size_t{1}); 108 const auto FromDisk = FromDiskVector[0]; 109 EXPECT_THAT(FromDisk.Key.Instructions, 110 Pointwise(EqMCInst(), ToDisk.Key.Instructions)); 111 EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config); 112 EXPECT_EQ(FromDisk.Mode, ToDisk.Mode); 113 EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName); 114 EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple); 115 EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions); 116 EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements); 117 EXPECT_THAT(FromDisk.Error, ToDisk.Error); 118 EXPECT_EQ(FromDisk.Info, ToDisk.Info); 119 } 120 } 121 122 TEST_F(BenchmarkResultTest, PerInstructionStats) { 123 PerInstructionStats Stats; 124 Stats.push(BenchmarkMeasure{"a", 0.5, 0.0}); 125 Stats.push(BenchmarkMeasure{"a", 1.5, 0.0}); 126 Stats.push(BenchmarkMeasure{"a", -1.0, 0.0}); 127 Stats.push(BenchmarkMeasure{"a", 0.0, 0.0}); 128 EXPECT_EQ(Stats.min(), -1.0); 129 EXPECT_EQ(Stats.max(), 1.5); 130 EXPECT_EQ(Stats.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4 131 } 132 } // namespace 133 } // namespace exegesis 134 } // namespace llvm 135