diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-05 10:05:19 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-05 10:05:19 +0100 |
commit | 93d1c82ffd17df45a5cecd875a01ee3cb15d9983 (patch) | |
tree | cda5c31857362ce81f3a50959024f4270d149a07 /tests/storagetest.cpp | |
parent | 639fc60c100204c87b93112516cf3b3117cfff0d (diff) | |
parent | 351a66b5fb1c8659bff8ea20d60f5a6d2d3263ad (diff) | |
download | sink-93d1c82ffd17df45a5cecd875a01ee3cb15d9983.tar.gz sink-93d1c82ffd17df45a5cecd875a01ee3cb15d9983.zip |
Merge branch 'kyoto'
Conflicts:
common/storage.h
common/storage_lmdb.cpp
dummyresource/facade.cpp
store/test/CMakeLists.txt
tests/storagebenchmark.cpp
Diffstat (limited to 'tests/storagetest.cpp')
-rw-r--r-- | tests/storagetest.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp new file mode 100644 index 0000000..2b2805d --- /dev/null +++ b/tests/storagetest.cpp | |||
@@ -0,0 +1,112 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include <iostream> | ||
4 | |||
5 | #include <QDebug> | ||
6 | #include <QString> | ||
7 | #include <QtConcurrent/QtConcurrentRun> | ||
8 | |||
9 | #include "common/storage.h" | ||
10 | |||
11 | class StorageTest : public QObject | ||
12 | { | ||
13 | Q_OBJECT | ||
14 | private: | ||
15 | //This should point to a directory on disk and not a ramdisk (since we're measuring performance) | ||
16 | QString testDataPath; | ||
17 | QString dbName; | ||
18 | const char *keyPrefix = "key"; | ||
19 | |||
20 | void populate(int count) | ||
21 | { | ||
22 | Storage storage(testDataPath, dbName); | ||
23 | for (int i = 0; i < count; i++) { | ||
24 | //This should perhaps become an implementation detail of the db? | ||
25 | if (i % 10000 == 0) { | ||
26 | if (i > 0) { | ||
27 | storage.commitTransaction(); | ||
28 | } | ||
29 | storage.startTransaction(); | ||
30 | } | ||
31 | storage.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); | ||
32 | } | ||
33 | storage.commitTransaction(); | ||
34 | } | ||
35 | |||
36 | bool verify(Storage &storage, int i) | ||
37 | { | ||
38 | bool success = true; | ||
39 | bool keyMatch = true; | ||
40 | const auto reference = keyPrefix + std::to_string(i); | ||
41 | success = storage.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { | ||
42 | if (value != reference) { | ||
43 | qDebug() << "Mismatch while reading"; | ||
44 | keyMatch = false; | ||
45 | } | ||
46 | }); | ||
47 | return succes && keyMatch; | ||
48 | } | ||
49 | |||
50 | private Q_SLOTS: | ||
51 | void initTestCase() | ||
52 | { | ||
53 | testDataPath = "./testdb"; | ||
54 | dbName = "test"; | ||
55 | } | ||
56 | |||
57 | void cleanupTestCase() | ||
58 | { | ||
59 | Database db(testDataPath, dbName); | ||
60 | db.removeFromDisk(); | ||
61 | } | ||
62 | |||
63 | |||
64 | void testRead() | ||
65 | { | ||
66 | const int count = 100; | ||
67 | |||
68 | populate(count); | ||
69 | |||
70 | //ensure we can read everything back correctly | ||
71 | { | ||
72 | Database db(testDataPath, dbName); | ||
73 | for (int i = 0; i < count; i++) { | ||
74 | QVERIFY(verify(db, i)); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | Database db(testDataPath, dbName); | ||
79 | db.removeFromDisk(); | ||
80 | } | ||
81 | |||
82 | void testConcurrentRead() | ||
83 | { | ||
84 | const int count = 10000; | ||
85 | |||
86 | populate(count); | ||
87 | |||
88 | //Try to concurrently read | ||
89 | QList<QFuture<void> > futures; | ||
90 | const int concurrencyLevel = 4; | ||
91 | for (int num = 0; num < concurrencyLevel; num++) { | ||
92 | futures << QtConcurrent::run([this, count](){ | ||
93 | Database db(testDataPath, dbName); | ||
94 | for (int i = 0; i < count; i++) { | ||
95 | if (!verify(db, i)) { | ||
96 | qWarning() << "invalid value"; | ||
97 | break; | ||
98 | } | ||
99 | } | ||
100 | }); | ||
101 | } | ||
102 | for(auto future : futures) { | ||
103 | future.waitForFinished(); | ||
104 | } | ||
105 | |||
106 | Database db(testDataPath, dbName); | ||
107 | db.removeFromDisk(); | ||
108 | } | ||
109 | }; | ||
110 | |||
111 | QTEST_MAIN(StorageTest) | ||
112 | #include "storagetest.moc" | ||