a354e72cf9f60f4c62a76f4c05c6b7b7b394b5bb
[ipdf/code.git] / tools / scaling.py
1 #!/usr/bin/python -u
2
3 #much copy paste such python
4
5 import sys
6 import os
7 from matplotlib.pyplot import *
8 from numpy import *
9 import subprocess
10 import time
11
12 import gpubounds
13
14 def FixedScales(binname, x0=0, y0=0, w0=1, h0=1, s=0.5, steps=100, xz = 0.5, yz = 0.5, testsvg="svg-tests/grid.svg", renderer="gpu", fps=1):
15         accuracy = []
16         performance = []
17         n = open("/dev/null", "w")
18         #n = sys.stderr
19         try:
20                 p = subprocess.Popen(binname + " -s stdin", bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=n, shell=True)
21                 p.stdin.write("%s\n" % renderer)
22                 p.stdin.write("setbounds %s %s %s %s\n" % (str(x0),str(y0),str(w0),str(h0)))
23                 p.stdin.write("loadsvg %s\n" % testsvg)
24                 p.stdin.write("querygpubounds original.dat\n")
25                 p.stdin.write("screenshot original.bmp\n")
26         except Exception, e:
27                 print "%s - Couldn't start - %s" % (binname, str(e))
28                 return {"accuracy" : asarray(accuracy), \
29                         "performance" : asarray(performance)}
30         
31         for i in xrange(steps):
32                 try:
33                         start_time = time.time()
34                         p.stdin.write("clear\n")
35                         p.stdin.write("loop 1 zoom %s %s %s\n" % (str(xz), str(yz), str(s)))
36                         p.stdin.write("loadsvg %s\n" % testsvg)
37                         p.stdin.write("querygpubounds step%d.dat\n" % i)
38                 #while not os.path.isfile("step%d.dat" % i):
39                 #       pass
40                         p.stdin.write("loop %d printspf\n" % fps) # Print an FPS count to signal it is safe to read the file
41                         fpsout = p.stdout.readline().strip(" \r\n").split("\t")
42                 #print(str(fpsout))
43                         p.stdin.write("printbounds\n")
44                         bounds = p.stdout.readline().strip(" \r\n").split("\t")
45                         try:
46                                 bounds = map(float, bounds)
47                         except:
48                                 pass
49                 
50                         performance += [map(float, fpsout) + [time.time() - start_time]]
51                         accuracy += [bounds + [gpubounds.ComputeError("original.dat", "step%d.dat" % i), \
52                                  gpubounds.UniqueBounds("step%d.dat" % i)]]
53                                  
54                         os.unlink("step%d.dat" % i) # Don't need it any more
55                 #print accuracy[-1][-1]
56                         if accuracy[-1][-1] <= 1:
57                                 print "%s - Quit early after %d steps - No precision left" % (binname, i)
58                                 break
59                         if performance[-1][-1] > 60:
60                                 print "%s - Quit early after %d steps - Took too long to render frames" % (binname, i)
61                                 break
62                 except Exception, e:
63                         print "%s - Quit early after %d steps - Exception %s" % (binname, i, str(e))
64                         break
65         
66         try:
67                 p.stdin.write("screenshot final.bmp\n")
68                 p.stdin.write("quit\n")
69                 p.stdin.close()
70         except Exception, e:
71                 print "%s - Couldn't exit - %s" % (binname, str(e))
72                 
73         return {"accuracy" : asarray(accuracy),
74                         "performance" : asarray(performance)}
75
76
77 def TestInvariance(binname, x0=0, y0=0, w0=1, h0=1, s=-1, steps=1000, xz = 400, yz = 300, testsvg="svg-tests/grid.svg", renderer="gpu", fps=1):
78         accuracy = []
79         performance = []
80         n = open("/dev/null", "w")
81         #n = sys.stderr
82         try:
83                 p = subprocess.Popen(binname + " -s stdin", bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=n, shell=True)
84                 p.stdin.write("%s\n" % renderer)
85                 p.stdin.write("setbounds %s %s %s %s\n" % (str(x0),str(y0),str(w0),str(h0)))
86                 p.stdin.write("loadsvg %s\n" % testsvg)
87                 p.stdin.write("querygpubounds original.dat\n")
88                 p.stdin.write("screenshot original.bmp\n")
89                 p.stdin.write("printbounds\n")
90                 bounds_orig = map(float, p.stdout.readline().strip(" \r\n").split("\t"))
91         except Exception, e:
92                 print "%s - Couldn't start - %s" % (binname, str(e))
93                 return {"accuracy" : asarray(accuracy),
94                         "performance" : asarray(performance)}
95         
96         for i in xrange(1,steps,50):
97                 try:
98                         start_time = time.time()
99                         p.stdin.write("loop %d pxzoom %s %s %s\n" % (i, str(xz), str(yz), str(int(s))))
100                         p.stdin.write("printbounds\n")
101                         bounds = map(float, p.stdout.readline().strip(" \r\n").split("\t"))
102                         p.stdin.write("loop %d pxzoom %s %s %s\n" % (i, str(xz), str(yz), str(-int(s))))
103                         p.stdin.write("querygpubounds step%d.dat\n" % i)
104                         while not os.path.isfile("step%d.dat" % i):
105                                 pass
106                         p.stdin.write("loop %d printspf\n" % fps) # Print an FPS count to signal it is safe to read the file
107                         fpsout = p.stdout.readline().strip(" \r\n").split("\t")
108                         #print(str(fpsout))
109                 
110                 
111                         bounds[0] = bounds[0]-bounds_orig[0]
112                         bounds[1] = bounds[1]-bounds_orig[1]
113                         bounds[2] = bounds[2]/bounds_orig[2]
114                         bounds[3] = bounds[3]/bounds_orig[3]
115                         performance += [map(float, fpsout) + [time.time() - start_time]]
116                         accuracy += [bounds + [gpubounds.ComputeError("original.dat", "step%d.dat" % i), \
117                                  gpubounds.UniqueBounds("step%d.dat" % i)]]
118                                  
119                         os.unlink("step%d.dat" % i) # Don't need it any more
120                         if accuracy[-1][-1] <= 10:
121                                 print "%s - Quit early after %d steps - No precision left" % (binname, i)
122                                 break
123                         if performance[-1][-1] > 60:
124                                 print "%s - Quit early after %d steps - Took too long to render frames" % (binname, i)
125                                 break
126                 except Exception, e:
127                         print "%s - Quit early after %d steps - %s" % (binname, i, str(e))
128                         
129         try:
130                 p.stdin.write("screenshot final.bmp\n")
131                 p.stdin.write("quit\n")
132                 p.stdin.close()
133         except Exception, e:
134                 print "%s - Couldn't exit - %s" % (binname, str(e))
135         return {"accuracy" : asarray(accuracy),
136                         "performance" : asarray(performance)}
137
138 if __name__ == "__main__":
139         results = Scaling("./single", xz=0, yz=0, fps=100)
140
141         

UCC git Repository :: git.ucc.asn.au