File: ResultAnalyzerTest.cpp

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (170 lines) | stat: -rw-r--r-- 7,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//===--  Automemcpy Json Results Analyzer Test ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "automemcpy/ResultAnalyzer.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using testing::ElementsAre;
using testing::Pair;
using testing::SizeIs;

namespace llvm {
namespace automemcpy {
namespace {

TEST(AutomemcpyJsonResultsAnalyzer, getThroughputsOneSample) {
  static constexpr FunctionId Foo1 = {"memcpy1", FunctionType::MEMCPY};
  static constexpr DistributionId DistA = {{"A"}};
  static constexpr SampleId Id = {Foo1, DistA};
  static constexpr Sample kSamples[] = {
      Sample{Id, 4},
  };

  const std::vector<FunctionData> Data = getThroughputs(kSamples);
  EXPECT_THAT(Data, SizeIs(1));
  EXPECT_THAT(Data[0].Id, Foo1);
  EXPECT_THAT(Data[0].PerDistributionData, SizeIs(1));
  // A single value is provided.
  EXPECT_THAT(
      Data[0].PerDistributionData.lookup(DistA.Name).MedianBytesPerSecond, 4);
}

TEST(AutomemcpyJsonResultsAnalyzer, getThroughputsManySamplesSameBucket) {
  static constexpr FunctionId Foo1 = {"memcpy1", FunctionType::MEMCPY};
  static constexpr DistributionId DistA = {{"A"}};
  static constexpr SampleId Id = {Foo1, DistA};
  static constexpr Sample kSamples[] = {Sample{Id, 4}, Sample{Id, 5},
                                        Sample{Id, 5}};

  const std::vector<FunctionData> Data = getThroughputs(kSamples);
  EXPECT_THAT(Data, SizeIs(1));
  EXPECT_THAT(Data[0].Id, Foo1);
  EXPECT_THAT(Data[0].PerDistributionData, SizeIs(1));
  // When multiple values are provided we pick the median one (here median of 4,
  // 5, 5).
  EXPECT_THAT(
      Data[0].PerDistributionData.lookup(DistA.Name).MedianBytesPerSecond, 5);
}

TEST(AutomemcpyJsonResultsAnalyzer, getThroughputsServeralFunctionAndDist) {
  static constexpr FunctionId Foo1 = {"memcpy1", FunctionType::MEMCPY};
  static constexpr DistributionId DistA = {{"A"}};
  static constexpr FunctionId Foo2 = {"memcpy2", FunctionType::MEMCPY};
  static constexpr DistributionId DistB = {{"B"}};
  static constexpr Sample kSamples[] = {
      Sample{{Foo1, DistA}, 1}, Sample{{Foo1, DistB}, 2},
      Sample{{Foo2, DistA}, 3}, Sample{{Foo2, DistB}, 4}};
  // Data is aggregated per function.
  const std::vector<FunctionData> Data = getThroughputs(kSamples);
  EXPECT_THAT(Data, SizeIs(2)); // 2 functions Foo1 and Foo2.
  // Each function has data for both distributions DistA and DistB.
  EXPECT_THAT(Data[0].PerDistributionData, SizeIs(2));
  EXPECT_THAT(Data[1].PerDistributionData, SizeIs(2));
}

TEST(AutomemcpyJsonResultsAnalyzer, getScore) {
  static constexpr FunctionId Foo1 = {"memcpy1", FunctionType::MEMCPY};
  static constexpr FunctionId Foo2 = {"memcpy2", FunctionType::MEMCPY};
  static constexpr FunctionId Foo3 = {"memcpy3", FunctionType::MEMCPY};
  static constexpr DistributionId Dist = {{"A"}};
  static constexpr Sample kSamples[] = {Sample{{Foo1, Dist}, 1},
                                        Sample{{Foo2, Dist}, 2},
                                        Sample{{Foo3, Dist}, 3}};

  // Data is aggregated per function.
  std::vector<FunctionData> Data = getThroughputs(kSamples);

  // Sort Data by function name so we can test them.
  std::sort(
      Data.begin(), Data.end(),
      [](const FunctionData &A, const FunctionData &B) { return A.Id < B.Id; });

  EXPECT_THAT(Data[0].Id, Foo1);
  EXPECT_THAT(Data[0].PerDistributionData.lookup("A").MedianBytesPerSecond, 1);
  EXPECT_THAT(Data[1].Id, Foo2);
  EXPECT_THAT(Data[1].PerDistributionData.lookup("A").MedianBytesPerSecond, 2);
  EXPECT_THAT(Data[2].Id, Foo3);
  EXPECT_THAT(Data[2].PerDistributionData.lookup("A").MedianBytesPerSecond, 3);

  // Normalizes throughput per distribution.
  fillScores(Data);
  EXPECT_THAT(Data[0].PerDistributionData.lookup("A").Score, 0);
  EXPECT_THAT(Data[1].PerDistributionData.lookup("A").Score, 0.5);
  EXPECT_THAT(Data[2].PerDistributionData.lookup("A").Score, 1);
}

TEST(AutomemcpyJsonResultsAnalyzer, castVotes) {
  static constexpr double kAbsErr = 0.01;

  static constexpr FunctionId Foo1 = {"memcpy1", FunctionType::MEMCPY};
  static constexpr FunctionId Foo2 = {"memcpy2", FunctionType::MEMCPY};
  static constexpr FunctionId Foo3 = {"memcpy3", FunctionType::MEMCPY};
  static constexpr DistributionId DistA = {{"A"}};
  static constexpr DistributionId DistB = {{"B"}};
  static constexpr Sample kSamples[] = {
      Sample{{Foo1, DistA}, 0}, Sample{{Foo1, DistB}, 30},
      Sample{{Foo2, DistA}, 1}, Sample{{Foo2, DistB}, 100},
      Sample{{Foo3, DistA}, 7}, Sample{{Foo3, DistB}, 100},
  };

  // DistA Thoughput ranges from 0 to 7.
  // DistB Thoughput ranges from 30 to 100.

  // Data is aggregated per function.
  std::vector<FunctionData> Data = getThroughputs(kSamples);

  // Sort Data by function name so we can test them.
  std::sort(
      Data.begin(), Data.end(),
      [](const FunctionData &A, const FunctionData &B) { return A.Id < B.Id; });

  // Normalizes throughput per distribution.
  fillScores(Data);

  // Cast votes
  castVotes(Data);

  EXPECT_THAT(Data[0].Id, Foo1);
  EXPECT_THAT(Data[1].Id, Foo2);
  EXPECT_THAT(Data[2].Id, Foo3);

  // Distribution A
  // Throughput is 0, 1 and 7, so normalized scores are 0, 1/7 and 1.
  EXPECT_NEAR(Data[0].PerDistributionData.lookup("A").Score, 0, kAbsErr);
  EXPECT_NEAR(Data[1].PerDistributionData.lookup("A").Score, 1. / 7, kAbsErr);
  EXPECT_NEAR(Data[2].PerDistributionData.lookup("A").Score, 1, kAbsErr);
  // which are turned into grades BAD,  MEDIOCRE and EXCELLENT.
  EXPECT_THAT(Data[0].PerDistributionData.lookup("A").Grade, Grade::BAD);
  EXPECT_THAT(Data[1].PerDistributionData.lookup("A").Grade, Grade::MEDIOCRE);
  EXPECT_THAT(Data[2].PerDistributionData.lookup("A").Grade, Grade::EXCELLENT);

  // Distribution B
  // Throughput is 30, 100 and 100, so normalized scores are 0, 1 and 1.
  EXPECT_NEAR(Data[0].PerDistributionData.lookup("B").Score, 0, kAbsErr);
  EXPECT_NEAR(Data[1].PerDistributionData.lookup("B").Score, 1, kAbsErr);
  EXPECT_NEAR(Data[2].PerDistributionData.lookup("B").Score, 1, kAbsErr);
  // which are turned into grades BAD, EXCELLENT and EXCELLENT.
  EXPECT_THAT(Data[0].PerDistributionData.lookup("B").Grade, Grade::BAD);
  EXPECT_THAT(Data[1].PerDistributionData.lookup("B").Grade, Grade::EXCELLENT);
  EXPECT_THAT(Data[2].PerDistributionData.lookup("B").Grade, Grade::EXCELLENT);

  // Now looking from the functions point of view.
  // Note the array is indexed by GradeEnum values (EXCELLENT=0 / BAD = 6)
  EXPECT_THAT(Data[0].GradeHisto, ElementsAre(0, 0, 0, 0, 0, 0, 2));
  EXPECT_THAT(Data[1].GradeHisto, ElementsAre(1, 0, 0, 0, 0, 1, 0));
  EXPECT_THAT(Data[2].GradeHisto, ElementsAre(2, 0, 0, 0, 0, 0, 0));

  EXPECT_THAT(Data[0].FinalGrade, Grade::BAD);
  EXPECT_THAT(Data[1].FinalGrade, Grade::MEDIOCRE);
  EXPECT_THAT(Data[2].FinalGrade, Grade::EXCELLENT);
}

} // namespace
} // namespace automemcpy
} // namespace llvm