This file is indexed.

/usr/lib/python3/dist-packages/molotov/run.py is in python3-molotov 1.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import os
import sys
import argparse
import platform

from importlib import import_module
from importlib.util import spec_from_file_location, module_from_spec

from molotov.runner import Runner
from molotov.api import get_scenarios, get_scenario
from molotov import __version__
from molotov.util import expand_options, OptionError, printable_error
from molotov.sharedconsole import SharedConsole


PYPY = platform.python_implementation() == 'PyPy'


def _parser():
    parser = argparse.ArgumentParser(description='Load test.')

    parser.add_argument('scenario', default="loadtest.py",
                        help="path or module name that contains scenarii",
                        nargs="?")

    parser.add_argument('-s', '--single-mode', default=None, type=str,
                        help="Name of a single scenario to run once.")

    parser.add_argument('--config', default=None, type=str,
                        help='Point to a JSON config file.')

    parser.add_argument('--version', action='store_true', default=False,
                        help='Displays version and exits.')

    parser.add_argument('--debug', action='store_true', default=False,
                        help='Run the event loop in debug mode.')

    parser.add_argument('-v', '--verbose', action='count', default=0,
                        help=('Verbosity level. -v will display '
                              'tracebacks. -vv requests and responses.'))

    parser.add_argument('-w', '--workers', help='Number of workers',
                        type=int, default=1)

    parser.add_argument('--ramp-up', help='Ramp-up time in seconds',
                        type=float, default=0.)

    parser.add_argument('--sizing', help='Autosizing', action='store_true',
                        default=False)

    parser.add_argument('--sizing-tolerance', help='Sizing tolerance',
                        type=float, default=5.)

    parser.add_argument('--delay', help='Delay between each worker run',
                        type=float, default=0.)

    parser.add_argument('--console-update',
                        help='Delay between each console update',
                        type=float, default=0.2)

    parser.add_argument('-p', '--processes', help='Number of processes',
                        type=int, default=1)

    parser.add_argument('-d', '--duration', help='Duration in seconds',
                        type=int, default=86400)

    parser.add_argument('-r', '--max-runs', help='Maximum runs per worker',
                        type=int, default=None)

    parser.add_argument('-q', '--quiet', action='store_true', default=False,
                        help='Quiet')

    parser.add_argument('-x', '--exception', action='store_true',
                        default=False,
                        help='Stop on first failure.')

    parser.add_argument('-c', '--console', action='store_true',
                        default=True,
                        help='Use simple console for feedback')

    parser.add_argument('--statsd', help='Activates statsd',
                        action='store_true', default=False)

    parser.add_argument('--statsd-address', help='Statsd Address',
                        type=str, default="udp://127.0.0.1:8125")

    parser.add_argument('--uvloop', help='Use uvloop', default=False,
                        action='store_true')

    parser.add_argument('--use-extension',
                        help='Imports a module containing Molotov extensions',
                        default=None, type=str, nargs='+')

    return parser


def main():
    parser = _parser()
    args = parser.parse_args()

    if args.version:
        print(__version__)
        sys.exit(0)

    if args.config:
        if args.scenario == 'loadtest.py':
            args.scenario = 'test'

        try:
            expand_options(args.config, args.scenario, args)
        except OptionError as e:
            print(str(e))
            sys.exit(0)

    if args.uvloop:
        if PYPY:
            print("You can't use uvloop with PyPy")     # pragma: no cover
            sys.exit(0)                                 # pragma: no cover

        try:
            import uvloop
        except ImportError:
            print('You need to install uvloop when using --uvloop')
            sys.exit(0)

        import asyncio
        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

    if args.sizing:
        # sizing is just ramping up workers indefinitely until
        # something things break. If the user has not set the values,
        # we do it here with 5 minutes and 500 workers
        if args.ramp_up == 0.:
            args.ramp_up = 300
        if args.workers == 1:
            args.workers = 500

    return run(args)


_SIZING = """\

Sizing is over!

Error Ratio %(RATIO).2f %% obtained with %(WORKER)d workers.

OVERALL: SUCCESSES: %(OK)d | FAILURES: %(FAILED)d
LAST MINUTE: SUCCESSES: %(MINUTE_OK)d | FAILURES: %(MINUTE_FAILED)d
"""

HELLO = '**** Molotov v%s. Happy breaking! ****' % __version__


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 ***')