More unit tests
[codimension.git] / pythonparser-exp / speed_test.py
blob0c98054f00b9d42043422cedf6646d0ba1a4a5f7
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # codimension - graphics python two-way code editor and analyzer
5 # Copyright (C) 2010 Sergey Satskiy <sergey.satskiy@gmail.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # $Id$
23 " Speed test for the codimension brief parser "
25 import os, os.path, sys
26 import datetime
27 import pyclbr
28 import cdmbriefparser
29 import gc
30 import time
33 # Shows found errors in the parsed files
34 SHOW_ERRORS = False
37 def collectFiles( path, files ):
38 " Collects python files "
39 if not path.endswith( os.path.sep ):
40 path += os.path.sep
41 for item in os.listdir( path ):
42 if os.path.isdir( path + item ):
43 collectFiles( path + item, files )
44 if os.path.isfile( path + item ) and \
45 (item.endswith( ".py" ) or item.endswith( ".py3" )):
46 if item.startswith( "__" ):
47 continue
48 files.append( os.path.abspath( path + item ) )
49 continue
50 return
53 def pyclbrTest( files ):
54 " Loop for the library standard parser "
55 # time.sleep( 0.1 )
56 # return
57 count = 0
58 for item in files:
59 try:
60 tempObj = pyclbr.readmodule_ex(
61 os.path.basename( item ).replace( ".py", "" ),
62 [os.path.dirname( item )] )
63 except Exception, ex:
64 print "Error parsing " + item
65 print ex
66 count += 1
67 print "pyclbr: processed " + str(count) + " files"
68 return
70 def cdmpyparserTest( files ):
71 " Loop for the codimension parser "
72 errorCount = 0
73 count = 0
74 for item in files:
75 # print "Processing " + item + " ..."
76 tempObj = cdmbriefparser.getBriefModuleInfoFromFile( item )
77 if SHOW_ERRORS:
78 if not tempObj.isOK:
79 errorCount += 1
80 print "Failed to parse: " + item
81 for item in tempObj.errors:
82 print item
83 for item in tempObj.lexerErrors:
84 print " L: " + item
85 count += 1
86 print "cdmpyparser: processed " + str(count) + " file(s)"
87 print "cdmpyparser: number of errors: " + str(errorCount)
88 return
91 def deltaToFloat( delta ):
92 " Converts time delta to float "
93 return delta.seconds + delta.microseconds / 1E6 + delta.days * 86400
96 print "Speed test compares the time required for " \
97 "cdmpyparser and the standard pyclbr modules to collect module info."
98 print "cdmpyparser version: " + cdmbriefparser.getVersion()
99 print ""
102 pythonFiles = []
103 if len( sys.argv ) > 1:
104 # File names are expected
105 for fname in sys.argv[ 1: ]:
106 if not os.path.exists( fname ):
107 print "Cannot find file specified: '" + fname + "'"
108 sys.exit( 1 )
109 pythonFiles.append( os.path.abspath( fname ) )
110 print "Files to test: " + str(len(pythonFiles))
111 else:
112 print "Collecting a list of python files..."
113 paths = list( sys.path )
114 if '' in paths:
115 paths.remove( '' )
116 for path in paths:
117 if os.path.isdir( path ):
118 collectFiles( path, pythonFiles )
119 pythonFiles = set( pythonFiles )
120 print "Collected " + str(len(pythonFiles))
123 # timing for pyclbr
124 start = datetime.datetime.now()
125 pyclbrTest( pythonFiles )
126 end = datetime.datetime.now()
127 delta = end - start
128 count = gc.collect()
130 print "pyclbr timing:"
131 print "Start: " + str( start )
132 print "End: " + str( end )
133 print "Delta: " + str( delta ) + " as float: " + str( deltaToFloat( delta ) )
134 print "GC collected: " + str( count ) + " object(s)"
136 print ""
139 # timing for cdmpyparser
140 start = datetime.datetime.now()
141 cdmpyparserTest( pythonFiles )
142 end = datetime.datetime.now()
143 delta2 = end - start
144 count = gc.collect()
146 print "cdmpyparser timing:"
147 print "Start: " + str( start )
148 print "End: " + str( end )
149 print "Delta: " + str( delta2 ) + " as float: " + str( deltaToFloat( delta2 ) )
150 print "GC collected: " + str( count ) + " object(s)"
152 print "\nRatio: " + str( deltaToFloat( delta ) / deltaToFloat( delta2 ) )