mesa-demos: NOTE! Default branch is now main
[mesa-demos.git] / src / tests / api_speed.py
blob0ddaf5836c901d6b55e628c3764d51b2be8a9bfa
1 #!/usr/bin/env python2
3 # (C) Copyright IBM Corporation 2004
4 # All Rights Reserved.
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
15 # Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 # IN THE SOFTWARE.
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
29 # This script is used to run api_speed against several different libGL
30 # libraries and compare the results. See the show_usage function for more
31 # details on how to use it.
34 import re, os, sys, getopt
36 class results:
37 def process_file(self, f):
38 self.cycles = {}
39 self.iterations = -1
41 for line in f.readlines():
42 m = re.match("(\d+) calls to (.{20}) required (\d+) cycles.", line)
44 if self.iterations != -1 and int(m.group(1)) != self.iterations:
45 raise
47 # This could be done with lstrip, but the version of
48 # the Python library on my system doesn't have it.
49 # The installed version of Python is quite old. :(
51 temp = m.group(2)
52 function_name = None
53 for i in range(len(temp)):
54 if temp[i] != ' ':
55 function_name = temp[i:]
56 break
58 if function_name == None:
59 raise
61 self.cycles[ function_name ] = int(m.group(3))
62 self.iterations = int(m.group(1))
65 def show_results(self):
66 for name in self.cycles:
67 print "%s -> %f" % (name, float(self.cycles[name]) / self.iterations)
70 def compare_results(self, other):
71 for name in self.cycles:
72 if other.cycles.has_key(name):
73 a = float(self.cycles[name]) / float(self.iterations)
74 b = float(other.cycles[name]) / float(other.iterations)
75 if abs( a ) < 0.000001:
76 print "a = %f, b = %f" % (a, b)
77 else:
78 p = (100.0 * b / a) - 100.0
79 print "%- 20s %7.2f - %7.2f = % -6.2f (%+.1f%%)" % (name, a, b, a - b, p)
80 return
83 def make_execution_string(lib, iterations):
84 if lib == None:
85 return "./api_speed %u" % (iterations)
86 else:
87 return "LD_PRELOAD=%s ./api_speed %u" % (lib, iterations)
90 def show_usage():
91 print """Usage: %s [-i iterations] {library ...}
93 The full path to one or more libGL libraries (including the full name of the
94 library) can be included on the command-line. Each library will be tested,
95 and the results compared. The first library listed will be used as the
96 "base line" for all comparisons.""" % (sys.argv[0])
97 sys.exit(1)
100 if __name__ == '__main__':
101 try:
102 (args, trail) = getopt.getopt(sys.argv[1:], "i:")
103 except Exception,e:
104 show_usage()
106 iterations = 1000000
107 try:
108 for (arg,val) in args:
109 if arg == "-i":
110 iterations = int(val)
111 except Exception,e:
112 show_usage()
115 # If no libraries were specifically named, just run the test against
116 # the default system libGL.
118 if len(trail) == 0:
119 trail.append(None)
122 result_array = []
123 names = []
125 for lib in trail:
126 s = make_execution_string( lib, iterations )
127 r = results()
128 r.process_file( os.popen(s) )
129 names.append(lib)
130 result_array.append(r)
133 # If the test was only run against one library, just show the results
134 # of the test run. Otherwise, compare each successive run against
135 # the first run.
137 if len( result_array ) == 1:
138 result_array[0].show_results()
139 else:
140 for i in range(1, len( result_array )):
141 print "%s vs. %s" % (names[0], names[i])
142 result_array[0].compare_results( result_array[i] )
143 print ""