Skip to content

Commit

Permalink
benchmark: serve files via twistd
Browse files Browse the repository at this point in the history
- remove benchmark.file_server
- move serve_files function to benchmark.download_sites module
  • Loading branch information
immerrr committed Mar 2, 2015
1 parent 70128ec commit 8b9cfea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 48 deletions.
9 changes: 6 additions & 3 deletions splash/benchmark/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import re

import requests
from splash.benchmark.file_server import serve_files
from splash.benchmark.download_sites import serve_files
from splash.tests.utils import SplashServer


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

with splash, serve_files(PORT, args.sites_dir):
with splash, serve_files(port=PORT, directory=args.sites_dir,
logfile=FILESERVER_LOG):
start_time = time()
results = parallel_map(invoke_request, generate_requests(splash, args),
args.thread_count)
Expand Down
29 changes: 28 additions & 1 deletion splash/benchmark/download_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
Site downloader script for Splash benchmark suite.
"""

from contextlib import contextmanager
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import errno
import json
import os
import re
import subprocess
from urlparse import urlsplit
import time

import requests
from lxml import html

import w3lib.html
from splash.benchmark.file_server import serve_files
from splash.tests.stress import lua_runonce

SCRIPT_HTML = """
Expand Down Expand Up @@ -72,6 +74,31 @@ def preprocess_main_page(sites_dir, url):
return filename


@contextmanager
def serve_files(port, directory, logfile=None):
"""Serve files from specified directory statically in a subprocess."""
command = ['twistd',
'-n', # don't daemonize
'web', # start web component
'--port', str(int(port)),
'--path', os.path.abspath(directory), ]
if logfile is not None:
command += ['--logfile', logfile]
site_server = subprocess.Popen(command)
try:
# It might take some time to bring up the server, wait for up to 10s.
for i in xrange(100):
try:
requests.get('http://localhost:%d' % port)
except requests.ConnectionError:
time.sleep(0.1)
else:
break
yield
finally:
site_server.terminate()


def download_sites(sites_dir, sites):
local_files = [preprocess_main_page(sites_dir, s) for s in sites]

Expand Down
44 changes: 0 additions & 44 deletions splash/benchmark/file_server.py

This file was deleted.

0 comments on commit 8b9cfea

Please sign in to comment.