blob: b23938a62029a5dfdf1670f456c499f302dcf51a [file] [log] [blame]
Clement Courbet96715412018-05-07 09:09:48 +00001//===-- ClusteringTest.cpp --------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Clement Courbet96715412018-05-07 09:09:48 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Clustering.h"
10#include "BenchmarkResult.h"
11#include "llvm/Support/Error.h"
12#include "llvm/Support/raw_ostream.h"
13#include "gmock/gmock.h"
14#include "gtest/gtest.h"
15
Fangrui Song32401af2018-10-22 17:10:47 +000016namespace llvm {
Clement Courbet96715412018-05-07 09:09:48 +000017namespace exegesis {
18
19namespace {
20
21using testing::Field;
22using testing::UnorderedElementsAre;
23using testing::UnorderedElementsAreArray;
24
Clement Courbetc08b26e2019-03-22 13:37:39 +000025static const auto HasPoints = [](const std::vector<int> &Indices) {
Clement Courbet28550772019-03-22 13:13:12 +000026 return Field(&InstructionBenchmarkClustering::Cluster::PointIndices,
27 UnorderedElementsAreArray(Indices));
28};
29
Clement Courbet96715412018-05-07 09:09:48 +000030TEST(ClusteringTest, Clusters3D) {
31 std::vector<InstructionBenchmark> Points(6);
32
33 // Cluster around (x=0, y=1, z=2): points {0, 3}.
Clement Courbet684a5f62018-09-26 08:37:21 +000034 Points[0].Measurements = {
Clement Courbet28d4f852018-09-26 13:35:10 +000035 {"x", 0.01, 0.0}, {"y", 1.02, 0.0}, {"z", 1.98, 0.0}};
Clement Courbet684a5f62018-09-26 08:37:21 +000036 Points[3].Measurements = {
Clement Courbet28d4f852018-09-26 13:35:10 +000037 {"x", -0.01, 0.0}, {"y", 1.02, 0.0}, {"z", 1.98, 0.0}};
Clement Courbet96715412018-05-07 09:09:48 +000038 // Cluster around (x=1, y=1, z=2): points {1, 4}.
Clement Courbet684a5f62018-09-26 08:37:21 +000039 Points[1].Measurements = {
Clement Courbet28d4f852018-09-26 13:35:10 +000040 {"x", 1.01, 0.0}, {"y", 1.02, 0.0}, {"z", 1.98, 0.0}};
Clement Courbet684a5f62018-09-26 08:37:21 +000041 Points[4].Measurements = {
Clement Courbet28d4f852018-09-26 13:35:10 +000042 {"x", 0.99, 0.0}, {"y", 1.02, 0.0}, {"z", 1.98, 0.0}};
Clement Courbet96715412018-05-07 09:09:48 +000043 // Cluster around (x=0, y=0, z=0): points {5}, marked as noise.
Clement Courbet684a5f62018-09-26 08:37:21 +000044 Points[5].Measurements = {
Clement Courbet28d4f852018-09-26 13:35:10 +000045 {"x", 0.0, 0.0}, {"y", 0.01, 0.0}, {"z", -0.02, 0.0}};
Clement Courbet96715412018-05-07 09:09:48 +000046 // Error cluster: points {2}
47 Points[2].Error = "oops";
48
Roman Lebedevc2423fe2019-03-28 08:55:01 +000049 auto Clustering = InstructionBenchmarkClustering::create(
50 Points, InstructionBenchmarkClustering::ModeE::Dbscan, 2, 0.25);
Clement Courbet96715412018-05-07 09:09:48 +000051 ASSERT_TRUE((bool)Clustering);
52 EXPECT_THAT(Clustering.get().getValidClusters(),
53 UnorderedElementsAre(HasPoints({0, 3}), HasPoints({1, 4})));
54 EXPECT_THAT(Clustering.get().getCluster(
55 InstructionBenchmarkClustering::ClusterId::noise()),
56 HasPoints({5}));
57 EXPECT_THAT(Clustering.get().getCluster(
58 InstructionBenchmarkClustering::ClusterId::error()),
59 HasPoints({2}));
60
61 EXPECT_EQ(Clustering.get().getClusterIdForPoint(2),
62 InstructionBenchmarkClustering::ClusterId::error());
63 EXPECT_EQ(Clustering.get().getClusterIdForPoint(5),
64 InstructionBenchmarkClustering::ClusterId::noise());
65 EXPECT_EQ(Clustering.get().getClusterIdForPoint(0),
66 Clustering.get().getClusterIdForPoint(3));
67 EXPECT_EQ(Clustering.get().getClusterIdForPoint(1),
68 Clustering.get().getClusterIdForPoint(4));
69}
70
71TEST(ClusteringTest, Clusters3D_InvalidSize) {
72 std::vector<InstructionBenchmark> Points(6);
Clement Courbet684a5f62018-09-26 08:37:21 +000073 Points[0].Measurements = {
Clement Courbet28d4f852018-09-26 13:35:10 +000074 {"x", 0.01, 0.0}, {"y", 1.02, 0.0}, {"z", 1.98, 0.0}};
75 Points[1].Measurements = {{"y", 1.02, 0.0}, {"z", 1.98, 0.0}};
Clement Courbet96715412018-05-07 09:09:48 +000076 auto Error =
Roman Lebedevc2423fe2019-03-28 08:55:01 +000077 InstructionBenchmarkClustering::create(
78 Points, InstructionBenchmarkClustering::ModeE::Dbscan, 2, 0.25)
79 .takeError();
Clement Courbet96715412018-05-07 09:09:48 +000080 ASSERT_TRUE((bool)Error);
81 consumeError(std::move(Error));
82}
83
84TEST(ClusteringTest, Clusters3D_InvalidOrder) {
85 std::vector<InstructionBenchmark> Points(6);
Clement Courbet28d4f852018-09-26 13:35:10 +000086 Points[0].Measurements = {{"x", 0.01, 0.0}, {"y", 1.02, 0.0}};
87 Points[1].Measurements = {{"y", 1.02, 0.0}, {"x", 1.98, 0.0}};
Clement Courbet96715412018-05-07 09:09:48 +000088 auto Error =
Roman Lebedevc2423fe2019-03-28 08:55:01 +000089 InstructionBenchmarkClustering::create(
90 Points, InstructionBenchmarkClustering::ModeE::Dbscan, 2, 0.25)
91 .takeError();
Clement Courbet96715412018-05-07 09:09:48 +000092 ASSERT_TRUE((bool)Error);
93 consumeError(std::move(Error));
94}
95
Clement Courbet17d3c252018-05-22 13:31:29 +000096TEST(ClusteringTest, Ordering) {
97 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::makeValid(1),
98 InstructionBenchmarkClustering::ClusterId::makeValid(2));
99
100 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::makeValid(2),
101 InstructionBenchmarkClustering::ClusterId::noise());
102
103 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::makeValid(2),
104 InstructionBenchmarkClustering::ClusterId::error());
105
106 ASSERT_LT(InstructionBenchmarkClustering::ClusterId::noise(),
107 InstructionBenchmarkClustering::ClusterId::error());
108}
109
Clement Courbet28550772019-03-22 13:13:12 +0000110TEST(ClusteringTest, Ordering1) {
111 std::vector<InstructionBenchmark> Points(3);
112
113 Points[0].Measurements = {
114 {"x", 0.0, 0.0}};
115 Points[1].Measurements = {
116 {"x", 1.0, 0.0}};
117 Points[2].Measurements = {
118 {"x", 2.0, 0.0}};
119
Roman Lebedevc2423fe2019-03-28 08:55:01 +0000120 auto Clustering = InstructionBenchmarkClustering::create(
121 Points, InstructionBenchmarkClustering::ModeE::Dbscan, 2, 1.1);
Clement Courbet28550772019-03-22 13:13:12 +0000122 ASSERT_TRUE((bool)Clustering);
123 EXPECT_THAT(Clustering.get().getValidClusters(),
124 UnorderedElementsAre(HasPoints({0, 1, 2})));
125}
126
127TEST(ClusteringTest, Ordering2) {
128 std::vector<InstructionBenchmark> Points(3);
129
130 Points[0].Measurements = {
131 {"x", 0.0, 0.0}};
132 Points[1].Measurements = {
133 {"x", 2.0, 0.0}};
134 Points[2].Measurements = {
135 {"x", 1.0, 0.0}};
136
Roman Lebedevc2423fe2019-03-28 08:55:01 +0000137 auto Clustering = InstructionBenchmarkClustering::create(
138 Points, InstructionBenchmarkClustering::ModeE::Dbscan, 2, 1.1);
Clement Courbet28550772019-03-22 13:13:12 +0000139 ASSERT_TRUE((bool)Clustering);
140 EXPECT_THAT(Clustering.get().getValidClusters(),
141 UnorderedElementsAre(HasPoints({0, 1, 2})));
142}
143
Clement Courbet96715412018-05-07 09:09:48 +0000144} // namespace
145} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000146} // namespace llvm