File: run.py

package info (click to toggle)
python-molotov 2.7-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,268 kB
  • sloc: python: 4,121; makefile: 60
file content (301 lines) | stat: -rw-r--r-- 8,592 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import argparse
import os
import platform
import sys
from importlib import import_module
from importlib.util import module_from_spec, spec_from_file_location

from molotov import __version__
from molotov.api import get_scenario, get_scenarios
from molotov.runner import Runner
from molotov.ui.console import Console
from molotov.util import OptionError, expand_options, printable_error

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


def _parser():
    parser = argparse.ArgumentParser(
        description="Load test.", formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )

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

    parser.add_argument(
        "--single-run",
        action="store_true",
        default=False,
        help="Run once every existing scenario",
    )

    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.0)

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

    parser.add_argument("--sizing-tolerance", help="Sizing tolerance", type=float, default=5.0)

    parser.add_argument("--delay", help="Delay between each worker run", type=float, default=0.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(
        "-f",
        "--fail",
        type=int,
        default=None,
        help="Number of failures required to fail",
    )

    parser.add_argument(
        "-c",
        "--console",
        action="store_true",
        default=False,
        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://localhost: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="+",
    )

    parser.add_argument(
        "--force-shutdown",
        help="Cancel all pending workers on shutdown",
        default=False,
        action="store_true",
    )

    return parser


def main(args=None):
    if args is None:
        parser = _parser()
        args = parser.parse_args()

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

    if args.processes > 1 and os.name == "nt":
        print("The -p/--processes option is unsupported on win32")
        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.0:
            args.ramp_up = 300
        if args.workers == 1:
            args.workers = 500

    run(args)
    return 0


_SIZING = """\

Sizing is over!

Error Ratio %(RATIO).2f %% obtained with %(MAX_WORKERS)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 direct_print(stream, msg):
    stream.write(msg + "\n")
    stream.flush()


def run(args, stream=None):
    if stream is None:
        stream = sys.stdout

    args.shared_console = Console(
        interval=args.console_update,
        simple_console=args.console,
        single_process=args.processes == 1,
    )

    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 Exception 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 Exception:
            direct_print(stream, "Cannot import %r" % args.scenario)
            direct_print(stream, "Try `molotov molotov.dummy`")
            direct_print(stream, "*** Bye ***")
            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:
        direct_print(stream, HELLO)
        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