示例#1
0
def run(num, duration, method=None, pre_hook=None, post_hook=None, quiet=False):
    print ""
    print "pre_hook", pre_hook
    print "method", method

    if pre_hook is not None:
        pre_hook = resolve_name(pre_hook)

    if method is not None:
        method = resolve_name(method)

    if post_hook is not None:
        post_hook = resolve_name(post_hook)

    if pre_hook:
        print "\n pre_hook start!!!"
        pre_res = RunResults(num, quiet=True)
        call_util(pre_hook, num, pre_res, duration)
        print "\n pre_hook finish!!!"


    if method:
        print "\n method start!!!"
        res = RunResults(num, quiet)
        call_util(method, num, res, duration)
        print "\n method finish!!!"

    if post_hook:
        post_pos = RunResults(num, quiet=True)
        call_util(post_hook, num, post_pos, duration)

    return res
示例#2
0
文件: _boom.py 项目: rbas/boom
def run(
    url,
    num=1,
    duration=None,
    method="GET",
    data=None,
    ct="text/plain",
    auth=None,
    concurrency=1,
    headers=None,
    hook=None,
):

    if headers is None:
        headers = {}

    if "content-type" not in headers:
        headers["Content-Type"] = ct

    if data is not None and data.startswith("py:"):
        callable = data[len("py:") :]
        data = resolve_name(callable)

    method = getattr(requests, method.lower())
    options = {"headers": headers}

    if hook is not None:
        options["hook"] = resolve_name(hook)

    if data is not None:
        options["data"] = data

    if auth is not None:
        options["auth"] = tuple(auth.split(":", 1))

    pool = Pool(concurrency)

    start = time.time()
    jobs = None

    try:
        if num is not None:
            global _progress_bar
            _progress_bar = AnimatedProgressBar(end=num, width=65)
            jobs = [pool.spawn(onecall, method, url, **options) for i in range(num)]
            pool.join()
        else:
            with gevent.Timeout(duration, False):
                jobs = []
                while True:
                    jobs.append(pool.spawn(onecall, method, url, **options))

                pool.join()
    finally:
        global _total
        _total = time.time() - start

    return [job.value for job in jobs if job.value]
示例#3
0
文件: _boom.py 项目: Natim/boom
def run(
    url, num=1, duration=None, method='GET', data=None, ct='text/plain',
        auth=None, concurrency=1, headers=None, pre_hook=None, post_hook=None,
        quiet=False):

    if headers is None:
        headers = {}

    if 'content-type' not in headers:
        headers['Content-Type'] = ct

    if data is not None and data.startswith('py:'):
        callable = data[len('py:'):]
        data = resolve_name(callable)

    method = getattr(requests, method.lower())
    options = {'headers': headers}

    if pre_hook is not None:
        options['pre_hook'] = resolve_name(pre_hook)

    if post_hook is not None:
        options['post_hook'] = resolve_name(post_hook)

    if data is not None:
        options['data'] = data

    if auth is not None:
        options['auth'] = tuple(auth.split(':', 1))

    pool = Pool(concurrency)

    start = time.time()
    jobs = None

    res = RunResults(num, quiet)

    try:
        if num is not None:
            jobs = [pool.spawn(onecall, method, url, res, **options)
                    for i in range(num)]
            pool.join()
        else:
            with gevent.Timeout(duration, False):
                jobs = []
                while True:
                    jobs.append(pool.spawn(onecall, method, url, res,
                                           **options))
                pool.join()
    except KeyboardInterrupt:
        # In case of a keyboard interrupt, just return whatever already got
        # put into the result object.
        pass
    finally:
        res.total_time = time.time() - start

    return res
示例#4
0
def run(
    url, num=1, duration=None, method='GET', data=None, ct='text/plain',
        auth=None, concurrency=1, headers=None, pre_hook=None, post_hook=None,
        quiet=False):

    if headers is None:
        headers = {}

    if 'content-type' not in headers:
        headers['Content-Type'] = ct

    if data is not None and data.startswith('py:'):
        callable = data[len('py:'):]
        data = resolve_name(callable)

    method = getattr(requests, method.lower())
    options = {'headers': headers}

    if pre_hook is not None:
        options['pre_hook'] = resolve_name(pre_hook)

    if post_hook is not None:
        options['post_hook'] = resolve_name(post_hook)

    if data is not None:
        options['data'] = data

    if auth is not None:
        options['auth'] = tuple(auth.split(':', 1))

    pool = Pool(concurrency)

    start = time.time()
    jobs = None

    res = RunResults(num, quiet)

    try:
        if num is not None:
            jobs = [pool.spawn(onecall, method, url, res, **options)
                    for i in range(num)]
            pool.join()
        else:
            with gevent.Timeout(duration, False):
                jobs = []
                while True:
                    jobs.append(pool.spawn(onecall, method, url, res,
                                           **options))
                pool.join()
    except KeyboardInterrupt:
        # In case of a keyboard interrupt, just return whatever already got
        # put into the result object.
        pass
    finally:
        res.total_time = time.time() - start

    return res
示例#5
0
文件: _boom.py 项目: acdha/boom
def run(url, num=1, duration=None, method='GET', data=None, ct='text/plain',
        auth=None, concurrency=1, headers=None):

    if headers is None:
        headers = {}

    if 'content-type' not in headers:
        headers['Content-Type'] = ct

    if data is not None and data.startswith('py:'):
        callable = data[len('py:'):]
        data = resolve_name(callable)

    method = getattr(requests, method.lower())
    options = {'headers': headers}

    if data is not None:
        options['data'] = data

    if auth is not None:
        options['auth'] = tuple(auth.split(':', 1))

    pool = Pool(concurrency)

    if num is not None:
        for i in range(num):
            pool.spawn(onecall, method, url, **options)

        pool.join()
    else:
        with gevent.Timeout(duration, False):
            while True:
                pool.spawn(onecall, method, url, **options)

            pool.join()
示例#6
0
    def __init__(self, import_name, exception):
        self.import_name = import_name
        self.exception = exception

        msg = (
            'import_string() failed for %r. Possible reasons are:\n\n'
            '- missing __init__.py in a package;\n'
            '- package or module path not included in sys.path;\n'
            '- duplicated package or module name taking precedence in '
            'sys.path;\n'
            '- missing module, class, function or variable;\n\n'
            'Debugged import:\n\n%s\n\n'
            'Original exception:\n\n%s: %s')

        name = ''
        tracked = []
        for part in import_name.replace(':', '.').split('.'):
            name += (name and '.') + part
            imported = resolve_name(name, silent=True)
            if imported:
                tracked.append((name, getattr(imported, '__file__', None)))
            else:
                track = ['- %r found in %r.' % (n, i) for n, i in tracked]
                track.append('- %r not found.' % name)
                msg = msg % (import_name, '\n'.join(track),
                             exception.__class__.__name__, str(exception))
                break

        ImportError.__init__(self, msg)
示例#7
0
文件: _boom.py 项目: pearkes/boom
def run(url, num=1, duration=None, method='GET', data=None, ct='text/plain',
        auth=None, concurrency=1, headers=None, hook=None):

    if headers is None:
        headers = {}

    if 'content-type' not in headers:
        headers['Content-Type'] = ct

    if data is not None and data.startswith('py:'):
        callable = data[len('py:'):]
        data = resolve_name(callable)

    method = getattr(requests, method.lower())
    options = {'headers': headers}

    if hook is not None:
        options['hook'] = resolve_name(hook)

    if data is not None:
        options['data'] = data

    if auth is not None:
        options['auth'] = tuple(auth.split(':', 1))

    pool = Pool(concurrency)

    start = time.time()
    jobs = None

    try:
        if num is not None:
            jobs = [pool.spawn(onecall, method, url, **options)
                    for i in range(num)]
            pool.join()
        else:
            with gevent.Timeout(duration, False):
                jobs = []
                while True:
                    jobs.append(pool.spawn(onecall, method, url, **options))

                pool.join()
    finally:
        global _total
        _total = time.time() - start

    return [job.value for job in jobs if job.value]