|
| 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