llvm/unittests/ADT/StatisticTest.cpp
Daniel Sanders 0caccbe952 Make STATISTIC() values available programmatically
Summary:
It can be useful for tools to be able to retrieve the values of variables
declared via STATISTIC() directly without having to emit them and parse
them back. Use cases include:
* Needing to report specific statistics to a test harness
* Wanting to post-process statistics. For example, to produce a percentage of
  functions that were fully selected by GlobalISel

Make this possible by adding llvm::GetStatistics() which returns an
iterator_range that can be used to inspect the statistics that have been
touched during execution. When statistics are disabled (NDEBUG and not
LLVM_ENABLE_STATISTICS) this method will return an empty range.

This patch doesn't address the effect of multiple compilations within the same
process. In such situations, the statistics will be cumulative for all
compilations up to the GetStatistics() call.

Reviewers: qcolombet, rtereshin, aditya_nandakumar, bogner

Reviewed By: rtereshin, bogner

Subscribers: llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D43901

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326723 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-05 17:41:45 +00:00

107 lines
2.3 KiB
C++

//===- llvm/unittest/ADT/StatisticTest.cpp - Statistic unit tests ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
#define DEBUG_TYPE "unittest"
STATISTIC(Counter, "Counts things");
STATISTIC(Counter2, "Counts other things");
TEST(StatisticTest, Count) {
EnableStatistics();
Counter = 0;
EXPECT_EQ(Counter, 0u);
Counter++;
Counter++;
#if LLVM_ENABLE_STATS
EXPECT_EQ(Counter, 2u);
#else
EXPECT_EQ(Counter, 0u);
#endif
}
TEST(StatisticTest, Assign) {
EnableStatistics();
Counter = 2;
#if LLVM_ENABLE_STATS
EXPECT_EQ(Counter, 2u);
#else
EXPECT_EQ(Counter, 0u);
#endif
}
TEST(StatisticTest, API) {
EnableStatistics();
Counter = 0;
EXPECT_EQ(Counter, 0u);
Counter++;
Counter++;
#if LLVM_ENABLE_STATS
EXPECT_EQ(Counter, 2u);
#else
EXPECT_EQ(Counter, 0u);
#endif
#if LLVM_ENABLE_STATS
const auto Range1 = GetStatistics();
EXPECT_NE(Range1.begin(), Range1.end());
EXPECT_EQ(Range1.begin() + 1, Range1.end());
Optional<std::pair<StringRef, unsigned>> S1;
Optional<std::pair<StringRef, unsigned>> S2;
for (const auto &S : Range1) {
if (std::string(S.first) == "Counter")
S1 = S;
if (std::string(S.first) == "Counter2")
S2 = S;
}
EXPECT_NE(S1.hasValue(), false);
EXPECT_EQ(S2.hasValue(), false);
// Counter2 will be registered when it's first touched.
Counter2++;
const auto Range2 = GetStatistics();
EXPECT_NE(Range2.begin(), Range2.end());
EXPECT_EQ(Range2.begin() + 2, Range2.end());
S1 = None;
S2 = None;
for (const auto &S : Range2) {
if (std::string(S.first) == "Counter")
S1 = S;
if (std::string(S.first) == "Counter2")
S2 = S;
}
EXPECT_NE(S1.hasValue(), false);
EXPECT_NE(S2.hasValue(), false);
EXPECT_EQ(S1->first, "Counter");
EXPECT_EQ(S1->second, 2u);
EXPECT_EQ(S2->first, "Counter2");
EXPECT_EQ(S2->second, 1u);
#else
Counter2++;
auto &Range = GetStatistics();
EXPECT_EQ(Range.begin(), Range.end());
#endif
}
} // end anonymous namespace