From 7cbf25f2f6d38efa384c7f8e0a5b2b2d274ebb00 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 30 Nov 2014 17:57:31 +0100 Subject: Moved buffertest to store/test and turned it into a reproducible benchmark. --- store/test/storagebenchmark.cpp | 132 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 store/test/storagebenchmark.cpp (limited to 'store/test/storagebenchmark.cpp') diff --git a/store/test/storagebenchmark.cpp b/store/test/storagebenchmark.cpp new file mode 100644 index 0000000..939ae0b --- /dev/null +++ b/store/test/storagebenchmark.cpp @@ -0,0 +1,132 @@ +#include + +#include "calendar_generated.h" +#include +#include +#include +#include +#include +#include + +#include "store/database.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 dbPath; + QString filePath; + +private Q_SLOTS: + void initTestCase() + { + testDataPath = "./"; + dbPath = testDataPath + "testdb"; + filePath = testDataPath + "buffer.fb"; + + QDir dir(testDataPath); + dir.remove("testdb/data.mdb"); + dir.remove("testdb/lock.mdb"); + dir.remove(filePath); + } + + void testWriteRead_data() + { + QTest::addColumn("useDb"); + QTest::addColumn("count"); + + const int count = 50000; + QTest::newRow("db, 50k") << true << count; + QTest::newRow("file, 50k") << false << count; + } + + void testWriteRead() + { + QFETCH(bool, useDb); + QFETCH(int, count); + + Database db(dbPath); + + std::ofstream myfile; + myfile.open(filePath.toStdString()); + + QTime time; + time.start(); + { + auto transaction = db.startTransaction(); + for (int i = 0; i < count; i++) { + const auto key = QString("key%1").arg(i); + auto event = createEvent(); + if (useDb) { + db.write(key.toStdString(), event, transaction); + } else { + myfile << event; + } + } + if (useDb) { + db.endTransaction(transaction); + } else { + myfile.close(); + } + } + const int writeDuration = time.elapsed(); + qDebug() << "Writing took[ms]: " << writeDuration; + + time.start(); + { + for (int i = 0; i < count; i++) { + const auto key = QString("key%1").arg(i); + if (useDb) { + db.read(key.toStdString()); + } + } + } + const int readDuration = time.elapsed(); + if (useDb) { + qDebug() << "Reading took[ms]: " << readDuration; + } else { + qDebug() << "File reading is not implemented."; + } + } + + void testSizes() + { + QFileInfo dbInfo(dbPath, "data.mdb"); + QFileInfo fileInfo(filePath); + qDebug() << "Database size [kb]: " << dbInfo.size()/1024; + qDebug() << "File size [kb]: " << fileInfo.size()/1024; + } +}; + +QTEST_MAIN(StorageBenchmark) +#include "storagebenchmark.moc" -- cgit v1.2.3