summaryrefslogtreecommitdiff
path: root/main.py
blob: e468379a73b85ebfc4176a25900af4a66d1c9cc4 (plain)
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
#!/usr/bin/env python3
'''
       _/      _/                                                _/
      _/_/    _/    _/_/    _/_/_/  _/_/      _/_/      _/_/_/        _/_/_/
     _/  _/  _/  _/_/_/_/  _/    _/    _/  _/_/_/_/  _/_/      _/  _/_/
    _/    _/_/  _/        _/    _/    _/  _/            _/_/  _/      _/_/
   _/      _/    _/_/_/  _/    _/    _/    _/_/_/  _/_/_/    _/  _/_/_/

Copyright:

   Copyright © 2025 bdunahu <bdunahu@operationnull.com>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Commentary:
Code:
'''
import argparse
import sys
import traceback
from nemesis.nemesis import Nemesis
from nemesis.utils import validate_html, validate_dir

def parse_args():
    parser = argparse.ArgumentParser(
        usage='%(prog)s [args] -- prog'
    )

    parser.add_argument('-i', '--interval',
                        help='The minimum amount of time inbetween \
                        samples in seconds.',
                        metavar='',
                        type=float,
                        default=0.01)
    parser.add_argument('-e', '--experiment-duration',
                        help='The performance experiment duration. Defaults to 3 seconds.',
                        metavar='',
                        type=float,
                        default=3)
    parser.add_argument('-f', '--filename',
                        help='The filename to write results to. Must name an HTML file.',
                        metavar='',
                        type=validate_html,
                        default="results.html")
    parser.add_argument('--include-paths',
                        help='Specify the path(s) containing files to profile. If a file is in this path, it is a candidate for optimization.',
                        nargs="+",
                        type=validate_dir,
                        required=True)
    parser.add_argument('--exclude-paths',
                        help='Specify the path(s) containing files to exclude profile. Takes priority over --include-paths.',
                        nargs="*",
                        type=validate_dir,
                        required=False,
                        default=[])
    parser.add_argument('prog',
                        type=str,
                        nargs='*',
                        help='Path to the python script and its arguments.')
    return parser.parse_args()

the_globals = {
    '__name__': '__main__',
    '__doc__': None,
    '__package__': None,
    '__loader__': globals()['__loader__'],
    '__spec__': None,
    '__annotations__': {},
    '__builtins__': globals()['__builtins__'],
    '__file__': None,
    '__cached__': None,
}

if __name__ == "__main__":
    args = parse_args()
    sys.argv = args.prog
    try:
        with open(args.prog[0], 'r', encoding='utf-8') as fp:
            code = compile(fp.read(), args.prog[0], "exec")
            Nemesis(args.experiment_duration,
                    args.include_paths,
                    args.exclude_paths,
                    args.filename,
                    args.prog[0],
                    args.interval).start()
            exec(code, the_globals)
            Nemesis.stop()
    except Exception:
        traceback.print_exc()