-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf_storage.py
executable file
·236 lines (167 loc) · 5.79 KB
/
perf_storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python3
import collections
import logging
import os
import random
import sqlite3
import time
import perf
def main():
testables = (Dummy,
PyListAppend,
PyListPrepend,
PyDeque,
Logger,
AppendToFile,
AppendToOpenedFile,
SQliteBufferedList,
SQliteBufferedReverseList,
SQliteBufferedDeque,
SQlitePeriodicCommit,
SQliteSingleTransaction,
# SQlite,
SQliteRamSingleTransaction,
SQliteRam)
print(perf.Benchmark(testables, 300000).as_text())
class Testable(perf.Testable):
storage = None
buffer_size = 100000
@classmethod
def setup_storage(cls, storage):
if type(storage) == str and storage.startswith('tmp.'):
dirname = os.path.dirname(os.path.abspath(__file__))
storage = os.path.join(dirname, storage)
if os.path.isfile(storage):
os.remove(storage)
return storage
def action_value(self):
return f"{time.time()}: INFO: {random.random()}"
class Dummy(Testable):
"""Do not store value, just for comparison."""
def action(self):
self.action_value()
class PyListAppend(Testable):
"""Save records in python runtime list."""
def setup(self):
self.storage = []
def action(self):
self.storage.append(self.action_value())
class PyListPrepend(Testable):
"""Save records in python runtime list."""
def setup(self):
self.storage = []
def action(self):
self.storage.insert(0, self.action_value())
class PyDeque(Testable):
"""Save records in python runtime list."""
def setup(self):
self.storage = collections.deque()
def action(self):
self.storage.appendleft(self.action_value())
class Logger(Testable):
"""Standard logging into file."""
def setup(self):
self.storage = self.setup_storage('tmp.log')
self.logger = logging.getLogger('test')
self.logger.setLevel(logging.INFO)
self.logger.addHandler(logging.FileHandler(self.storage))
def action(self):
self.logger.info(self.action_value())
class AppendToFile(Testable):
"""Append lines to file."""
def setup(self):
self.storage = self.setup_storage('tmp.file_to_append')
def action(self):
with open(self.storage, 'a') as f:
f.write(f"{self.action_value()}\n")
class AppendToOpenedFile(Testable):
"""Append lines to opened file."""
def setup(self):
self.storage = self.setup_storage('tmp.opened_file_to_append')
self.f = open(self.storage, 'w')
def action(self):
self.f.write(f"{self.action_value()}\n")
def finalize(self):
self.f.close()
class SQlite(Testable):
"""Transaction per each record."""
INSERT = "INSERT INTO log (ts, level, message) VALUES (?, ?, ?)"
DB = 'tmp.sqlite'
def setup(self):
self.storage = self.setup_storage(self.DB)
self.con = sqlite3.connect(self.storage)
with self.con:
self.con.execute("CREATE TABLE log (ts real, level integer, message varchar)")
self.cur = self.con.cursor()
def action(self):
with self.con:
self.con.execute(self.INSERT, (time.time(), 1, self.action_value()))
def finalize(self):
self.con.commit()
self.con.close()
class SQliteSingleTransaction(SQlite):
"""Save all records in one transaction."""
DB = 'tmp.sqlite_st'
def action(self):
self.cur.execute(self.INSERT, (time.time(), 1, self.action_value()))
class SQliteBufferedList(SQlite):
"""Buffer records and save them periodically."""
DB = 'tmp.sqlite_list'
def setup(self):
super().setup()
self.buffer = []
def action(self):
self.buffer.insert(0, (time.time(), 1, self.action_value()))
if len(self.buffer) >= self.buffer_size:
self._flush_buffer()
def finalize(self):
self._flush_buffer()
super().finalize()
def _flush_buffer(self):
while self.buffer:
self.cur.execute(self.INSERT, self.buffer.pop(0))
self.con.commit()
class SQliteBufferedReverseList(SQliteBufferedList):
"""Buffer records and save them periodically."""
DB = 'tmp.sqlite_rlist'
def action(self):
self.buffer.append((time.time(), 1, self.action_value()))
if len(self.buffer) >= self.buffer_size:
self._flush_buffer()
class SQliteBufferedDeque(SQlite):
"""Buffer records and save them periodically."""
DB = 'tmp.sqlite_deque'
def setup(self):
super().setup()
self.buffer = collections.deque()
def action(self):
self.buffer.appendleft((time.time(), 1, self.action_value()))
if len(self.buffer) >= self.buffer_size:
self._flush_buffer()
def finalize(self):
self._flush_buffer()
super().finalize()
def _flush_buffer(self):
while self.buffer:
self.cur.execute(self.INSERT, self.buffer.popleft())
self.con.commit()
class SQlitePeriodicCommit(SQlite):
"""Buffer records and save them periodically."""
DB = 'tmp.sqlite_deque'
def setup(self):
super().setup()
self.buffer = 0
def action(self):
self.cur.execute(self.INSERT, (time.time(), 1, self.action_value()))
self.buffer += 1
if self.buffer >= self.buffer_size:
self.con.commit()
self.buffer = 0
class SQliteRam(SQlite):
"""Transaction per each record, in RAM."""
DB = ':memory:'
class SQliteRamSingleTransaction(SQliteSingleTransaction):
"""Save all records in one transaction, in RAM."""
DB = ':memory:'
if __name__ == '__main__':
main()