| 1 |
import sys |
| 2 |
import py |
| 3 |
ROOT = py.path.local(__file__).dirpath('..', '..', '..') |
| 4 |
sys.path.insert(0, str(ROOT)) |
| 5 |
|
| 6 |
import time |
| 7 |
from pypy.interpreter.error import OperationError |
| 8 |
from pypy.module._pypyjson.interp_decoder import loads, JSONDecoder |
| 9 |
from rpython.rlib.objectmodel import specialize, dont_inline |
| 10 |
|
| 11 |
def _create_dict(self, d): |
| 12 |
w_res = W_Dict() |
| 13 |
w_res.dictval = d |
| 14 |
return w_res |
| 15 |
|
| 16 |
JSONDecoder._create_dict = _create_dict |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class W_Root(object): |
| 21 |
pass |
| 22 |
|
| 23 |
class W_Dict(W_Root): |
| 24 |
def __init__(self): |
| 25 |
self.dictval = {} |
| 26 |
|
| 27 |
class W_Unicode(W_Root): |
| 28 |
def __init__(self, x): |
| 29 |
self.unival = x |
| 30 |
|
| 31 |
class W_String(W_Root): |
| 32 |
def __init__(self, x): |
| 33 |
self.strval = x |
| 34 |
|
| 35 |
class W_Int(W_Root): |
| 36 |
def __init__(self, x): |
| 37 |
self.intval = x |
| 38 |
|
| 39 |
class W_Float(W_Root): |
| 40 |
def __init__(self, x): |
| 41 |
self.floatval = x |
| 42 |
|
| 43 |
class W_List(W_Root): |
| 44 |
def __init__(self): |
| 45 |
self.listval = [] |
| 46 |
|
| 47 |
class W_Singleton(W_Root): |
| 48 |
def __init__(self, name): |
| 49 |
self.name = name |
| 50 |
|
| 51 |
class FakeSpace(object): |
| 52 |
|
| 53 |
w_None = W_Singleton('None') |
| 54 |
w_True = W_Singleton('True') |
| 55 |
w_False = W_Singleton('False') |
| 56 |
w_ValueError = W_Singleton('ValueError') |
| 57 |
w_UnicodeDecodeError = W_Singleton('UnicodeDecodeError') |
| 58 |
w_unicode = W_Unicode |
| 59 |
w_int = W_Int |
| 60 |
w_float = W_Float |
| 61 |
|
| 62 |
def newtuple(self, items): |
| 63 |
return None |
| 64 |
|
| 65 |
def newdict(self): |
| 66 |
return W_Dict() |
| 67 |
|
| 68 |
def newlist(self, items): |
| 69 |
return W_List() |
| 70 |
|
| 71 |
def isinstance_w(self, w_x, w_type): |
| 72 |
return isinstance(w_x, w_type) |
| 73 |
|
| 74 |
def bytes_w(self, w_x): |
| 75 |
assert isinstance(w_x, W_String) |
| 76 |
return w_x.strval |
| 77 |
|
| 78 |
@dont_inline |
| 79 |
def call_method(self, obj, name, arg): |
| 80 |
assert name == 'append' |
| 81 |
assert isinstance(obj, W_List) |
| 82 |
obj.listval.append(arg) |
| 83 |
|
| 84 |
def call_function(self, w_func, *args_w): |
| 85 |
return self.w_None |
| 86 |
|
| 87 |
def setitem(self, d, key, value): |
| 88 |
assert isinstance(d, W_Dict) |
| 89 |
assert isinstance(key, W_Unicode) |
| 90 |
d.dictval[key.unival] = value |
| 91 |
|
| 92 |
def newtext(self, x): |
| 93 |
return W_String(x) |
| 94 |
newbytes = newtext |
| 95 |
|
| 96 |
def newint(self, x): |
| 97 |
return W_Int(x) |
| 98 |
|
| 99 |
def newfloat(self, x): |
| 100 |
return W_Float(x) |
| 101 |
|
| 102 |
@specialize.argtype(1) |
| 103 |
def wrap(self, x): |
| 104 |
if isinstance(x, int): |
| 105 |
return W_Int(x) |
| 106 |
elif isinstance(x, float): |
| 107 |
return W_Float(x) |
| 108 |
|
| 109 |
|
| 110 |
else: |
| 111 |
return W_Unicode(unicode(x)) |
| 112 |
|
| 113 |
|
| 114 |
fakespace = FakeSpace() |
| 115 |
|
| 116 |
def myloads(msg): |
| 117 |
return loads(fakespace, W_String(msg)) |
| 118 |
|
| 119 |
def bench(title, N, fn, arg): |
| 120 |
a = time.clock() |
| 121 |
for i in range(N): |
| 122 |
res = fn(arg) |
| 123 |
b = time.clock() |
| 124 |
print title, (b-a) / N * 1000 |
| 125 |
|
| 126 |
def entry_point(argv): |
| 127 |
if len(argv) != 3: |
| 128 |
print 'Usage: %s FILE n' % argv[0] |
| 129 |
return 1 |
| 130 |
filename = argv[1] |
| 131 |
N = int(argv[2]) |
| 132 |
f = open(filename) |
| 133 |
msg = f.read() |
| 134 |
|
| 135 |
try: |
| 136 |
bench('loads ', N, myloads, msg) |
| 137 |
except OperationError as e: |
| 138 |
print 'Error', e._compute_value(fakespace) |
| 139 |
|
| 140 |
return 0 |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
def target(*args): |
| 145 |
return entry_point, None |
| 146 |
|
| 147 |
if __name__ == '__main__': |
| 148 |
entry_point(sys.argv) |