From 767312e2063f4e58af3de0f27aba52de49e14295 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 5 Dec 2014 09:17:46 +0100 Subject: major reorg that puts Storage (previously Database) into common there is now a top-level tests dir, and a compile time switch for lmdb vs kyotocabinet --- tests/storagebenchmark.cpp | 157 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 tests/storagebenchmark.cpp (limited to 'tests/storagebenchmark.cpp') diff --git a/tests/storagebenchmark.cpp b/tests/storagebenchmark.cpp new file mode 100644 index 0000000..15da9da --- /dev/null +++ b/tests/storagebenchmark.cpp @@ -0,0 +1,157 @@ +#include + +#include "calendar_generated.h" + +#include +#include + +#include +#include +#include +#include + +#include "common/storage.h" + +using namespace Calendar; +using namespace flatbuffers; + +static std::string createEvent() +{ + FlatBufferBuilder fbb; + { + auto summary = fbb.CreateString("summary"); + + const int attachmentSize = 1024*2; // 2KB + int8_t rawData[attachmentSize]; + auto data = fbb.CreateVector(rawData, attachmentSize); + + Calendar::EventBuilder eventBuilder(fbb); + eventBuilder.add_summary(summary); + eventBuilder.add_attachment(data); + auto eventLocation = eventBuilder.Finish(); + FinishEventBuffer(fbb, eventLocation); + } + return std::string(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize()); +} + +// static void readEvent(const std::string &data) +// { +// auto readEvent = GetEvent(data.c_str()); +// std::cout << readEvent->summary()->c_str() << std::endl; +// } + +class StorageBenchmark : public QObject +{ + Q_OBJECT +private: + //This should point to a directory on disk and not a ramdisk (since we're measuring performance) + QString testDataPath; + QString dbName; + QString filePath; + const int count = 50000; + +private Q_SLOTS: + void initTestCase() + { + testDataPath = "./testdb"; + dbName = "test"; + filePath = testDataPath + "buffer.fb"; + } + + void testWriteRead_data() + { + QTest::addColumn("useDb"); + QTest::addColumn("count"); + + QTest::newRow("db, 50k") << true << count; + QTest::newRow("file, 50k") << false << count; + } + + void testWriteRead() + { + QFETCH(bool, useDb); + QFETCH(int, count); + + Storage *store = 0; + if (useDb) { + store = new Storage(testDataPath, dbName); + } + + std::ofstream myfile; + myfile.open(filePath.toStdString()); + const char *keyPrefix = "key"; + + QTime time; + + time.start(); + { + auto event = createEvent(); + for (int i = 0; i < count; i++) { + if (store) { + if (i % 10000 == 0) { + if (i > 0) { + store->commitTransaction(); + } + store->startTransaction(); + } + + store->write(keyPrefix + std::to_string(i), event); + } else { + myfile << event; + } + } + + if (store) { + store->commitTransaction(); + } else { + myfile.close(); + } + } + const int writeDuration = time.restart(); + qDebug() << "Writing took[ms]: " << writeDuration; + + { + for (int i = 0; i < count; i++) { + if (store) { + store->read(keyPrefix + std::to_string(i), [](std::string value){}); + } + } + } + const int readDuration = time.restart(); + + if (store) { + qDebug() << "Reading took[ms]: " << readDuration; + } else { + qDebug() << "File reading is not implemented."; + } + + delete store; + } + + void testBufferCreation() + { + QTime time; + + time.start(); + { + for (int i = 0; i < count; i++) { + auto event = createEvent(); + } + } + const int bufferDuration = time.elapsed(); + qDebug() << "Creating buffers took[ms]: " << bufferDuration; + } + + void testSizes() + { + Storage store(testDataPath, dbName); + qDebug() << "Database size [kb]: " << store.diskUsage()/1024; + store.removeFromDisk(); + + QFileInfo fileInfo(filePath); + qDebug() << "File size [kb]: " << fileInfo.size()/1024; + } +}; + +QTEST_MAIN(StorageBenchmark) +#include "storagebenchmark.moc" -- cgit v1.2.3 From 88085fd52f886692c9cbb534166e68b791d5abce Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 5 Dec 2014 09:29:37 +0100 Subject: port over 707344 by cmollekopf: small cleanup --- tests/storagebenchmark.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'tests/storagebenchmark.cpp') diff --git a/tests/storagebenchmark.cpp b/tests/storagebenchmark.cpp index 15da9da..f9fea7c 100644 --- a/tests/storagebenchmark.cpp +++ b/tests/storagebenchmark.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include @@ -58,6 +57,12 @@ private Q_SLOTS: filePath = testDataPath + "buffer.fb"; } + void cleanupTestCase() + { + Storage store(testDataPath, dbName); + store.removeFromDisk(); + } + void testWriteRead_data() { QTest::addColumn("useDb"); @@ -146,7 +151,6 @@ private Q_SLOTS: { Storage store(testDataPath, dbName); qDebug() << "Database size [kb]: " << store.diskUsage()/1024; - store.removeFromDisk(); QFileInfo fileInfo(filePath); qDebug() << "File size [kb]: " << fileInfo.size()/1024; -- cgit v1.2.3