File: bench3.py

package info (click to toggle)
python-mujson 1.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,068 kB
  • sloc: python: 308; makefile: 17
file content (95 lines) | stat: -rw-r--r-- 1,696 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import sys

from timeit import Timer


DECODE_TESTS = [
    'yajl',
    'nssjson',
    'json',
    'rapidjson',
    'simplejson',
    'ujson',
    'orjson',
    'mujson'
]


ENCODE_TESTS = [
    'simplejson',
    'nssjson',
    'json',
    'yajl',
    'ujson',
    'rapidjson',
    'metamagic.json',
    'orjson',
    'mujson'
]


DECODE_ENCODE_TESTS = [
    'nssjson',
    'simplejson',
    'yajl',
    'json',
    'rapidjson',
    'ujson',
    'orjson',
    'mujson'
]


ACTIONS = {
    'decoded': 'loads(bin)',
    'encoded': 'dumps(obj)',
    'de/encoded': 'dumps(loads(bin))'
}


ASCII_BREAK = '\n' + '*' * 75 + '\n'


_import_tpl = 'from {} import (dumps, loads)'


_bin_tmpl = "bin = open('json/{}', 'r').read()"


_obj = "obj = loads(bin)"


_warm_up = "loads(dumps([]))"


def timeit(mod, act, num, jsn, import_stmt):
    setup = [import_stmt, _warm_up, _bin_tmpl.format(jsn), _obj]
    try:
        msecs = Timer(ACTIONS[act], '; '.join(setup)).timeit(num) * 1000
        print(f'{mod:15s} {act} {jsn} {num} times in {msecs} milliseconds!')
    except:
        print(f'{mod:15s} threw Exception trying to {act} {jsn} {num} times!')


def main(number, jsn):
    print(ASCII_BREAK)

    for module in DECODE_TESTS:
        timeit(module, 'decoded', number, jsn, _import_tpl.format(module))

    print(ASCII_BREAK)

    for module in ENCODE_TESTS:
        timeit(module, 'encoded', number, jsn, _import_tpl.format(module))

    print(ASCII_BREAK)

    for module in DECODE_ENCODE_TESTS:
        timeit(module, 'de/encoded', number, jsn, _import_tpl.format(module))

    print(ASCII_BREAK)


if __name__ == '__main__':
    if len(sys.argv) == 3:
        main(int(sys.argv[1]), sys.argv[2])