File: benchmark_kv_store.py

package info (click to toggle)
diskcache 5.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,364 kB
  • sloc: python: 7,026; makefile: 20
file content (77 lines) | stat: -rw-r--r-- 2,273 bytes parent folder | download | duplicates (2)
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
"""Benchmarking Key-Value Stores

$ python -m IPython tests/benchmark_kv_store.py
"""

from IPython import get_ipython

import diskcache

ipython = get_ipython()
assert ipython is not None, 'No IPython! Run with $ ipython ...'

value = 'value'

print('diskcache set')
dc = diskcache.FanoutCache('/tmp/diskcache')
ipython.magic("timeit -n 100 -r 7 dc['key'] = value")
print('diskcache get')
ipython.magic("timeit -n 100 -r 7 dc['key']")
print('diskcache set/delete')
ipython.magic("timeit -n 100 -r 7 dc['key'] = value; del dc['key']")

try:
    import dbm.gnu  # Only trust GNU DBM
except ImportError:
    print('Error: Cannot import dbm.gnu')
    print('Error: Skipping import shelve')
else:
    print('dbm set')
    d = dbm.gnu.open('/tmp/dbm', 'c')
    ipython.magic("timeit -n 100 -r 7 d['key'] = value; d.sync()")
    print('dbm get')
    ipython.magic("timeit -n 100 -r 7 d['key']")
    print('dbm set/delete')
    ipython.magic(
        "timeit -n 100 -r 7 d['key'] = value; d.sync(); del d['key']; d.sync()"
    )

    import shelve

    print('shelve set')
    s = shelve.open('/tmp/shelve')
    ipython.magic("timeit -n 100 -r 7 s['key'] = value; s.sync()")
    print('shelve get')
    ipython.magic("timeit -n 100 -r 7 s['key']")
    print('shelve set/delete')
    ipython.magic(
        "timeit -n 100 -r 7 s['key'] = value; s.sync(); del s['key']; s.sync()"
    )

try:
    import sqlitedict
except ImportError:
    print('Error: Cannot import sqlitedict')
else:
    print('sqlitedict set')
    sd = sqlitedict.SqliteDict('/tmp/sqlitedict', autocommit=True)
    ipython.magic("timeit -n 100 -r 7 sd['key'] = value")
    print('sqlitedict get')
    ipython.magic("timeit -n 100 -r 7 sd['key']")
    print('sqlitedict set/delete')
    ipython.magic("timeit -n 100 -r 7 sd['key'] = value; del sd['key']")

try:
    import pickledb
except ImportError:
    print('Error: Cannot import pickledb')
else:
    print('pickledb set')
    p = pickledb.load('/tmp/pickledb', True)
    ipython.magic("timeit -n 100 -r 7 p['key'] = value")
    print('pickledb get')
    ipython.magic(
        "timeit -n 100 -r 7 p = pickledb.load('/tmp/pickledb', True); p['key']"
    )
    print('pickledb set/delete')
    ipython.magic("timeit -n 100 -r 7 p['key'] = value; del p['key']")