Skip to content

Commit 8b9cfea

Browse files
committed
benchmark: serve files via twistd
- remove benchmark.file_server - move serve_files function to benchmark.download_sites module
1 parent 70128ec commit 8b9cfea

File tree

3 files changed

+34
-48
lines changed

3 files changed

+34
-48
lines changed

splash/benchmark/benchmark.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import re
2121

2222
import requests
23-
from splash.benchmark.file_server import serve_files
23+
from splash.benchmark.download_sites import serve_files
2424
from splash.tests.utils import SplashServer
2525

2626

@@ -68,8 +68,10 @@ def make_render_png_lua_req(splash, params):
6868
PORT = 8806
6969
#: Combinations of width & height to test.
7070
WIDTH_HEIGHT = [(None, None), (500, None), (None, 500), (500, 500)]
71-
#: Splash log filename.
71+
#: Splash log filename (set to None to put it to stdout).
7272
SPLASH_LOG = 'splash.log'
73+
#: Static file server log filename (set to None to put it to stdout).
74+
FILESERVER_LOG = 'fileserver.log'
7375
#: This script is used to collect maxrss & cpu time from splash process.
7476
GET_PERF_STATS_SCRIPT = """
7577
function main(splash)
@@ -172,7 +174,8 @@ def main():
172174
'--disable-xvfb',
173175
'--max-timeout=600'])
174176

175-
with splash, serve_files(PORT, args.sites_dir):
177+
with splash, serve_files(port=PORT, directory=args.sites_dir,
178+
logfile=FILESERVER_LOG):
176179
start_time = time()
177180
results = parallel_map(invoke_request, generate_requests(splash, args),
178181
args.thread_count)

splash/benchmark/download_sites.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
Site downloader script for Splash benchmark suite.
55
"""
66

7+
from contextlib import contextmanager
78
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
89
import errno
910
import json
1011
import os
1112
import re
1213
import subprocess
1314
from urlparse import urlsplit
15+
import time
1416

17+
import requests
1518
from lxml import html
1619

1720
import w3lib.html
18-
from splash.benchmark.file_server import serve_files
1921
from splash.tests.stress import lua_runonce
2022

2123
SCRIPT_HTML = """
@@ -72,6 +74,31 @@ def preprocess_main_page(sites_dir, url):
7274
return filename
7375

7476

77+
@contextmanager
78+
def serve_files(port, directory, logfile=None):
79+
"""Serve files from specified directory statically in a subprocess."""
80+
command = ['twistd',
81+
'-n', # don't daemonize
82+
'web', # start web component
83+
'--port', str(int(port)),
84+
'--path', os.path.abspath(directory), ]
85+
if logfile is not None:
86+
command += ['--logfile', logfile]
87+
site_server = subprocess.Popen(command)
88+
try:
89+
# It might take some time to bring up the server, wait for up to 10s.
90+
for i in xrange(100):
91+
try:
92+
requests.get('http://localhost:%d' % port)
93+
except requests.ConnectionError:
94+
time.sleep(0.1)
95+
else:
96+
break
97+
yield
98+
finally:
99+
site_server.terminate()
100+
101+
75102
def download_sites(sites_dir, sites):
76103
local_files = [preprocess_main_page(sites_dir, s) for s in sites]
77104

splash/benchmark/file_server.py

-44
This file was deleted.

0 commit comments

Comments
 (0)