summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorNathan Slingerland <slingn@gmail.com>2015-12-10 17:21:42 +0000
committerNathan Slingerland <slingn@gmail.com>2015-12-10 17:21:42 +0000
commit46cf0f07dcf34cfe59bfcfe7fb8b5a5797e786e6 (patch)
tree76f4a2157a66ec24fa21ba553d6f5a46be29e604 /unittests
parent7845ed025cc5f41530cb43b9144ec4556bf69128 (diff)
[ProfileData] Add unit test infrastructure for sample profile reader/writer
Summary: Adds support for in-memory round-trip of sample profile data along with basic round trip unit tests. This will also make it easier to include unit tests for future changes to sample profiling. Reviewers: davidxl, dnovillo, silvas Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D15211 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255264 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/ProfileData/CMakeLists.txt1
-rw-r--r--unittests/ProfileData/SampleProfTest.cpp102
2 files changed, 103 insertions, 0 deletions
diff --git a/unittests/ProfileData/CMakeLists.txt b/unittests/ProfileData/CMakeLists.txt
index 79137c9510a..011f8c58179 100644
--- a/unittests/ProfileData/CMakeLists.txt
+++ b/unittests/ProfileData/CMakeLists.txt
@@ -7,4 +7,5 @@ set(LLVM_LINK_COMPONENTS
add_llvm_unittest(ProfileDataTests
CoverageMappingTest.cpp
InstrProfTest.cpp
+ SampleProfTest.cpp
)
diff --git a/unittests/ProfileData/SampleProfTest.cpp b/unittests/ProfileData/SampleProfTest.cpp
new file mode 100644
index 00000000000..aa1144d7913
--- /dev/null
+++ b/unittests/ProfileData/SampleProfTest.cpp
@@ -0,0 +1,102 @@
+//===- unittest/ProfileData/SampleProfTest.cpp -------------------*- C++
+//-*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ProfileData/SampleProfReader.h"
+#include "llvm/ProfileData/SampleProfWriter.h"
+#include "gtest/gtest.h"
+
+#include <cstdarg>
+
+using namespace llvm;
+using namespace sampleprof;
+
+static ::testing::AssertionResult NoError(std::error_code EC) {
+ if (!EC)
+ return ::testing::AssertionSuccess();
+ return ::testing::AssertionFailure() << "error " << EC.value() << ": "
+ << EC.message();
+}
+
+namespace {
+
+struct SampleProfTest : ::testing::Test {
+ std::string Data;
+ std::unique_ptr<raw_ostream> OS;
+ std::unique_ptr<SampleProfileWriter> Writer;
+ std::unique_ptr<SampleProfileReader> Reader;
+
+ SampleProfTest()
+ : Data(), OS(new raw_string_ostream(Data)), Writer(), Reader() {}
+
+ void createWriter(SampleProfileFormat Format) {
+ auto WriterOrErr = SampleProfileWriter::create(OS, Format);
+ ASSERT_TRUE(NoError(WriterOrErr.getError()));
+ Writer = std::move(WriterOrErr.get());
+ }
+
+ void readProfile(std::unique_ptr<MemoryBuffer> &Profile) {
+ auto ReaderOrErr = SampleProfileReader::create(Profile, getGlobalContext());
+ ASSERT_TRUE(NoError(ReaderOrErr.getError()));
+ Reader = std::move(ReaderOrErr.get());
+ }
+
+ void testRoundTrip(SampleProfileFormat Format) {
+ createWriter(Format);
+
+ StringRef FooName("_Z3fooi");
+ FunctionSamples FooSamples;
+ FooSamples.addTotalSamples(7711);
+ FooSamples.addHeadSamples(610);
+ FooSamples.addBodySamples(1, 0, 610);
+
+ StringRef BarName("_Z3bari");
+ FunctionSamples BarSamples;
+ BarSamples.addTotalSamples(20301);
+ BarSamples.addHeadSamples(1437);
+ BarSamples.addBodySamples(1, 0, 1437);
+
+ StringMap<FunctionSamples> Profiles;
+ Profiles[FooName] = std::move(FooSamples);
+ Profiles[BarName] = std::move(BarSamples);
+
+ std::error_code EC;
+ EC = Writer->write(Profiles);
+ ASSERT_TRUE(NoError(EC));
+
+ Writer->getOutputStream().flush();
+
+ auto Profile = MemoryBuffer::getMemBufferCopy(Data);
+ readProfile(Profile);
+
+ EC = Reader->read();
+ ASSERT_TRUE(NoError(EC));
+
+ StringMap<FunctionSamples> &ReadProfiles = Reader->getProfiles();
+ ASSERT_EQ(2u, ReadProfiles.size());
+
+ FunctionSamples &ReadFooSamples = ReadProfiles[FooName];
+ ASSERT_EQ(7711u, ReadFooSamples.getTotalSamples());
+ ASSERT_EQ(610u, ReadFooSamples.getHeadSamples());
+
+ FunctionSamples &ReadBarSamples = ReadProfiles[BarName];
+ ASSERT_EQ(20301u, ReadBarSamples.getTotalSamples());
+ ASSERT_EQ(1437u, ReadBarSamples.getHeadSamples());
+ }
+};
+
+TEST_F(SampleProfTest, roundtrip_text_profile) {
+ testRoundTrip(SampleProfileFormat::SPF_Text);
+}
+
+TEST_F(SampleProfTest, roundtrip_binary_profile) {
+ testRoundTrip(SampleProfileFormat::SPF_Binary);
+}
+
+} // end anonymous namespace