22
22
from splash .benchmark .file_server import serve_files
23
23
from splash .tests .utils import SplashServer
24
24
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
+
25
66
#: Port at which static pages will be served.
26
67
PORT = 8806
27
68
#: Static pages to be used in the benchmark.
28
69
PAGES = glob ('localhost_8806/*.html' )
29
70
#: Combinations of width & height to test.
30
71
WIDTH_HEIGHT = [(None , None ), (500 , None ), (None , 500 ), (500 , 500 )]
31
- # XXX: add benchmark of different API endpoints.
32
72
SPLASH_LOG = 'splash.log'
33
73
#: This script is used to collect maxrss & cpu time from splash process.
34
74
GET_PERF_STATS_SCRIPT = """
37
77
end
38
78
"""
39
79
80
+
40
81
parser = ArgumentParser (description = __doc__ ,
41
82
formatter_class = ArgumentDefaultsHelpFormatter )
42
83
parser .add_argument ('--seed' , type = int , default = 1234 , help = 'PRNG seed number' )
@@ -53,10 +94,12 @@ def generate_requests(splash, args):
53
94
for i in xrange (args .request_count ):
54
95
page = rng .choice (PAGES )
55
96
width , height = rng .choice (WIDTH_HEIGHT )
97
+ req_factory = rng .choice (REQ_FACTORIES )
56
98
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 ))
60
103
61
104
62
105
def parallel_map (func , iterable , thread_count ):
@@ -72,12 +115,15 @@ def invoke_request(invoke_args):
72
115
req_no , total_reqs , kwargs = invoke_args
73
116
log .info ("Initiating request %d/%d: %s" , req_no , total_reqs , kwargs )
74
117
stime = time ()
75
- requests .get (** kwargs )
118
+ response = requests .get (** kwargs )
76
119
etime = time ()
120
+ if response .status_code != 200 :
121
+ log .error ("Non-OK response:\n %s" , response .text )
77
122
return {'start_time' : stime ,
78
123
'end_time' : etime ,
79
124
'duration' : etime - stime ,
80
125
'endpoint' : kwargs ['url' ],
126
+ 'status' : response .status_code ,
81
127
'site' : kwargs ['params' ]['url' ],
82
128
'width' : kwargs ['params' ]['width' ],
83
129
'height' : kwargs ['params' ]['height' ]}
0 commit comments