コード例 #1
0
ファイル: test_sharedconsole.py プロジェクト: sthagen/molotov
    def test_simple_usage(self):
        test_loop = asyncio.get_event_loop()
        stream = io.StringIO()
        console = SharedConsole(interval=0.0, stream=stream)

        async def add_lines():
            console.print("one")
            console.print("two")
            console.print("3")
            try:
                1 + "e"
            except Exception as e:
                console.print_error(e)
                console.print_error(e, sys.exc_info()[2])
            await asyncio.sleep(0.2)
            await console.stop()

        with catch_output() as (stdout, stderr):
            adder = asyncio.ensure_future(add_lines())
            displayer = asyncio.ensure_future(console.display())
            test_loop.run_until_complete(asyncio.gather(adder, displayer))

        stream.seek(0)
        output = stream.read()
        test_loop.close()
        self.assertTrue(
            re.match(OUTPUT, output, re.S | re.M) is not None, output)
コード例 #2
0
ファイル: test_sharedconsole.py プロジェクト: loads/ailoads
    def test_simple_usage(self):
        test_loop = asyncio.get_event_loop()
        console = SharedConsole(interval=0.)

        async def add_lines():
            console.print("one")
            console.print("two")
            console.print("3")
            try:
                1 + 'e'
            except Exception as e:
                console.print_error(e)
                console.print_error(e, sys.exc_info()[2])
            await asyncio.sleep(.2)
            await console.stop()

        with catch_output() as (stdout, stderr):
            adder = asyncio.ensure_future(add_lines())
            displayer = asyncio.ensure_future(console.display())
            test_loop.run_until_complete(asyncio.gather(adder, displayer))

        output = stdout.read()
        test_loop.close()
        self.assertTrue(re.match(OUTPUT, output, re.S | re.M) is not None,
                        output)
コード例 #3
0
    def get_args(self, console=None):
        args = namedtuple('args', 'verbose quiet duration exception')
        args.force_shutdown = False
        args.ramp_up = .0
        args.verbose = 1
        args.quiet = False
        args.duration = 5
        args.exception = True
        args.processes = 1
        args.debug = True
        args.workers = 1
        args.console = True
        args.statsd = False
        args.single_mode = None
        args.max_runs = None
        args.delay = .0
        args.sizing = False
        args.sizing_tolerance = .0
        args.console_update = 0
        args.use_extension = []
        args.fail = None
        args.force_reconnection = False

        if console is None:
            console = SharedConsole(interval=0)
        args.shared_console = console
        return args
コード例 #4
0
ファイル: support.py プロジェクト: sthagen/molotov
    def _async_test(*args, **kw):
        oldloop = asyncio.get_event_loop()
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        loop.set_debug(True)
        console = SharedConsole(interval=0)
        results = SharedCounters(
            "WORKER",
            "REACHED",
            "RATIO",
            "OK",
            "FAILED",
            "MINUTE_OK",
            "MINUTE_FAILED",
            "MAX_WORKERS",
            "SETUP_FAILED",
            "SESSION_SETUP_FAILED",
        )
        kw["loop"] = loop
        kw["console"] = console
        kw["results"] = results

        fut = asyncio.ensure_future(func(*args, **kw))
        try:
            loop.run_until_complete(fut)
        finally:
            loop.stop()
            loop.close()
            asyncio.set_event_loop(oldloop)

        return fut.result()
コード例 #5
0
def run(args):
    args.shared_console = SharedConsole(interval=args.console_update)
    if not args.quiet:
        args.shared_console.print(HELLO)

    if os.path.exists(args.scenario):
        spec = spec_from_file_location("loadtest", args.scenario)
        module = module_from_spec(spec)
        spec.loader.exec_module(module)
    else:
        try:
            import_module(args.scenario)
        except (ImportError, ValueError):
            print('Cannot import %r' % args.scenario)
            sys.exit(1)

    if len(get_scenarios()) == 0:
        print('You need at least one scenario. No scenario was found.')
        print('A scenario with a weight of 0 is ignored')
        sys.exit(1)

    if args.verbose > 0 and args.quiet:
        print("You can't use -q and -v at the same time")
        sys.exit(1)

    if args.single_mode:
        if get_scenario(args.single_mode) is None:
            print("Can't find %r in registered scenarii" % args.single_mode)
            sys.exit(1)

    res = runner(args)

    def _dict(counters):
        res = {}
        for k, v in counters.items():
            if k == 'RATIO':
                res[k] = float(v.value) / 100.
            else:
                res[k] = v.value
        return res

    res = _dict(res)

    if not args.quiet:
        if args.sizing:
            if res['REACHED'] == 1:
                print(_SIZING % res)
            else:
                print('Sizing was not finished. (interrupted)')
        else:
            print('SUCCESSES: %(OK)d | FAILURES: %(FAILED)d\r' % res)
        print('*** Bye ***')
コード例 #6
0
 def _async_test(*args, **kw):
     cofunc = asyncio.coroutine(func)
     oldloop = asyncio.get_event_loop()
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     loop.set_debug(True)
     console = SharedConsole(loop=loop, interval=0)
     kw['loop'] = loop
     kw['console'] = console
     try:
         loop.run_until_complete(cofunc(*args, **kw))
     finally:
         loop.stop()
         loop.close()
         asyncio.set_event_loop(oldloop)
コード例 #7
0
 def _async_test(*args, **kw):
     cofunc = asyncio.coroutine(func)
     oldloop = asyncio.get_event_loop()
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     loop.set_debug(True)
     console = SharedConsole(interval=0)
     results = SharedCounters('WORKER', 'REACHED', 'RATIO', 'OK', 'FAILED',
                              'MINUTE_OK', 'MINUTE_FAILED')
     kw['loop'] = loop
     kw['console'] = console
     kw['results'] = results
     try:
         loop.run_until_complete(cofunc(*args, **kw))
     finally:
         loop.stop()
         loop.close()
         asyncio.set_event_loop(oldloop)
コード例 #8
0
 def _async_test(*args, **kw):
     cofunc = asyncio.coroutine(func)
     oldloop = asyncio.get_event_loop()
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     loop.set_debug(True)
     console = SharedConsole(interval=0)
     results = SharedCounters("WORKER", "REACHED", "RATIO", "OK", "FAILED",
                              "MINUTE_OK", "MINUTE_FAILED")
     kw["loop"] = loop
     kw["console"] = console
     kw["results"] = results
     try:
         loop.run_until_complete(cofunc(*args, **kw))
     finally:
         loop.stop()
         loop.close()
         asyncio.set_event_loop(oldloop)
コード例 #9
0
ファイル: test_sharedconsole.py プロジェクト: sthagen/molotov
import re
import io

from molotov.util import multiprocessing
from molotov.sharedconsole import SharedConsole
from molotov.tests.support import dedicatedloop, catch_output

OUTPUT = """\
one
two
3
TypeError\\("unsupported operand type(.*)?
TypeError\\("unsupported operand type.*"""

# pre-forked variable
_CONSOLE = SharedConsole(interval=0.0)
_PROC = []


def run_worker(input):
    if os.getpid() not in _PROC:
        _PROC.append(os.getpid())
    _CONSOLE.print("hello")
    try:
        3 + ""
    except Exception:
        _CONSOLE.print_error("meh")

    with catch_output() as (stdout, stderr):
        loop = asyncio.new_event_loop()
        fut = asyncio.ensure_future(_CONSOLE.display(), loop=loop)
コード例 #10
0
def run(args, stream=None):
    if stream is None:
        stream = sys.stdout

    args.shared_console = SharedConsole(interval=args.console_update, stream=stream)

    if not args.quiet:
        direct_print(stream, HELLO)

    if args.use_extension:
        for extension in args.use_extension:
            if not args.quiet:
                direct_print(stream, "Loading extension %r" % extension)
            if os.path.exists(extension):
                spec = spec_from_file_location("extension", extension)
                module = module_from_spec(spec)
                spec.loader.exec_module(module)
            else:
                try:
                    import_module(extension)
                except (ImportError, ValueError) as e:
                    direct_print(stream, "Cannot import %r" % extension)
                    direct_print(stream, "\n".join(printable_error(e)))
                    sys.exit(1)

    if os.path.exists(args.scenario):
        sys.path.insert(0, os.path.dirname(args.scenario))
        spec = spec_from_file_location("loadtest", args.scenario)
        module = module_from_spec(spec)
        spec.loader.exec_module(module)
    else:
        try:
            module = import_module(args.scenario)
        except (ImportError, ValueError) as e:
            direct_print(stream, "Cannot import %r" % args.scenario)
            direct_print(stream, "\n".join(printable_error(e)))
            sys.exit(1)
        sys.path.insert(0, os.path.dirname(module.__file__))

    if len(get_scenarios()) == 0:
        direct_print(stream, "You need at least one scenario. No scenario was found.")
        direct_print(stream, "A scenario with a weight of 0 is ignored")
        sys.exit(1)

    if args.verbose > 0 and args.quiet:
        direct_print(stream, "You can't use -q and -v at the same time")
        sys.exit(1)

    if args.single_mode and args.single_run:
        direct_print(stream, "You can't use --singlee-mode and --single-run")
        sys.exit(1)

    if args.single_mode:
        if get_scenario(args.single_mode) is None:
            direct_print(
                stream, "Can't find %r in registered scenarii" % args.single_mode
            )
            sys.exit(1)

    res = Runner(args)()

    def _dict(counters):
        res = {}
        for k, v in counters.items():
            if k == "RATIO":
                res[k] = float(v.value) / 100.0
            else:
                res[k] = v.value
        return res

    res = _dict(res)

    if not args.quiet:
        if args.sizing:
            if res["REACHED"] == 1:
                direct_print(stream, _SIZING % res)
            else:
                direct_print(stream, "Sizing was not finished. (interrupted)")
        else:
            direct_print(stream, "SUCCESSES: %(OK)d | FAILURES: %(FAILED)d\r" % res)
        direct_print(stream, "*** Bye ***")
        if args.fail is not None and res["FAILED"] >= args.fail:
            sys.exit(1)
    return res