Skip to content

Instantly share code, notes, and snippets.

@iwanbk
Created April 23, 2012 06:33
Show Gist options
  • Save iwanbk/2469133 to your computer and use it in GitHub Desktop.
Save iwanbk/2469133 to your computer and use it in GitHub Desktop.
flask with ujson & json benchmark
#!/usr/bin/env python
import timeit
import jsonrpclib
import ujson
import requests
N_TEST = 10000
server_json = jsonrpclib.Server('http://localhost:8000/json/')
server_ujson = jsonrpclib.Server('http://localhost:8000/ujson/')
server_jsonrpclib = jsonrpclib.Server('http://localhost:8080/')
param_1 = "IOOIHIUGOJOIUGIHOHjphogojhpohpjhpjp12p3j1p3j1p4j1p4j1p41jpj414"
param_2 = "IOOIHIUGOJOIUGIHOHjphogojhpohpjhpjp12p3j1p3j1p4j1p4j1p41jpj414"
param_3 = 10
def test_ujson_rpc():
server_ujson.echo_ujson(param_1, param_2, param_3)
def test_json_rpc():
server_json.echo_json(param_1, param_2, param_3)
print "test flask ujson rpc"
t = timeit.Timer(test_ujson_rpc)
print t.timeit(N_TEST)
print "test flask json rpc"
t = timeit.Timer(test_json_rpc)
print t.timeit(N_TEST)
from flask import Flask, request
import ujson
import json
app = Flask(__name__)
@app.route("/ujson/", methods = ["POST"])
def echo_ujson():
req_data = ujson.loads(request.data)
params = req_data['params']
reply = {
"jsonrpc": "2.0",
"result": [params[0], params[1], params[2]],
"id": req_data['id']
}
return ujson.dumps(reply)
@app.route("/json/", methods = ["POST"])
def echo_json():
req_data = json.loads(request.data)
params = req_data['params']
reply = {
"jsonrpc": "2.0",
"result": [params[0], params[1], params[2]],
"id": req_data['id']
}
return json.dumps(reply)
if __name__ == "__main__":
app.run(debug = False, port = 8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment