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 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 ExitOnErr(ToDisk.writeYaml(State, Filename)); 82 83 { 84 // One-element version. 85 const auto FromDisk = 86 ExitOnErr(InstructionBenchmark::readYaml(State, Filename)); 87 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); 98 } 99 { 100 // Vector version. 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); 115 } 116 } 117 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 127 } 128 } // namespace 129 } // namespace exegesis 130 } // namespace llvm 131