Skip to content

Commit 2e5cce1

Browse files
committed
benchmark: add different endpoints (png, json, lua-png)
1 parent a9f8f4b commit 2e5cce1

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

splash/benchmark/benchmark.py

+51-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,53 @@
2222
from splash.benchmark.file_server import serve_files
2323
from splash.tests.utils import SplashServer
2424

25+
26+
def make_render_png_req(splash, params):
27+
"""Prepare request for render.png endpoint."""
28+
return {'url': splash.url('render.png'),
29+
'params': params}
30+
31+
32+
def make_render_json_req(splash, params):
33+
"""Prepare request for render.json endpoint."""
34+
json_params = params.copy()
35+
json_params['png'] = 1
36+
return {'url': splash.url('render.json'),
37+
'params': json_params}
38+
39+
40+
def make_render_png_lua_req(splash, params):
41+
"""Prepare request for execute endpoint."""
42+
lua_params = params.copy()
43+
lua_params['lua_source'] = """
44+
function main(splash)
45+
assert(splash:go(splash.args.url))
46+
if splash.args.wait then
47+
assert(splash:wait(splash.args.wait))
48+
end
49+
splash:set_result_content_type("image/png")
50+
return splash:png{width=splash.args.width,
51+
height=splash.args.height,
52+
render_all=splash.args.render_all}
53+
end
54+
"""
55+
return {'url': splash.url('execute'),
56+
'params': lua_params}
57+
58+
59+
REQ_FACTORIES = [
60+
make_render_png_req,
61+
make_render_json_req,
62+
make_render_png_lua_req,
63+
]
64+
65+
2566
#: Port at which static pages will be served.
2667
PORT = 8806
2768
#: Static pages to be used in the benchmark.
2869
PAGES = glob('localhost_8806/*.html')
2970
#: Combinations of width & height to test.
3071
WIDTH_HEIGHT = [(None, None), (500, None), (None, 500), (500, 500)]
31-
# XXX: add benchmark of different API endpoints.
3272
SPLASH_LOG = 'splash.log'
3373
#: This script is used to collect maxrss & cpu time from splash process.
3474
GET_PERF_STATS_SCRIPT = """
@@ -37,6 +77,7 @@
3777
end
3878
"""
3979

80+
4081
parser = ArgumentParser(description=__doc__,
4182
formatter_class=ArgumentDefaultsHelpFormatter)
4283
parser.add_argument('--seed', type=int, default=1234, help='PRNG seed number')
@@ -53,10 +94,12 @@ def generate_requests(splash, args):
5394
for i in xrange(args.request_count):
5495
page = rng.choice(PAGES)
5596
width, height = rng.choice(WIDTH_HEIGHT)
97+
req_factory = rng.choice(REQ_FACTORIES)
5698
url = 'http://localhost:%d/%s' % (PORT, page)
57-
yield (i + 1, args.request_count,
58-
{'url': splash.url('render.png'),
59-
'params': {'url': url, 'width': width, 'height': height}})
99+
params = {'url': url, 'render_all': 1, 'wait': 0.1,
100+
'width': width, 'height': height}
101+
log.debug("Req factory: %s, params: %s", req_factory, params)
102+
yield (i + 1, args.request_count, req_factory(splash, params))
60103

61104

62105
def parallel_map(func, iterable, thread_count):
@@ -72,12 +115,15 @@ def invoke_request(invoke_args):
72115
req_no, total_reqs, kwargs = invoke_args
73116
log.info("Initiating request %d/%d: %s", req_no, total_reqs, kwargs)
74117
stime = time()
75-
requests.get(**kwargs)
118+
response = requests.get(**kwargs)
76119
etime = time()
120+
if response.status_code != 200:
121+
log.error("Non-OK response:\n%s", response.text)
77122
return {'start_time': stime,
78123
'end_time': etime,
79124
'duration': etime - stime,
80125
'endpoint': kwargs['url'],
126+
'status': response.status_code,
81127
'site': kwargs['params']['url'],
82128
'width': kwargs['params']['width'],
83129
'height': kwargs['params']['height']}

0 commit comments

Comments
 (0)