Contact: fumanchu@aminus.org

Log in as guest/misc to create tickets

root/test_stopwatch.py

Revision 178 (checked in by fumanchu, 2 years ago)

stopwatch: more stable trace hooking

  • Property svn:eol-style set to native
Line 
1 """Test stopwatch.py"""
2
3 import datetime
4 import os
5 localDir = os.path.dirname(__file__)
6 import sys
7 import threading
8 import time
9 import unittest
10
11 import stopwatch
12
13 class Thing(object):
14     @stopwatch.autowatch()
15     def f(self, a, b, c):
16         time.sleep(0.2)
17         for i in range(b):
18             x = c()
19         a += b
20         return a
21 thing = Thing()
22
23 @stopwatch.autowatch()
24 def g(x, y, z):
25     time.sleep(0.2)
26     for i in range(y):
27         z()
28     x += y
29     return x
30
31 h_sw = stopwatch.Stopwatch()
32 def h():
33     h_sw.click()
34     time.sleep(0.01)
35     h_sw.click('sleep')
36     for i in range(3):
37         x = dict()
38         h_sw.click('dict')
39     h_sw.reset()
40
41 a_sw = stopwatch.Stopwatch()
42 def a():
43     for i in range(5):
44         a_sw.click()
45         a_sw.click(u'foo')
46         if (i % 2):
47             a_sw.click('urk')
48         a_sw.click(u'bar')
49         a_sw.reset()
50
51 class Foo(object):
52     @stopwatch.autowatch()
53     def foo(self, a, b, c):
54         time.sleep(0.2)
55         for i in range(b):
56             x = c()
57         a += b
58         return a
59
60
61 class StopwatchTests(unittest.TestCase):
62    
63     def test_average(self):
64         a_sw.clear()
65         a_sw.columns = []
66         a()
67         self.assertEqual(len(a_sw.average()), 3)
68    
69     def test_occasional_click_errors(self):
70         # First, test errors when columns are not predefined.
71         # The 'urk' clicks should be discarded silently.
72         a_sw.clear()
73         a_sw.columns = []
74         a()
75         self.assertEqual(a_sw.columns, ['Point 0', 'foo', 'bar'])
76         self.assertEqual(a_sw.errors.keys(), ['urk'])
77         self.assertEqual([len(r) for r in a_sw.races], [3] * 5)
78    
79     def test_occasional_predefined_clicks(self):
80         a_sw.clear()
81         a_sw.columns = ['start', 'foo', 'urk', 'bar']
82         a()
83         self.assertEqual(a_sw.columns, ['start', 'foo', 'urk', 'bar'])
84         self.assertEqual(a_sw.errors, {})
85         self.assertEqual([len(r) for r in a_sw.races], [4] * 5)
86    
87     def test_csv_gen(self):
88         h_sw.clear()
89         for i in range(4):
90             h()
91         csv = list(h_sw.csv_gen(minimum=0.02))
92         self.assertEqual(csv[0], '"sleep","dict","Extra"' + os.linesep)
93         for race, line in zip(h_sw.races, csv[1:]):
94             self.assertEqual(
95                 line, ",".join(["%f" % (td.seconds + (td.microseconds / 1e6))
96                                 for td in race[1:] + [datetime.timedelta(0)]
97                                ]) + os.linesep
98                 )
99
100
101 class AutowatchTests(unittest.TestCase):
102    
103     def test_bare_func(self):
104         for i in range(3):
105             g(i, 10, dict)
106         expected = [
107             "24:def g(x, y, z):",
108             "25:    time.sleep(0.2)",
109             "26:    for i in range(y):",
110             "27:        z()",
111             "28:    x += y",
112             "29:    return x"]
113         self.assertEqual(g.watch.columns, expected)
114         self.assertEqual([len(r) for r in g.watch.races], [6] * 3)
115    
116     def test_instancemethod(self):
117         for i in range(3):
118             thing.f(i, 300, dict)
119         expected = [
120             "15:    def f(self, a, b, c):",
121             "16:        time.sleep(0.2)",
122             "17:        for i in range(b):",
123             "18:            x = c()",
124             "19:        a += b",
125             "20:        return a"]
126         self.assertEqual(Thing.f.watch.columns, expected)
127         self.assertEqual([len(r) for r in Thing.f.watch.races], [6] * 3)
128    
129     def test_threaded_instancemethod(self):
130         foo = Foo()
131         ts = [threading.Thread(target=foo.foo, args=(i, 300, dict))
132               for i in range(10)]
133         for t in ts:
134             t.start()
135         for t in ts:
136             t.join()
137         expected = [
138             "53:    def foo(self, a, b, c):",
139             "54:        time.sleep(0.2)",
140             "55:        for i in range(b):",
141             "56:            x = c()",
142             "57:        a += b",
143             "58:        return a"]
144         self.assertEqual(Foo.foo.watch.columns, expected)
145         self.assertEqual([len(r) for r in Foo.foo.watch.races], [6] * 10)
146
147 if __name__ == '__main__':
148     unittest.main()
149
Note: See TracBrowser for help on using the browser.