1# Copyright 2014-2017 Insight Software Consortium.
2# Copyright 2004-2009 Roman Yakovenko.
3# Distributed under the Boost Software License, Version 1.0.
4# See http://www.boost.org/LICENSE_1_0.txt
5
6import os
7import sys
8import timeit
9import hotshot
10import hotshot.stats
11
12this_module_dir_path = os.path.abspath(
13    os.path.dirname(sys.modules[__name__].__file__))
14
15sys.path.insert(1, os.path.join(this_module_dir_path, '../../'))
16sys.path.insert(2, os.path.join(this_module_dir_path, '../'))
17
18import autoconfig  # nopep8
19from pygccxml import parser  # nopep8
20from pygccxml import declarations  # nopep8
21
22dcache_file_name = os.path.join(autoconfig.data_directory, 'pygccxml.cache')
23if os.path.exists(dcache_file_name):
24    os.remove(dcache_file_name)
25
26
27def test_on_windows_dot_h():
28    he = r"2003\Vc7\PlatformSDK\Include\windows.h"
29    windows_header = r"D:\Program Files\Microsoft Visual Studio .NET " + he
30    clock_prev = timeit.default_timer()
31    dcache = parser.file_cache_t(dcache_file_name)
32    reader = parser.source_reader_t(
33        parser.xml_generator_configuration_t(
34            xml_generator_path=autoconfig.generator_path),
35        dcache)
36    reader.read_file(windows_header)
37    dcache.flush()
38    clock_now = timeit.default_timer()
39    print('without cache: %f seconds' % (clock_now - clock_prev))
40
41    clock_prev = timeit.default_timer()
42    dcache = parser.file_cache_t(dcache_file_name)
43    reader = parser.source_reader_t(
44        parser.xml_generator_configuration_t(
45            xml_generator_path=autoconfig.generator_path),
46        dcache)
47    reader.read_file(windows_header)
48    clock_now = timeit.default_timer()
49    print('with cache   : %f seconds' % (clock_now - clock_prev))
50
51#########################################################################
52# testing include_std.hpp
53
54
55def test_source_on_include_std_dot_hpp():
56    include_std_header = os.path.join(
57        autoconfig.data_directory,
58        'include_std.hpp')
59    clock_prev = timeit.default_timer()
60    dcache = parser.file_cache_t(dcache_file_name)
61    reader = parser.source_reader_t(
62        parser.xml_generator_configuration_t(
63            xml_generator_path=autoconfig.generator_path),
64        dcache)
65    reader.read_file(include_std_header)
66    dcache.flush()
67    clock_now = timeit.default_timer()
68    print('without cache: %f seconds' % (clock_now - clock_prev))
69
70    clock_prev = timeit.default_timer()
71    dcache = parser.file_cache_t(dcache_file_name)
72    reader = parser.source_reader_t(
73        parser.xml_generator_configuration_t(
74            xml_generator_path=autoconfig.generator_path),
75        dcache)
76    reader.read_file(include_std_header)
77    clock_now = timeit.default_timer()
78    print('with cache   : %f seconds' % (clock_now - clock_prev))
79
80
81#########################################################################
82# testing include_std.hpp
83def test_project_on_include_std_dot_hpp():
84    include_std_header = os.path.join(
85        autoconfig.data_directory,
86        'include_std.hpp')
87    clock_prev = timeit.default_timer()
88    dcache = parser.file_cache_t(dcache_file_name)
89    reader = parser.project_reader_t(
90        parser.xml_generator_configuration_t(
91            xml_generator_path=autoconfig.generator_path),
92        dcache)
93    reader.read_files([include_std_header])
94    dcache.flush()
95    clock_now = timeit.default_timer()
96    print('without cache: %f seconds' % (clock_now - clock_prev))
97
98    clock_prev = timeit.default_timer()
99    dcache = parser.file_cache_t(dcache_file_name)
100    reader = parser.project_reader_t(
101        parser.xml_generator_configuration_t(
102            xml_generator_path=autoconfig.generator_path),
103        dcache)
104    reader.read_files([include_std_header])
105    clock_now = timeit.default_timer()
106    print('with cache   : %f seconds' % (clock_now - clock_prev))
107
108
109def profile_project():
110    include_std_header = os.path.join(
111        autoconfig.data_directory,
112        'include_std.hpp')
113    reader = parser.project_reader_t(
114        parser.xml_generator_configuration_t(
115            xml_generator_path=autoconfig.generator_path))
116    reader.read_files([include_std_header])
117
118
119def profile_project2():
120    he = r"2003\Vc7\PlatformSDK\Include\windows.h"
121    include_std_header = r"D:\Program Files\Microsoft Visual Studio .NET " + he
122    reader = parser.project_reader_t(
123        parser.xml_generator_configuration_t(
124            xml_generator_path=autoconfig.generator_path))
125    reader.read_files([include_std_header])
126
127
128def test_on_big_file(file_name, count):
129    file_name = os.path.join(autoconfig.data_directory, file_name)
130    for i in range(count):
131        reader = parser.project_reader_t(
132            parser.xml_generator_configuration_t(
133                xml_generator_path=autoconfig.generator_path))
134        decls = reader.read_files([parser.create_gccxml_fc(file_name)])
135        global_ns = declarations.get_global_namespace(decls)
136        global_ns.init_optimizer()
137
138
139if __name__ == "__main__":
140
141    # test_on_windows_dot_h()
142    # test_source_on_include_std_dot_hpp()
143    # test_project_on_include_std_dot_hpp()
144    print('running')
145    prof = hotshot.Profile('parser.prof')
146    prof.runcall(lambda: test_on_big_file('itkImage.xml', 1))
147    stats = hotshot.stats.load("parser.prof")
148    stats.sort_stats('time', 'calls')
149    stats.print_stats(30)
150    print('running - done')
151    # print 'loading file'
152    # pdata = pstats.Stats('pygccxml.profile')
153    # print 'loading file - done'
154    # print 'striping dirs'
155    # pdata.strip_dirs()
156    # print 'striping dirs - done'
157    # print 'sorting stats'
158    # pdata.sort_stats('time').print_stats(476)
159    # print 'sorting stats - done'
160    # pdata.print_callers('find_all_declarations')
161