Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
1import os
2import json
3import contextlib
4import time
5
6from Plugin import PluginManager
7from Config import config
8
9
10@PluginManager.registerTo("Actions")
11class ActionsPlugin:
12 def getBenchmarkTests(self, online=False):
13 tests = super().getBenchmarkTests(online)
14 tests.extend([
15 {"func": self.testDbConnect, "num": 10, "time_standard": 0.27},
16 {"func": self.testDbInsert, "num": 10, "time_standard": 0.91},
17 {"func": self.testDbInsertMultiuser, "num": 1, "time_standard": 0.57},
18 {"func": self.testDbQueryIndexed, "num": 1000, "time_standard": 0.84},
19 {"func": self.testDbQueryNotIndexed, "num": 1000, "time_standard": 1.30}
20 ])
21 return tests
22
23
24 @contextlib.contextmanager
25 def getTestDb(self):
26 from Db import Db
27 path = "%s/benchmark.db" % config.data_dir
28 if os.path.isfile(path):
29 os.unlink(path)
30 schema = {
31 "db_name": "TestDb",
32 "db_file": path,
33 "maps": {
34 ".*": {
35 "to_table": {
36 "test": "test"
37 }
38 }
39 },
40 "tables": {
41 "test": {
42 "cols": [
43 ["test_id", "INTEGER"],
44 ["title", "TEXT"],
45 ["json_id", "INTEGER REFERENCES json (json_id)"]
46 ],
47 "indexes": ["CREATE UNIQUE INDEX test_key ON test(test_id, json_id)"],
48 "schema_changed": 1426195822
49 }
50 }
51 }
52
53 db = Db.Db(schema, path)
54
55 yield db
56
57 db.close()
58 if os.path.isfile(path):
59 os.unlink(path)
60
61 def testDbConnect(self, num_run=1):
62 import sqlite3
63 for i in range(num_run):
64 with self.getTestDb() as db:
65 db.checkTables()
66 yield "."
67 yield "(SQLite version: %s, API: %s)" % (sqlite3.sqlite_version, sqlite3.version)
68
69 def testDbInsert(self, num_run=1):
70 yield "x 1000 lines "
71 for u in range(num_run):
72 with self.getTestDb() as db:
73 db.checkTables()
74 data = {"test": []}
75 for i in range(1000): # 1000 line of data
76 data["test"].append({"test_id": i, "title": "Testdata for %s message %s" % (u, i)})
77 json.dump(data, open("%s/test_%s.json" % (config.data_dir, u), "w"))
78 db.updateJson("%s/test_%s.json" % (config.data_dir, u))
79 os.unlink("%s/test_%s.json" % (config.data_dir, u))
80 assert db.execute("SELECT COUNT(*) FROM test").fetchone()[0] == 1000
81 yield "."
82
83 def fillTestDb(self, db):
84 db.checkTables()
85 cur = db.getCursor()
86 cur.logging = False
87 for u in range(100, 200): # 100 user
88 data = {"test": []}
89 for i in range(100): # 1000 line of data
90 data["test"].append({"test_id": i, "title": "Testdata for %s message %s" % (u, i)})
91 json.dump(data, open("%s/test_%s.json" % (config.data_dir, u), "w"))
92 db.updateJson("%s/test_%s.json" % (config.data_dir, u), cur=cur)
93 os.unlink("%s/test_%s.json" % (config.data_dir, u))
94 if u % 10 == 0:
95 yield "."
96
97 def testDbInsertMultiuser(self, num_run=1):
98 yield "x 100 users x 100 lines "
99 for u in range(num_run):
100 with self.getTestDb() as db:
101 for progress in self.fillTestDb(db):
102 yield progress
103 num_rows = db.execute("SELECT COUNT(*) FROM test").fetchone()[0]
104 assert num_rows == 10000, "%s != 10000" % num_rows
105
106 def testDbQueryIndexed(self, num_run=1):
107 s = time.time()
108 with self.getTestDb() as db:
109 for progress in self.fillTestDb(db):
110 pass
111 yield " (Db warmup done in %.3fs) " % (time.time() - s)
112 found_total = 0
113 for i in range(num_run): # 1000x by test_id
114 found = 0
115 res = db.execute("SELECT * FROM test WHERE test_id = %s" % (i % 100))
116 for row in res:
117 found_total += 1
118 found += 1
119 del(res)
120 yield "."
121 assert found == 100, "%s != 100 (i: %s)" % (found, i)
122 yield "Found: %s" % found_total
123
124 def testDbQueryNotIndexed(self, num_run=1):
125 s = time.time()
126 with self.getTestDb() as db:
127 for progress in self.fillTestDb(db):
128 pass
129 yield " (Db warmup done in %.3fs) " % (time.time() - s)
130 found_total = 0
131 for i in range(num_run): # 1000x by test_id
132 found = 0
133 res = db.execute("SELECT * FROM test WHERE json_id = %s" % i)
134 for row in res:
135 found_total += 1
136 found += 1
137 yield "."
138 del(res)
139 if i == 0 or i > 100:
140 assert found == 0, "%s != 0 (i: %s)" % (found, i)
141 else:
142 assert found == 100, "%s != 100 (i: %s)" % (found, i)
143 yield "Found: %s" % found_total