Exemplo n.º 1
0
    async def _run(self):
        verbose = self.args.verbose
        exception = self.args.exception

        if self.args.single_mode:
            single = get_scenario(self.args.single_mode)
        elif self.args.single_run:
            single = next_scenario()
        else:
            single = None

        self.count = 1
        self.worker_start = _now()

        try:
            options = await self.setup()
        except FixtureError as e:
            self.results["SETUP_FAILED"] += 1
            stop(why=e)
            return

        async with get_session(self.loop, self.console, verbose, self.statsd,
                               **options) as session:
            get_context(session).args = self.args
            get_context(session).worker_id = self.wid
            try:
                await self.session_setup(session)
            except FixtureError as e:
                self.results["SESSION_SETUP_FAILED"] += 1
                stop(why=e)
                return

            while self._may_run():
                step_start = _now()
                get_context(session).step = self.count
                result = await self.step(self.count, session, scenario=single)
                if result == 1:
                    self.results["OK"] += 1
                    self.results["MINUTE_OK"] += 1
                elif result != 0:
                    self.results["FAILED"] += 1
                    self.results["MINUTE_FAILED"] += 1
                    if exception:
                        stop(why=result)

                if not is_stopped() and self._reached_tolerance(step_start):
                    stop()
                    cancellable_sleep.cancel_all()
                    break

                self.count += 1
                if self.args.delay > 0.0:
                    await cancellable_sleep(self.args.delay)
                else:
                    # forces a context switch
                    await asyncio.sleep(0)

            await self.session_teardown(session)
Exemplo n.º 2
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 ***')
Exemplo n.º 3
0
def run(args):
    if not args.quiet:
        log('**** Molotov v%s. Happy breaking! ****' % __version__, pid=None)

    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.verbose > 0 and not args.console:
        print("You have to be in console mode (-c) to use -v")
        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, screen=ui.init_screen)

    if not args.quiet:
        print('SUCCESSES: %(OK)d | FAILURES: %(FAILED)d\r' % res)
        print('*** Bye ***')
Exemplo n.º 4
0
    async def _run(self):
        verbose = self.args.verbose
        exception = self.args.exception

        if self.args.single_mode:
            single = get_scenario(self.args.single_mode)
        else:
            single = None
        self.count = 1
        self.worker_start = _now()
        setup = get_fixture('setup')
        if setup is not None:
            try:
                options = await setup(self.wid, self.args)
            except Exception as e:
                self.console.print_error(e)
                stop()
                return
            if options is None:
                options = {}
            elif not isinstance(options, dict):
                self.console.print('The setup function needs to return a dict')
                stop()
                return
        else:
            options = {}

        ssetup = get_fixture('setup_session')
        steardown = get_fixture('teardown_session')

        async with Session(self.loop, self.console, verbose, self.statsd,
                           **options) as session:
            session.args = self.args
            session.worker_id = self.wid

            if ssetup is not None:
                try:
                    await ssetup(self.wid, session)
                except Exception as e:
                    self.console.print_error(e)
                    stop()
                    return

            while self._may_run():
                step_start = _now()
                session.step = self.count
                result = await self.step(self.count, session, scenario=single)
                if result == 1:
                    self.results['OK'] += 1
                    self.results['MINUTE_OK'] += 1
                elif result == -1:
                    self.results['FAILED'] += 1
                    self.results['MINUTE_FAILED'] += 1
                    if exception:
                        stop()

                if not is_stopped() and self._reached_tolerance(step_start):
                    stop()
                    cancellable_sleep.cancel_all()
                    break

                self.count += 1
                if self.args.delay > 0.:
                    await cancellable_sleep(self.args.delay)
                else:
                    # forces a context switch
                    await asyncio.sleep(0)

            if steardown is not None:
                try:
                    await steardown(self.wid, session)
                except Exception as e:
                    # we can't stop the teardown process
                    self.console.print_error(e)
Exemplo n.º 5
0
async def _worker(num, loop, args, statsd, delay):
    global _STOP
    quiet = args.quiet
    duration = args.duration
    verbose = args.verbose
    exception = args.exception
    console = args.shared_console

    if args.single_mode:
        single = get_scenario(args.single_mode)
    else:
        single = None
    count = 1
    start = _now()
    howlong = 0
    setup = get_fixture('setup')
    if setup is not None:
        try:
            options = await setup(num, args)
        except Exception as e:
            console.print_error(e)
            return
        if options is None:
            options = {}
        elif not isinstance(options, dict):
            console.print('The setup function needs to return a dict')
            return
    else:
        options = {}

    ssetup = get_fixture('setup_session')
    steardown = get_fixture('teardown_session')

    async with Session(loop, console, verbose, statsd, **options) as session:
        session.args = args
        session.worker_id = num

        if ssetup is not None:
            await ssetup(num, session)

        while (howlong < duration and not _STOP and
               not _RESULTS['REACHED'] == 1):
            if args.max_runs and count > args.max_runs:
                break
            current_time = _now()
            howlong = current_time - start
            session.step = count
            result = await step(num, count, session, quiet, verbose,
                                console, single)
            if result == 1:
                _RESULTS['OK'] += 1
                _RESULTS['MINUTE_OK'] += 1
            elif result == -1:
                _RESULTS['FAILED'] += 1
                _RESULTS['MINUTE_FAILED'] += 1
                if exception:
                    _STOP = True
            elif result == 0:
                break

            if _reached_tolerance(current_time, args):
                _STOP = True
                os.kill(os.getpid(), signal.SIGINT)

            count += 1
            if args.delay > 0.:
                await asyncio.sleep(args.delay)
            else:
                # forces a context switch
                await asyncio.sleep(0)

        if steardown is not None:
            try:
                await steardown(num, session)
            except Exception as e:
                # we can't stop the teardown process
                console.print_error(e)
Exemplo n.º 6
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
Exemplo n.º 7
0
Arquivo: run.py Projeto: loads/ailoads
def run(args):
    args.shared_console = SharedConsole(interval=args.console_update)

    if not args.quiet:
        print(HELLO)

    if args.use_extension:
        for extension in args.use_extension:
            if not args.quiet:
                print("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:
                    print('Cannot import %r' % extension)
                    print('\n'.join(printable_error(e)))
                    sys.exit(1)

    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) as e:
            print('Cannot import %r' % args.scenario)
            print('\n'.join(printable_error(e)))
            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 ***')
        if args.fail is not None and res['FAILED'] >= args.fail:
            sys.exit(1)
Exemplo n.º 8
0
async def worker(num, loop, results, args, stream, statsd):
    global _STOP
    quiet = args.quiet
    duration = args.duration
    verbose = args.verbose
    exception = args.exception
    if args.single_mode:
        single = get_scenario(args.single_mode)
    else:
        single = None
    count = 1
    start = _now()
    howlong = 0
    setup = get_fixture('setup')
    if setup is not None:
        try:
            options = await setup(num, args)
        except Exception as e:
            log(e)
            await stream.put('WORKER_STOPPED')
            return
        if options is None:
            options = {}
        elif not isinstance(options, dict):
            log('The setup function needs to return a dict')
            await stream.put('WORKER_STOPPED')
            return
    else:
        options = {}

    ssetup = get_fixture('setup_session')
    steardown = get_fixture('teardown_session')

    async with Session(loop, stream, verbose, statsd, **options) as session:
        session.args = args
        session.worker_id = num

        if ssetup is not None:
            await ssetup(num, session)

        while howlong < duration and not _STOP:
            if args.max_runs and count > args.max_runs:
                break
            howlong = _now() - start
            session.step = count
            result = await step(session, quiet, verbose, stream, single)
            if result == 1:
                results['OK'] += 1
            elif result == -1:
                results['FAILED'] += 1
                if exception:
                    await stream.put('WORKER_STOPPED')
                    _STOP = True
            elif result == 0:
                break
            count += 1

        if steardown is not None:
            try:
                await steardown(num, session)
            except Exception as e:
                # we can't stop the teardown process
                log(e)

    if not _STOP:
        await stream.put('WORKER_STOPPED')