1
# Copyright 2009, 2010 Canonical Ltd
1
# Copyright 2009, 2010, 2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
38
40
if __name__ == 'bzrlib.plugins.loggerhead':
40
42
from bzrlib.api import require_any_api
43
from bzrlib import commands
42
45
require_any_api(bzrlib, bzr_compatible_versions)
48
51
HELP = ('Loggerhead, a web-based code viewer and server. (default port: %d)' %
51
def setup_logging(config):
55
logger = logging.getLogger('loggerhead')
56
handler = logging.StreamHandler(sys.stderr)
57
handler.setLevel(logging.DEBUG)
58
logger.addHandler(handler)
59
logging.getLogger('simpleTAL').addHandler(handler)
60
logging.getLogger('simpleTALES').addHandler(handler)
62
54
def _ensure_loggerhead_path():
63
55
"""Ensure that you can 'import loggerhead' and get the root."""
64
56
# loggerhead internal code will try to 'import loggerhead', so
70
62
sys.path.append(os.path.dirname(__file__))
72
64
def serve_http(transport, host=None, port=None, inet=None):
65
# TODO: if we supported inet to pass requests in and respond to them,
66
# then it would be easier to test the full stack, but it probably
67
# means routing around paste.httpserver.serve which probably
68
# isn't testing the full stack
73
69
from paste.httpexceptions import HTTPExceptionHandler
74
70
from paste.httpserver import serve
76
72
_ensure_loggerhead_path()
74
from loggerhead.apps.http_head import HeadMiddleware
78
75
from loggerhead.apps.transport import BranchesFromTransportRoot
79
76
from loggerhead.config import LoggerheadConfig
77
from loggerhead.main import setup_logging
82
80
host = DEFAULT_HOST
86
84
if not transport.is_readonly():
87
85
argv.insert(0, '--allow-writes')
88
86
config = LoggerheadConfig(argv)
87
setup_logging(config, init_logging=False, log_file=sys.stderr)
90
88
app = BranchesFromTransportRoot(transport.base, config)
89
# Bug #758618, HeadMiddleware seems to break HTTPExceptionHandler from
90
# actually sending appropriate return codes to the client. Since nobody
91
# desperately needs HeadMiddleware right now, just ignoring it.
92
# app = HeadMiddleware(app)
91
93
app = HTTPExceptionHandler(app)
92
94
serve(app, host=host, port=port)
94
96
transport_server_registry.register('http', serve_http, help=HELP)
98
class cmd_load_test_loggerhead(commands.Command):
99
"""Run a load test against a live loggerhead instance.
101
Pass in the name of a script file to run. See loggerhead/load_test.py
102
for a description of the file format.
105
takes_args = ["filename"]
107
def run(self, filename):
108
from bzrlib.plugins.loggerhead.loggerhead import load_test
109
script = load_test.run_script(filename)
110
for thread_id in sorted(script._threads):
111
worker = script._threads[thread_id][0]
112
for url, success, time in worker.stats:
113
self.outf.write(' %5.3fs %s %s\n'
114
% (time, str(success)[0], url))
116
commands.register_command(cmd_load_test_loggerhead)
96
118
def load_tests(standard_tests, module, loader):
97
119
_ensure_loggerhead_path()
98
120
standard_tests.addTests(loader.loadTestsFromModuleNames(