| •1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
A library to communicate a menu object set accross DBus and |
| 5 |
track updates and maintain consistency. |
| 6 |
|
| 7 |
Copyright 2010 Canonical Ltd. |
| 8 |
|
| 9 |
Authors: |
| 10 |
Aurélien Gâteau <[email protected]m> |
| 11 |
|
| 12 |
This program is free software: you can redistribute it and/or modify it |
| 13 |
under the terms of either or both of the following licenses: |
| 14 |
|
| 15 |
1) the GNU Lesser General Public License version 3, as published by the |
| 16 |
Free Software Foundation; and/or |
| 17 |
2) the GNU Lesser General Public License version 2.1, as published by |
| 18 |
the Free Software Foundation. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, but |
| 21 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 22 |
MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR |
| 23 |
PURPOSE. See the applicable version of the GNU Lesser General Public |
| 24 |
License for more details. |
| 25 |
|
| 26 |
You should have received a copy of both the GNU Lesser General Public |
| 27 |
License version 3 and version 2.1 along with this program. If not, see |
| 28 |
<http://www.gnu.org/licenses/> |
| 29 |
""" |
| 30 |
import itertools |
| 31 |
import time |
| 32 |
import sys |
| 33 |
from optparse import OptionParser |
| 34 |
from xml.etree import ElementTree as ET |
| 35 |
|
| 36 |
import dbus |
| 37 |
|
| 38 |
DBUS_INTERFACE = "com.canonical.dbusmenu" |
| 39 |
DBUS_SERVICE = "org.dbusmenu.test" |
| 40 |
DBUS_PATH = "/MenuBar" |
| 41 |
|
| 42 |
PROBE_GET_LAYOUT = "GetLayout" |
| 43 |
PROBE_GET_PROPERTIES = "GetProperties" |
| 44 |
PROBE_GET_CHILDREN = "GetChildren" |
| 45 |
PROBES = PROBE_GET_LAYOUT, PROBE_GET_PROPERTIES, PROBE_GET_CHILDREN |
| 46 |
|
| 47 |
class Chrono(object): |
| 48 |
def __init__(self): |
| 49 |
self._time = 0 |
| 50 |
self.restart() |
| 51 |
|
| 52 |
def restart(self): |
| 53 |
new_time = time.time() |
| 54 |
delta = new_time - self._time |
| 55 |
self._time = new_time |
| 56 |
return delta |
| 57 |
|
| 58 |
def elapsed(self): |
| 59 |
return time.time() - self._time |
| 60 |
|
| 61 |
|
| 62 |
def dump_properties(properties, prepend=""): |
| •63 |
for key, value in list(properties.items()): |
| 64 |
print("%s- %s: %s" % (prepend, key, value)) |
| 65 |
|
| 66 |
|
| 67 |
def run_test_sequence(menu, dump=False): |
| 68 |
""" |
| 69 |
Runs the test sequence and returns a dict of method_name: seconds |
| 70 |
""" |
| 71 |
property_names = ["type", "label", "enabled", "icon-name"] |
| 72 |
times = dict() |
| 73 |
chrono = Chrono() |
| 74 |
revision, layout = menu.GetLayout(dbus.Int32(0)) |
| 75 |
times["GetLayout"] = chrono.elapsed() |
| 76 |
if dump: |
| •77 |
print("revision:", revision) |
| 78 |
print("layout:") |
| 79 |
print(layout) |
| 80 |
|
| 81 |
|
| 82 |
tree = ET.fromstring(layout) |
| 83 |
root_id = int(tree.attrib["id"]) |
| 84 |
child_element = tree.find("menu") |
| 85 |
assert child_element is not None |
| 86 |
child_id = int(child_element.attrib["id"]) |
| 87 |
|
| 88 |
chrono.restart() |
| 89 |
children = menu.GetChildren(dbus.Int32(root_id), property_names) |
| 90 |
times["GetChildren"] = chrono.elapsed() |
| 91 |
if dump: |
| •92 |
print("children:") |
| 93 |
for child in children: |
| 94 |
id, properties = child |
| •95 |
print("- %d:" % id) |
| 96 |
dump_properties(properties, prepend=" ") |
| 97 |
|
| 98 |
chrono.restart() |
| 99 |
properties = menu.GetProperties(dbus.Int32(child_id), property_names) |
| 100 |
times["GetProperties"] = chrono.elapsed() |
| 101 |
if dump: |
| •102 |
print("properties:") |
| 103 |
dump_properties(properties) |
| 104 |
|
| 105 |
return times |
| 106 |
|
| 107 |
def create_timing_dict(): |
| •108 |
return dict(list(zip(PROBES, itertools.repeat(0)))) |
| 109 |
|
| 110 |
def print_probe(prefix, name, value, timestamp): |
| 111 |
value = int(value * 1000000) |
| •112 |
print("%(prefix)s.%(name)s:%(value)d@%(timestamp)d" % locals()) |
| 113 |
|
| 114 |
def main(): |
| 115 |
parser = OptionParser(usage = "%prog [options]") |
| 116 |
|
| 117 |
parser.add_option("-c", "--count", dest="count", type=int, default=1, |
| 118 |
help="repeat calls COUNT times", metavar="COUNT") |
| 119 |
parser.add_option("-d", "--dump", dest="dump", action="store_true", default=False, |
| 120 |
help="dump call output to stdout") |
| 121 |
|
| 122 |
(options, args) = parser.parse_args() |
| 123 |
|
| 124 |
bus = dbus.SessionBus() |
| 125 |
proxy = bus.get_object(DBUS_SERVICE, DBUS_PATH) |
| 126 |
menu = dbus.Interface(proxy, dbus_interface=DBUS_INTERFACE) |
| 127 |
|
| 128 |
if options.dump: |
| 129 |
run_test_sequence(menu, dump=True) |
| 130 |
return |
| 131 |
|
| 132 |
cumulated_timings = create_timing_dict() |
| 133 |
min_timings = create_timing_dict() |
| 134 |
max_timings = create_timing_dict() |
| 135 |
for x in range(options.count): |
| 136 |
timings = run_test_sequence(menu) |
| •137 |
for name, timing in list(timings.items()): |
| 138 |
cumulated_timings[name] += timing |
| 139 |
if min_timings[name] == 0 or min_timings[name] > timing: |
| 140 |
min_timings[name] = timing |
| 141 |
if max_timings[name] < timing: |
| 142 |
max_timings[name] = timing |
| 143 |
|
| 144 |
timestamp = int(time.time()) |
| •145 |
for name, timing in list(cumulated_timings.items()): |
| 146 |
print_probe("average", name, timing / options.count, timestamp) |
| •147 |
for name, timing in list(min_timings.items()): |
| 148 |
print_probe("min", name, timing, timestamp) |
| •149 |
for name, timing in list(max_timings.items()): |
| 150 |
print_probe("max", name, timing, timestamp) |
| 151 |
|
| 152 |
return 0 |
| 153 |
|
| 154 |
|
| 155 |
if __name__=="__main__": |
| 156 |
sys.exit(main()) |
| 157 |
|