From 97e4b7e2478bca7bc24c3eaf590808f590bd9fa1 Mon Sep 17 00:00:00 2001 From: Pascal Eberlein Date: Thu, 27 Jan 2022 21:43:42 +0100 Subject: [PATCH] Updated benchmarks and added cli output for documentation --- README.md | 23 +++++--- benchmarks.cpp | 145 +++++++++++++++++++++++++--------------------- doc/Benchmarks.md | 48 +++++++++++++++ 3 files changed, 140 insertions(+), 76 deletions(-) create mode 100644 doc/Benchmarks.md diff --git a/README.md b/README.md index 594eb17..4b4c015 100644 --- a/README.md +++ b/README.md @@ -5,28 +5,33 @@
`A header only framework for binary file formats` -## benchmarks +## [benchmarks](doc/Benchmarks.md) ### single insert (append EntryType) | Number | Time (s) | |--------|----------| -| 1k | ~ 1 | -| 10k | ~ 13 | -| 100k | toolong | +| 1k | 1.35 | +| 10k | 12.82 | +| 100k | 128.65 | ### vector insert (append std::vector) -| Number | Time (s) | -|--------|----------| -| 1k | < 0 s | -| 1M | 968ms | +| Number | Time (ms) | +|--------|-----------| +| 1k | 1 | +| 10k | 1 | +| 100k | 6 | +| 1M | 40 | ### read | Number | Time (s) | |--------|----------| -| 1M | 87ms | +| 1k | 0ms | +| 10k | 0ms | +| 100k | 0ms | +| 1M | 8ms | ## Minimal example diff --git a/benchmarks.cpp b/benchmarks.cpp index 833488f..4c1a675 100644 --- a/benchmarks.cpp +++ b/benchmarks.cpp @@ -5,85 +5,96 @@ #include "FunctionTimer/FunctionTimer.h" #include "test_common.h" -// NOLINTNEXTLINE(cert-err58-cpp) -TEST(BinaryFile, test1kInsert) { - auto t = getRandomTestFile(); - std::vector entries; - for (uint32_t i = 0; i < 1000; i++) { - entries.push_back(generateRandomTestEntryContainer().entry); - } - FunctionTimer ft( - [&t, entries]() { EXPECT_EQ(t.append(entries), binfmt::ErrorCode::OK); }); - std::cout << "1k insert: " << ft.getExecutionTimeMs() << "ms" << std::endl; - EXPECT_EQ(t.getFileSize(), - 1000 * sizeof(TestBinaryEntryContainer) + sizeof(TestBinaryHeader)); - cleanupTestFile(t); -} -// NOLINTNEXTLINE(cert-err58-cpp) -TEST(BinaryFile, test10kInsert) { - auto t = getRandomTestFile(); - std::vector entries; - for (uint32_t i = 0; i < 10000; i++) { - entries.push_back(generateRandomTestEntryContainer().entry); - } - FunctionTimer ft( - [&t, entries]() { EXPECT_EQ(t.append(entries), binfmt::ErrorCode::OK); }); - std::cout << "10k insert: " << ft.getExecutionTimeMs() << "ms" << std::endl; - EXPECT_EQ(t.getFileSize(), 10000 * sizeof(TestBinaryEntryContainer) + - sizeof(TestBinaryHeader)); +template +void benchmark_read(FileType* i_pFile, uint32_t i_u32Count, std::vector entries) { + auto t = *i_pFile; int ec = 0; - std::vector allEntries; - EXPECT_TRUE(t.getAllEntries(allEntries)); - for (const auto &entry : allEntries) { - EXPECT_EQ(entry.checksum, TestBinaryEntryContainer(entries[ec]).checksum); + std::vector allContainers; + FunctionTimer ft1( + [&t, &allContainers]() { EXPECT_TRUE(t.getAllEntries(allContainers)); }); + std::cout << i_u32Count << " getAllEntries: " << ft1.getExecutionTimeMs() << "ms" + << std::endl; + + for (const auto &container : allContainers) { + EXPECT_EQ(container.checksum, TestBinaryEntryContainer(entries[ec]).checksum); ec++; } +} + +void testSingleInsert(uint32_t i_u32Count) { + auto t = getRandomTestFile(); + std::vector entries; + + FunctionTimer ft([&t, &entries, i_u32Count]() { + for (uint32_t i = 0; i < i_u32Count; i++) { + auto container = generateRandomTestEntryContainer(); + entries.emplace_back(container); + EXPECT_EQ(t.append(container.entry), + binfmt::ErrorCode::OK); + } + }); + + std::cout << "Single insert of " << i_u32Count << " items took " << ft.getExecutionTimeMs() << "ms" << std::endl; + EXPECT_EQ(t.getFileSize(), i_u32Count * sizeof(TestBinaryEntryContainer) + sizeof(TestBinaryHeader)); + std::cout << "Size: " << t.getFileSize() << std::endl; + + benchmark_read(&t, i_u32Count, entries); + cleanupTestFile(t); } -// NOLINTNEXTLINE(cert-err58-cpp) -TEST(BinaryFile, test100kInsert) { +void testVectorInsert(uint32_t i_u32Count) { auto t = getRandomTestFile(); - std::vector entries; - for (uint32_t i = 0; i < 100000; i++) { - entries.push_back(generateRandomTestEntryContainer().entry); + std::vector entries; + for (uint32_t i = 0; i < i_u32Count; i++) { + entries.push_back(generateRandomTestEntryContainer()); } - FunctionTimer ft( - [&t, entries]() { EXPECT_EQ(t.append(entries), binfmt::ErrorCode::OK); }); - std::cout << "100k insert: " << ft.getExecutionTimeMs() << "ms" << std::endl; - EXPECT_EQ(t.getFileSize(), 100000 * sizeof(TestBinaryEntryContainer) + - sizeof(TestBinaryHeader)); + + FunctionTimer ft([&t, &entries]() { + EXPECT_EQ(t.append(entries), binfmt::ErrorCode::OK); + }); + + std::cout << "Vector insert of " << i_u32Count << " items took " << ft.getExecutionTimeMs() << "ms" << std::endl; + EXPECT_EQ(t.getFileSize(), i_u32Count * sizeof(TestBinaryEntryContainer) + sizeof(TestBinaryHeader)); + std::cout << "Size: " << t.getFileSize() << std::endl; + + benchmark_read(&t, i_u32Count, entries); + cleanupTestFile(t); } // NOLINTNEXTLINE(cert-err58-cpp) -TEST(BinaryFile, test1MInsert) { - auto t = getRandomTestFile(); - std::vector entries; - uint32_t insertCount = 10000000; - std::cout << "Generating file of size " - << std::to_string(insertCount * sizeof(TestBinaryEntryContainer) + - sizeof(TestBinaryHeader)) - << std::endl; - for (uint32_t i = 0; i < insertCount; i++) { - entries.push_back(generateRandomTestEntryContainer().entry); - } - FunctionTimer ft( - [&t, entries]() { EXPECT_EQ(t.append(entries), binfmt::ErrorCode::OK); }); - std::cout << "1M insert: " << ft.getExecutionTimeMs() << "ms" << std::endl; - EXPECT_EQ(t.getFileSize(), insertCount * sizeof(TestBinaryEntryContainer) + - sizeof(TestBinaryHeader)); - int ec = 0; - std::vector allEntries; - FunctionTimer ft1( - [&t, &allEntries]() { EXPECT_TRUE(t.getAllEntries(allEntries)); }); - std::cout << "1M getAllEntries: " << ft1.getExecutionTimeMs() << "ms" - << std::endl; +TEST(BinaryFile, test1kSingleInsert) { + testSingleInsert(1000); +} - for (const auto &entry : allEntries) { - EXPECT_EQ(entry.checksum, TestBinaryEntryContainer(entries[ec]).checksum); - ec++; - } - // cleanupTestFile(t); +// NOLINTNEXTLINE(cert-err58-cpp) +TEST(BinaryFile, test10kSingleInsert) { + testSingleInsert(10000); +} + +// NOLINTNEXTLINE(cert-err58-cpp) +TEST(BinaryFile, test100kSingleInsert) { + testSingleInsert(100000); +} + +// NOLINTNEXTLINE(cert-err58-cpp) +TEST(BinaryFile, test1kVectorInsert) { + testVectorInsert(1000); +} + +// NOLINTNEXTLINE(cert-err58-cpp) +TEST(BinaryFile, test10kVectorInsert) { + testVectorInsert(10000); +} + +// NOLINTNEXTLINE(cert-err58-cpp) +TEST(BinaryFile, test100kVectorInsert) { + testVectorInsert(100000); +} + +// NOLINTNEXTLINE(cert-err58-cpp) +TEST(BinaryFile, test1MVectorInsert) { + testVectorInsert(1000000); } \ No newline at end of file diff --git a/doc/Benchmarks.md b/doc/Benchmarks.md new file mode 100644 index 0000000..e079eb1 --- /dev/null +++ b/doc/Benchmarks.md @@ -0,0 +1,48 @@ +# Ryzen 9 3900xt + +``` +[==========] Running 7 tests from 1 test suite. +[----------] Global test environment set-up. +[----------] 7 tests from BinaryFile +[ RUN ] BinaryFile.test1kSingleInsert +Single insert of 1000 items took 1354ms +Size: 8020 +1000 getAllEntries: 0ms +[ OK ] BinaryFile.test1kSingleInsert (1366 ms) +[ RUN ] BinaryFile.test10kSingleInsert +Single insert of 10000 items took 12817ms +Size: 80020 +10000 getAllEntries: 0ms +[ OK ] BinaryFile.test10kSingleInsert (12820 ms) +[ RUN ] BinaryFile.test100kSingleInsert +Single insert of 100000 items took 128647ms +Size: 800020 +100000 getAllEntries: 1ms +[ OK ] BinaryFile.test100kSingleInsert (128656 ms) +[ RUN ] BinaryFile.test1kVectorInsert +Vector insert of 1000 items took 1ms +Size: 8020 +1000 getAllEntries: 0ms +[ OK ] BinaryFile.test1kVectorInsert (5 ms) +[ RUN ] BinaryFile.test10kVectorInsert +Vector insert of 10000 items took 1ms +Size: 80020 +10000 getAllEntries: 0ms +[ OK ] BinaryFile.test10kVectorInsert (5 ms) +[ RUN ] BinaryFile.test100kVectorInsert +Vector insert of 100000 items took 6ms +Size: 800020 +100000 getAllEntries: 0ms +[ OK ] BinaryFile.test100kVectorInsert (19 ms) +[ RUN ] BinaryFile.test1MVectorInsert +Vector insert of 1000000 items took 40ms +Size: 8000020 +1000000 getAllEntries: 8ms +[ OK ] BinaryFile.test1MVectorInsert (153 ms) +[----------] 7 tests from BinaryFile (143024 ms total) + +[----------] Global test environment tear-down +[==========] 7 tests from 1 test suite ran. (143024 ms total) +[ PASSED ] 7 tests. + +``` \ No newline at end of file