File: benchmark.py

package info (click to toggle)
python-oslo.rootwrap 7.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 444 kB
  • sloc: python: 2,155; makefile: 18; sh: 14
file content (132 lines) | stat: -rw-r--r-- 3,357 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
# Copyright (c) 2014 Mirantis Inc.
# All Rights Reserved.
#
#    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.

import atexit
import math
import os
import subprocess
import sys
import timeit

from oslo_rootwrap import client

config_path = "rootwrap.conf"
num_iterations = 100


def run_plain(cmd):
    obj = subprocess.Popen(
        cmd,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    out, err = obj.communicate()
    out = os.fsdecode(out)
    err = os.fsdecode(err)
    return obj.returncode, out, err


def run_sudo(cmd):
    return run_plain(["sudo"] + cmd)


def run_rootwrap(cmd):
    return run_plain(
        [
            "sudo",
            sys.executable,
            "-c",
            "from oslo_rootwrap import cmd; cmd.main()",
            config_path,
        ]
        + cmd
    )


run_daemon = client.Client(
    [
        "sudo",
        sys.executable,
        "-c",
        "from oslo_rootwrap import cmd; cmd.daemon()",
        config_path,
    ]
).execute


def run_one(runner, cmd):
    def __inner():
        code, out, err = runner(cmd)
        assert err == "", "Stderr not empty:\n" + err
        assert code == 0, "Command failed"

    return __inner


runners = [
    ("{0}", run_plain),
    ("sudo {0}", run_sudo),
    ("sudo rootwrap conf {0}", run_rootwrap),
    ("daemon.run('{0}')", run_daemon),
]


def get_time_string(sec):
    if sec > 0.9:
        return f"{sec:7.3f}s "
    elif sec > 0.0009:
        return f"{sec * 1000.0:7.3f}ms"
    else:
        return f"{sec * 1000000.0:7.3f}us"


def run_bench(cmd, runners):
    strcmd = ' '.join(cmd)
    max_name_len = max(len(name) for name, _ in runners) + len(strcmd) - 3
    print(f"Running '{strcmd}':")
    print(
        "{0:^{1}} :".format("method", max_name_len),
        "".join(map("{:^10}".format, ["min", "avg", "max", "dev"])),
    )
    for name, runner in runners:
        results = timeit.repeat(
            run_one(runner, cmd), repeat=num_iterations, number=1
        )
        avg = sum(results) / num_iterations
        min_ = min(results)
        max_ = max(results)
        dev = math.sqrt(sum((r - avg) ** 2 for r in results) / num_iterations)
        print(
            "{0:>{1}} :".format(name.format(strcmd), max_name_len),
            " ".join(map(get_time_string, [min_, avg, max_, dev])),
        )


def main():
    os.chdir(os.path.dirname(__file__))
    code, _, _ = run_sudo(["-vn"])
    if code:
        print("We need you to authorize with sudo to run this benchmark")
        run_sudo(["-v"])

    run_bench(["ip", "a"], runners)
    run_sudo(["ip", "netns", "add", "bench_ns"])
    atexit.register(run_sudo, ["ip", "netns", "delete", "bench_ns"])
    run_bench('ip netns exec bench_ns ip a'.split(), runners[1:])


if __name__ == "__main__":
    main()