/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk
423.2.1 by John Arbash Meinel
Start working on some code to do load testing of loggerhead.
1
# Copyright 2009, 2010, 2011 Canonical Ltd
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
467.2.1 by Toshio Kuratomi
Change to the FSF address in license headers
15
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
16
262.2.1 by Martin Pool
Stub code to allow loggerhead to load as a plugin
17
18
# This file allows loggerhead to be treated as a plugin for bzr.
19
#
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
20
# XXX: Because loggerhead already contains a loggerhead directory, much of the
491.2.1 by Jelmer Vernooij
s/bzrlib/breezy/
21
# code is going to appear loaded at breezy.plugins.loggerhead.loggerhead.
22
# This seems like the easiest thing, because breezy wants the top-level plugin
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
23
# directory to be the module, but when it's used as a library people expect
24
# the source directory to contain a directory called loggerhead.  -- mbp
25
# 20090123
26
27
"""Loggerhead web viewer for Bazaar branches.
28
329.2.1 by Matt Nordhoff
Strip trailing whitespace
29
This provides a new option "--http" to the "bzr serve" command, that
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
30
starts a web server to browse the contents of a branch.
31
"""
262.2.1 by Martin Pool
Stub code to allow loggerhead to load as a plugin
32
437 by John Arbash Meinel
Import sys at the start of loggerhead/__init__.py we use it as part of startup.
33
import sys
34
491.2.44 by Jelmer Vernooij
Add NEWS, drop info.py.
35
version_info = (1, 20, 0) # Keep in sync with loggerhead/__init__.py
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
36
491.2.28 by Jelmer Vernooij
Remove python path hackery.
37
import breezy
38
from breezy import commands
39
40
from breezy.transport import transport_server_registry
41
42
DEFAULT_HOST = '0.0.0.0'
43
DEFAULT_PORT = 8080
44
HELP = ('Loggerhead, a web-based code viewer and server. (default port: %d)' %
45
        (DEFAULT_PORT,))
46
47
48
def serve_http(transport, host=None, port=None, inet=None, client_timeout=None):
49
    # TODO: if we supported inet to pass requests in and respond to them,
50
    #       then it would be easier to test the full stack, but it probably
51
    #       means routing around paste.httpserver.serve which probably
52
    #       isn't testing the full stack
53
    from paste.httpexceptions import HTTPExceptionHandler
54
    from paste.httpserver import serve
55
56
    from .loggerhead.apps.http_head import HeadMiddleware
57
    from .loggerhead.apps.transport import BranchesFromTransportRoot
58
    from .loggerhead.config import LoggerheadConfig
59
    from .loggerhead.main import setup_logging
60
61
    if host is None:
62
        host = DEFAULT_HOST
63
    if port is None:
64
        port = DEFAULT_PORT
65
    argv = ['--host', host, '--port', str(port), '--', transport.base]
66
    if not transport.is_readonly():
67
        argv.insert(0, '--allow-writes')
68
    config = LoggerheadConfig(argv)
69
    setup_logging(config, init_logging=False, log_file=sys.stderr)
70
    app = BranchesFromTransportRoot(transport.base, config)
71
    # Bug #758618, HeadMiddleware seems to break HTTPExceptionHandler from
72
    # actually sending appropriate return codes to the client. Since nobody
73
    # desperately needs HeadMiddleware right now, just ignoring it.
74
    # app = HeadMiddleware(app)
75
    app = HTTPExceptionHandler(app)
76
    serve(app, host=host, port=port)
77
78
transport_server_registry.register('http', serve_http, help=HELP)
79
80
class cmd_load_test_loggerhead(commands.Command):
81
    """Run a load test against a live loggerhead instance.
82
83
    Pass in the name of a script file to run. See loggerhead/load_test.py
84
    for a description of the file format.
85
    """
86
87
    hidden = True
88
    takes_args = ["filename"]
89
90
    def run(self, filename):
91
        from .loggerhead.loggerhead import load_test
92
        script = load_test.run_script(filename)
93
        for thread_id in sorted(script._threads):
94
            worker = script._threads[thread_id][0]
95
            for url, success, time in worker.stats:
96
                self.outf.write(' %5.3fs %s %s\n'
97
                                % (time, str(success)[0], url))
98
99
commands.register_command(cmd_load_test_loggerhead)
100
101
def load_tests(loader, basic_tests, pattern):
491.2.40 by Jelmer Vernooij
Fix some more tests on Python 3.
102
    from .loggerhead.tests import test_suite
103
    basic_tests.addTest(test_suite())
491.2.28 by Jelmer Vernooij
Remove python path hackery.
104
    return basic_tests