Merged revisions 1582-1665 via svnmerge from
[scons.git] / test / option / debug-time.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import TestSCons
28 import sys
29 import string
30 import re
31 import time
32
33 test = TestSCons.TestSCons()
34
35 test.write('SConstruct', """
36 env = Environment(OBJSUFFIX = '.ooo', PROGSUFFIX = '.xxx')
37 env.Program('foo', Split('foo.c bar.c'))
38 """)
39
40 test.write('foo.c', r"""
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include "foo.h"
44 int main(int argc, char *argv[])
45 {
46         argv[argc++] = "--";
47         printf("f1.c\n");
48         exit (0);
49 }
50 """)
51
52 test.write('bar.c', """
53 #include "bar.h"
54 """)
55
56 test.write('foo.h', """
57 #ifndef FOO_H
58 #define FOO_H
59 #include "bar.h"
60 #endif
61 """)
62
63 test.write('bar.h', """
64 #ifndef BAR_H
65 #define BAR_H
66 #include "foo.h"
67 #endif
68 """)
69
70 def num(match, line):
71     return float(re.search(match, line).group(1))
72
73 # Try to make things a little more equal by measuring Python overhead
74 # executing a minimal file, and reading the scons.py script itself from
75 # disk so that it's already been cached.
76 test.write('pass.py', "pass\n")
77 test.read(test.program)
78
79 start_time = time.time()
80 test.run(program=TestSCons.python, arguments=test.workpath('pass.py'))
81 overhead = time.time() - start_time 
82
83 start_time = time.time()
84 test.run(arguments = "--debug=time .")
85 complete_time = time.time() - start_time
86
87 expected_total_time = complete_time - overhead
88 lines = string.split(test.stdout(), '\n')
89
90 expected_command_time = 0.0
91 for cmdline in filter(lambda x: x[:23] == "Command execution time:", lines):
92     n = num(r'Command execution time: (\d+\.\d+) seconds', cmdline)
93     expected_command_time = expected_command_time + n
94
95 stdout = test.stdout()
96
97 total_time = num(r'Total build time: (\d+\.\d+) seconds', stdout)
98 sconscript_time = num(r'Total SConscript file execution time: (\d+\.\d+) seconds', stdout)
99 scons_time = num(r'Total SCons execution time: (\d+\.\d+) seconds', stdout)
100 command_time = num(r'Total command execution time: (\d+\.\d+) seconds', stdout)
101
102 def within_tolerance(expected, actual, tolerance):
103     return abs((expected-actual)/actual) <= tolerance
104
105 failures = []
106
107 if not within_tolerance(expected_command_time, command_time, 0.01):
108     failures.append("""\
109 SCons reported a total command execution time of %s,
110 but command execution times really totalled %s,
111 outside of the 1%% tolerance.
112 """ % (command_time, expected_command_time))
113
114 added_times = sconscript_time+scons_time+command_time
115 if not within_tolerance(total_time, added_times, 0.01):
116     failures.append("""\
117 SCons reported a total build time of %s,
118 but the various execution times actually totalled %s,
119 outside of the 1%% tolerance.
120 """ % (total_time, added_times))
121
122 if not within_tolerance(total_time, expected_total_time, 0.15):
123     failures.append("""\
124 SCons reported total build time of %s,
125 but the actual measured build time was %s
126 (end-to-end time of %s less Python overhead of %s),
127 outside of the 15%% tolerance.
128 """ % (total_time, expected_total_time, complete_time, overhead))
129
130 if failures:
131     print string.join([test.stdout()] + failures, '\n')
132     test.fail_test(1)
133
134 test.pass_test()