Debian Codegraph @

test/speedtest.tcl

raw
1 #!/bin/sh
2 # the next line restarts using tclsh \
3 exec tclsh "$0" ${1+"$@"}
4 #
5 # This program runs performance testing on sqlite3.c. Usage:
6 set usage {USAGE:
7
8 speedtest.tcl sqlite3.c x1.txt trunk.txt -Os -DSQLITE_ENABLE_STAT4
9 | | | `-----------------------'
10 File to test ----' | | |
11 | | `- options
12 Output filename --------' |
13 `--- optional prior output to diff
14
15 Do a cache-grind performance analysis of the sqlite3.c file named and
16 write the results into the output file. The ".txt" is appended to the
17 output file (and diff-file) name if it is not already present. If the
18 diff-file is specified then show a diff from the diff-file to the new
19 output.
20
21 Other options include:
22 CC=... Specify an alternative C compiler. Default is "gcc".
23 -D... -D and -O options are passed through to the C compiler.
24 --dryrun Show what would happen but don't do anything.
25 --help Show this help screen.
26 --lean "Lean" mode.
27 --lookaside N SZ Lookahead uses N slots of SZ bytes each.
28 --osmalloc Use the OS native malloc() instead of MEMSYS5
29 --pagesize N Use N as the page size.
30 --quiet | -q "Quiet". Put results in file but don't pop up editor
31 --size N Change the test size. 100 means 100%. Default: 5.
32 --testset TEST Specify the specific testset to use. The default
33 is "mix1". Other options include: "main", "json",
34 "cte", "orm", "fp", "rtree".
35 }
36 set srcfile {}
37 set outfile {}
38 set difffile {}
39 set cflags {-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_THREADSAFE=0}
40 set cc gcc
41 set testset mix1
42 set dryrun 0
43 set quiet 0
44 set osmalloc 0
45 set speedtestflags {--shrink-memory --reprepare --stats}
46 lappend speedtestflags --journal wal --size 5
47
48 for {set i 0} {$i<[llength $argv]} {incr i} {
49 set arg [lindex $argv $i]
50 if {[string index $arg 0]=="-"} {
51 switch -- $arg {
52 -pagesize -
53 --pagesize {
54 lappend speedtestflags --pagesize
55 incr i
56 lappend speedtestflags [lindex $argv $i]
57 }
58 -lookaside -
59 --lookaside {
60 lappend speedtestflags --lookaside
61 incr i
62 lappend speedtestflags [lindex $argv $i]
63 incr i
64 lappend speedtestflags [lindex $argv $i]
65 }
66 -lean -
67 --lean {
68 lappend cflags \
69 -DSQLITE_DEFAULT_MEMSTATUS=0 \
70 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
71 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS=1 \
72 -DSQLITE_MAX_EXPR_DEPTH=1 \
73 -DSQLITE_OMIT_DECLTYPE \
74 -DSQLITE_OMIT_DEPRECATED \
75 -DSQLITE_OMIT_PROGRESS_CALLBACK \
76 -DSQLITE_OMIT_SHARED_CACHE \
77 -DSQLITE_USE_ALLOCA
78 }
79 -testset -
80 --testset {
81 incr i
82 set testset [lindex $argv $i]
83 }
84 -size -
85 --size {
86 incr i
87 set newsize [lindex $argv $i]
88 if {$newsize<1} {set newsize 1}
89 set speedtestflags \
90 [regsub {.-size \d+} $speedtestflags "-size $newsize"]
91 }
92 -n -
93 -dryrun -
94 --dryrun {
95 set dryrun 1
96 }
97 -osmalloc -
98 --osmalloc {
99 set osmalloc 1
100 }
101 -? -
102 -help -
103 --help {
104 puts $usage
105 exit 0
106 }
107 -q -
108 -quiet -
109 --quiet {
110 set quiet 1
111 }
112 default {
113 lappend cflags $arg
114 }
115 }
116 continue
117 }
118 if {[string match CC=* $arg]} {
119 set cc [string range $arg 3 end]
120 continue
121 }
122 if {[string match *.c $arg]} {
123 if {$srcfile!=""} {
124 puts stderr "multiple source files: $srcfile $arg"
125 exit 1
126 }
127 set srcfile $arg
128 continue
129 }
130 if {[lsearch {main cte rtree orm fp json parsenumber mix1} $arg]>=0} {
131 set testset $arg
132 continue
133 }
134 if {$outfile==""} {
135 set outfile $arg
136 continue
137 }
138 if {$difffile==""} {
139 set difffile $arg
140 continue
141 }
142 puts stderr "unknown option: \"$arg\". Use --help for more info."
143 exit 1
144 }
145 if {[lsearch -glob $cflags -O*]<0} {
146 lappend cflags -Os
147 }
148 if {!$osmalloc} {
149 append speedtestflags { --heap 40000000 64}
150 }
151 if {!$osmalloc && [lsearch -glob $cflags {-DSQLITE_ENABLE_MEMSYS*}]<0} {
152 lappend cflags -DSQLITE_ENABLE_MEMSYS5
153 }
154 if {[lsearch -glob $cflags {-DSQLITE_ENABLE_RTREE*}]<0} {
155 lappend cflags -DSQLITE_ENABLE_RTREE
156 }
157 if {$srcfile==""} {
158 puts stderr "no sqlite3.c source file specified"
159 exit 1
160 }
161 if {![file readable $srcfile]} {
162 puts stderr "source file \"$srcfile\" does not exist"
163 exit 1
164 }
165 if {$outfile==""} {
166 puts stderr "no output file specified"
167 exit 1
168 }
169 if {![string match *.* [file tail $outfile]]} {
170 append outfile .txt
171 }
172 if {$difffile!=""} {
173 if {![file exists $difffile]} {
174 if {[file exists $difffile.txt]} {
175 append difffile .txt
176 } else {
177 puts stderr "No such file: \"$difffile\""
178 exit 1
179 }
180 }
181 }
182
183 set cccmd [list $cc -g]
184 lappend cccmd -I[file dir $srcfile]
185 lappend cccmd {*}[lsort $cflags]
186 lappend cccmd [file dir $argv0]/speedtest1.c
187 lappend cccmd $srcfile
188 lappend cccmd -o speedtest1
189 puts $cccmd
190 if {!$dryrun} {
191 exec {*}$cccmd
192 }
193 lappend speedtestflags --testset $testset
194 set stcmd [list valgrind --tool=cachegrind ./speedtest1 {*}$speedtestflags]
195 lappend stcmd speedtest1.db
196 lappend stcmd >valgrind-out.txt 2>valgrind-err.txt
197 puts $stcmd
198 if {!$dryrun} {
199 foreach file {speedtest1.db speedtest1.db-journal speedtest1.db-wal
200 speedtest1.db-shm} {
201 if {[file exists $file]} {file delete $file}
202 }
203 exec {*}$stcmd
204 }
205
206 set maxmtime 0
207 set cgfile {}
208 foreach cgout [glob -nocomplain cachegrind.out.*] {
209 if {[file mtime $cgout]>$maxmtime} {
210 set cgfile $cgout
211 set maxmtime [file mtime $cgfile]
212 }
213 }
214 if {$cgfile==""} {
215 puts "no cachegrind output"
216 exit 1
217 }
218
219 ############# Process the cachegrind.out.# file ##########################
220 set fd [open $outfile wb]
221 set in [open "|cg_annotate --show=Ir --auto=yes --context=40 $cgfile" r]
222 set dest !
223 set out(!) {}
224 set linenum 0
225 set cntlines 0 ;# true to remember cycle counts on each line
226 set seenSqlite3 0 ;# true if we have seen the sqlite3.c file
227 while {![eof $in]} {
228 set line [string map {\t { }} [gets $in]]
229 if {[regexp {^-- Auto-annotated source: (.*)} $line all name]} {
230 set dest $name
231 if {[string match */sqlite3.c $dest]} {
232 set cntlines 1
233 set seenSqlite3 1
234 } else {
235 set cntlines 0
236 }
237 } elseif {[regexp {^-- line (\d+) ------} $line all ln]} {
238 set line [lreplace $line 2 2 {#}]
239 set linenum [expr {$ln-1}]
240 } elseif {[regexp {^The following files chosen for } $line]} {
241 set dest !
242 }
243 append out($dest) $line\n
244 if {$cntlines} {
245 incr linenum
246 if {[regexp {^ *([0-9,]+) } $line all x]} {
247 set x [string map {, {}} $x]
248 set cycles($linenum) $x
249 }
250 }
251 }
252 foreach x [lsort [array names out]] {
253 puts $fd $out($x)
254 }
255 # If the sqlite3.c file has been seen, then output a summary of the
256 # cycle counts for each file that went into making up sqlite3.c
257 #
258 if {$seenSqlite3} {
259 close $in
260 set in [open sqlite3.c]
261 set linenum 0
262 set fn sqlite3.c
263 set pattern1 {^/\*+ Begin file ([^ ]+) \*}
264 set pattern2 {^/\*+ Continuing where we left off in ([^ ]+) \*}
265 while {![eof $in]} {
266 set line [gets $in]
267 incr linenum
268 if {[regexp $pattern1 $line all newfn]} {
269 set fn $newfn
270 } elseif {[regexp $pattern2 $line all newfn]} {
271 set fn $newfn
272 } elseif {[info exists cycles($linenum)]} {
273 incr fcycles($fn) $cycles($linenum)
274 }
275 }
276 close $in
277 puts $fd \
278 {**********************************************************************}
279 set lx {}
280 set sum 0
281 foreach {fn cnt} [array get fcycles] {
282 lappend lx [list $cnt $fn]
283 incr sum $cnt
284 }
285 puts $fd [format {%20s %14d %8.3f%%} TOTAL $sum 100]
286 foreach entry [lsort -index 0 -integer -decreasing $lx] {
287 foreach {cnt fn} $entry break
288 puts $fd [format {%20s %14d %8.3f%%} $fn $cnt [expr {$cnt*100.0/$sum}]]
289 }
290 }
291 puts $fd "Executable size:"
292 close $fd
293 exec size speedtest1 >>$outfile
294 #
295 # Processed cachegrind output should now be in the $outfile
296 #############################################################################
297
298 if {$quiet} {
299 # Skip this last part of popping up a GUI viewer
300 } elseif {$difffile!=""} {
301 set fossilcmd {fossil xdiff --tk -c 20}
302 lappend fossilcmd $difffile
303 lappend fossilcmd $outfile
304 lappend fossilcmd &
305 puts $fossilcmd
306 if {!$dryrun} {
307 exec {*}$fossilcmd
308 }
309 } else {
310 if {!$dryrun} {
311 exec open $outfile
312 }
313 }