Skip to content

Commit c9f9036

Browse files
author
Ivan Dlugos
committed
add a simple CRUD benchmark
1 parent 9d7310f commit c9f9036

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ init: ${VENV}
2424
test: ${VENV}
2525
python3 -m pytest -s
2626

27+
benchmark: ${VENV}
28+
python3 -m benchmark
29+
2730
clean:
2831
rm -rf build/
2932
rm -rf dist/
3033
rm -rf *.egg-info
3134

32-
.PHONY: init test build
35+
.PHONY: init test build benchmark

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ cp lib/*objectbox* [/path/to/your/git/objectbox/checkout]/objectbox/lib/$(uname
120120
```
121121

122122
You can run `make test` to make sure everything works as expected.
123+
You can also try `make benchmark` to measure the CRUD performance on your machine.
123124

124125
License
125126
-------

benchmark.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import objectbox
2+
import time
3+
from tests.model import TestEntity
4+
from tests.common import remove_test_dir, load_empty_test_objectbox
5+
6+
7+
class ObjectBoxPerf:
8+
"""
9+
Performance executable
10+
"""
11+
12+
def __init__(self):
13+
self.ob = load_empty_test_objectbox()
14+
self.box = objectbox.Box(self.ob, TestEntity)
15+
16+
def remove_all(self):
17+
self.box.remove_all()
18+
19+
def put_many(self, items):
20+
self.box.put(items)
21+
22+
def read_all(self):
23+
return self.box.get_all()
24+
25+
26+
class Timer:
27+
"""
28+
Context manager to time a block of code.
29+
Appends the resulting runtime in milliseconds to a list of floats.
30+
"""
31+
32+
def __init__(self, out_list: [float]):
33+
self.start = time.time_ns()
34+
self.list = out_list
35+
36+
def __enter__(self):
37+
return self
38+
39+
def __exit__(self, exc_type, exc_val, exc_tb):
40+
self.list.append((time.time_ns() - self.start) / 1000 / 1000)
41+
42+
43+
class PerfExecutor:
44+
"""
45+
Performance executable
46+
"""
47+
48+
def __init__(self, executable):
49+
self.perf = executable
50+
51+
def run(self, count=1000, runs=10):
52+
inserts = self.__prepare_data(count)
53+
54+
# function => vector of runtimes in milliseconds
55+
from collections import defaultdict
56+
times = defaultdict(list)
57+
58+
progress_text = "Measuring performance"
59+
for i in range(runs):
60+
self.__progress_bar(progress_text, i, runs)
61+
62+
with Timer(times["insert-many"]):
63+
self.perf.put_many(inserts)
64+
65+
with Timer(times["read-all"]):
66+
items = self.perf.read_all()
67+
68+
with Timer(times["update-many"]):
69+
self.perf.put_many(items)
70+
71+
with Timer(times["remove-all"]):
72+
self.perf.remove_all()
73+
74+
self.__progress_bar(progress_text, runs, runs)
75+
76+
# print the results
77+
print()
78+
print('=' * 80)
79+
print("runs\t%d\t\tcount\t%d\t\tunit\tms" % (runs, count))
80+
print("Function\tMedian\tMean\tStdDev")
81+
import statistics
82+
for key in times.keys():
83+
print("%s\t%d\t%d\t%d" % (
84+
key,
85+
statistics.median(times[key]),
86+
statistics.mean(times[key]),
87+
statistics.stdev(times[key])
88+
))
89+
90+
@staticmethod
91+
def __prepare_data(count: int) -> [TestEntity]:
92+
result = []
93+
for i in range(count):
94+
object = TestEntity()
95+
object.str = "Entity no. %d" % i
96+
object.float = i * 1.1
97+
object.int = i
98+
result.append(object)
99+
return result
100+
101+
@staticmethod
102+
def __progress_bar(text, value, endvalue, bar_length=20):
103+
import sys
104+
percent = float(value) / endvalue
105+
arrow = '-' * int(round(percent * bar_length) - 1) + '>'
106+
spaces = ' ' * (bar_length - len(arrow))
107+
108+
sys.stdout.write("\r{0}: [{1}] {2}%".format(text, arrow + spaces, int(round(percent * 100))))
109+
sys.stdout.flush()
110+
111+
112+
if __name__ == "__main__":
113+
remove_test_dir()
114+
115+
obPerf = ObjectBoxPerf()
116+
executor = PerfExecutor(obPerf)
117+
executor.run(count=10000, runs=20)
118+
119+
remove_test_dir()

0 commit comments

Comments
 (0)