Move files from gst-devtools into the "subprojects/gst-devtools/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-devtools / tracer / tracer / structure_perf.py
1 import timeit
2
3 from structure import Structure
4 from gi.repository import Gst
5 Gst.init(None)
6
7 PLAIN_STRUCTURE = r'thread-rusage, thread-id=(guint64)37268592, ts=(guint64)79416000, average-cpuload=(uint)1000, current-cpuload=(uint)1000, time=(guint64)79418045;'
8 NESTED_STRUCTURE = r'latency.class, src=(structure)"scope\,\ type\=\(type\)gchararray\,\ related-to\=\(GstTracerValueScope\)GST_TRACER_VALUE_SCOPE_PAD\;", sink=(structure)"scope\,\ type\=\(type\)gchararray\,\ related-to\=\(GstTracerValueScope\)GST_TRACER_VALUE_SCOPE_PAD\;", time=(structure)"value\,\ type\=\(type\)guint64\,\ description\=\(string\)\"time\\\ it\\\ took\\\ for\\\ the\\\ buffer\\\ to\\\ go\\\ from\\\ src\\\ to\\\ sink\\\ ns\"\,\ flags\=\(GstTracerValueFlags\)GST_TRACER_VALUE_FLAGS_AGGREGATED\,\ min\=\(guint64\)0\,\ max\=\(guint64\)18446744073709551615\;";'
9
10 NAT_STRUCTURE = Structure(PLAIN_STRUCTURE)
11 GI_STRUCTURE = Gst.Structure.from_string(PLAIN_STRUCTURE)[0]
12
13
14 # native python impl
15
16 def nat_parse_plain():
17     s = Structure(PLAIN_STRUCTURE)
18
19
20 def nat_parse_nested():
21     s = Structure(NESTED_STRUCTURE)
22
23
24 def nat_get_name():
25     return NAT_STRUCTURE.name
26
27
28 def nat_get_value():
29     return NAT_STRUCTURE.values['thread-id']
30
31
32 # gstreamer impl via gi
33
34 def gi_parse_plain():
35     s = Gst.Structure.from_string(PLAIN_STRUCTURE)[0]
36
37
38 def gi_parse_nested():
39     s = Gst.Structure.from_string(NESTED_STRUCTURE)[0]
40
41
42 def gi_get_name():
43     return GI_STRUCTURE.get_name()
44
45
46 def gi_get_value():
47     return GI_STRUCTURE.get_value('thread-id')
48
49
50 # perf test
51
52 def perf(method, n, flavor):
53     t = timeit.timeit(method + '()', 'from __main__ import ' + method, number=n)
54     print("%6s: %lf s, (%lf calls/s)" % (flavor, t, (n / t)))
55
56
57 if __name__ == '__main__':
58     import argparse
59     parser = argparse.ArgumentParser()
60     parser.add_argument('-i', '--iterations', default=10000, type=int,
61                         help='number of iterations (default: 10000)')
62     args = parser.parse_args()
63     n = args.iterations
64
65     print("parse_plain:")
66     t = perf('nat_parse_plain', n, 'native')
67     t = perf('gi_parse_plain', n, 'gi')
68
69     print("parse_nested:")
70     t = perf('nat_parse_nested', n, 'native')
71     t = perf('gi_parse_nested', n, 'gi')
72
73     print("get_name:")
74     t = perf('nat_get_name', n, 'native')
75     t = perf('gi_get_name', n, 'gi')
76
77     print("get_value:")
78     t = perf('nat_get_value', n, 'native')
79     t = perf('gi_get_value', n, 'gi')