/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
491.2.54 by Jelmer Vernooij
Try local loggerhead first, then fall back to system one.
56
    try:
57
        from .loggerhead.apps.http_head import HeadMiddleware
58
        from .loggerhead.apps.transport import BranchesFromTransportRoot
59
        from .loggerhead.config import LoggerheadConfig
60
        from .loggerhead.main import setup_logging
61
    except ImportError:
62
        from loggerhead.apps.http_head import HeadMiddleware
63
        from loggerhead.apps.transport import BranchesFromTransportRoot
64
        from loggerhead.config import LoggerheadConfig
65
        from loggerhead.main import setup_logging
491.2.28 by Jelmer Vernooij
Remove python path hackery.
66
67
    if host is None:
68
        host = DEFAULT_HOST
69
    if port is None:
70
        port = DEFAULT_PORT
71
    argv = ['--host', host, '--port', str(port), '--', transport.base]
72
    if not transport.is_readonly():
73
        argv.insert(0, '--allow-writes')
74
    config = LoggerheadConfig(argv)
75
    setup_logging(config, init_logging=False, log_file=sys.stderr)
76
    app = BranchesFromTransportRoot(transport.base, config)
77
    # Bug #758618, HeadMiddleware seems to break HTTPExceptionHandler from
78
    # actually sending appropriate return codes to the client. Since nobody
79
    # desperately needs HeadMiddleware right now, just ignoring it.
80
    # app = HeadMiddleware(app)
81
    app = HTTPExceptionHandler(app)
82
    serve(app, host=host, port=port)
83
84
transport_server_registry.register('http', serve_http, help=HELP)
85
86
class cmd_load_test_loggerhead(commands.Command):
87
    """Run a load test against a live loggerhead instance.
88
89
    Pass in the name of a script file to run. See loggerhead/load_test.py
90
    for a description of the file format.
91
    """
92
93
    hidden = True
94
    takes_args = ["filename"]
95
96
    def run(self, filename):
491.2.54 by Jelmer Vernooij
Try local loggerhead first, then fall back to system one.
97
        try:
98
            from .loggerhead.loggerhead import load_test
99
        except ImportError:
100
            from loggerhead.loggerhead import load_test
491.2.28 by Jelmer Vernooij
Remove python path hackery.
101
        script = load_test.run_script(filename)
102
        for thread_id in sorted(script._threads):
103
            worker = script._threads[thread_id][0]
104
            for url, success, time in worker.stats:
105
                self.outf.write(' %5.3fs %s %s\n'
106
                                % (time, str(success)[0], url))
107
108
commands.register_command(cmd_load_test_loggerhead)
109
110
def load_tests(loader, basic_tests, pattern):
491.2.54 by Jelmer Vernooij
Try local loggerhead first, then fall back to system one.
111
    try:
112
        from .loggerhead.tests import test_suite
113
    except ImportError:
114
        from loggerhead.tests import test_suite
491.2.40 by Jelmer Vernooij
Fix some more tests on Python 3.
115
    basic_tests.addTest(test_suite())
491.2.28 by Jelmer Vernooij
Remove python path hackery.
116
    return basic_tests