aboutsummaryrefslogtreecommitdiff
path: root/test/speedtest.tcl
blob: 7cd3b5fa192e9c793f5804964c9a58b633001584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" ${1+"$@"}
#
# This program runs performance testing on sqlite3.c.  Usage:
set usage {USAGE:

  speedtest.tcl  sqlite3.c  x1.txt  trunk.txt  -Os -DSQLITE_ENABLE_STAT4
                     |         |        |       `-----------------------'
    File to test ----'         |        |                |
                               |        |                `- options
       Output filename --------'        |
                                        `--- optional prior output to diff

Do a cache-grind performance analysis of the sqlite3.c file named and
write the results into the output file.  The ".txt" is appended to the
output file (and diff-file) name if it is not already present.  If the
diff-file is specified then show a diff from the diff-file to the new
output.

Other options include:
   CC=...                Specify an alternative C compiler.  Default is "gcc".
   -D...                 -D and -O options are passed through to the C compiler.
   --dryrun              Show what would happen but don't do anything.
   --help                Show this help screen.
   --lean                "Lean" mode.
   --lookaside N SZ      Lookahead uses N slots of SZ bytes each.
   --osmalloc            Use the OS native malloc() instead of MEMSYS5
   --pagesize N          Use N as the page size.
   --quiet | -q          "Quite".  Put results in file but don't pop up editor
   --size N              Change the test size.  100 means 100%.  Default: 5.
   --testset TEST        Specify the specific testset to use.  The default
                         is "mix1".  Other options include: "main", "json",
                         "cte", "orm", "fp", "rtree".
}
set srcfile {}
set outfile {}
set difffile {}
set cflags {-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_THREADSAFE=0}
set cc gcc
set testset mix1
set dryrun 0
set quiet 0
set osmalloc 0
set speedtestflags {--shrink-memory --reprepare --stats}
lappend speedtestflags --journal wal --size 5

for {set i 0} {$i<[llength $argv]} {incr i} {
  set arg [lindex $argv $i]
  if {[string index $arg 0]=="-"} {
    switch -- $arg {
      -pagesize -
      --pagesize {
        lappend speedtestflags --pagesize
        incr i
        lappend speedtestflags [lindex $argv $i]
      }
      -lookaside -
      --lookaside {
        lappend speedtestflags --lookaside
        incr i
        lappend speedtestflags [lindex $argv $i]
        incr i
        lappend speedtestflags [lindex $argv $i]
      }
      -lean -
      --lean {
        lappend cflags \
           -DSQLITE_DEFAULT_MEMSTATUS=0 \
           -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
           -DSQLITE_LIKE_DOESNT_MATCH_BLOBS=1 \
           -DSQLITE_MAX_EXPR_DEPTH=1 \
           -DSQLITE_OMIT_DECLTYPE \
           -DSQLITE_OMIT_DEPRECATED \
           -DSQLITE_OMIT_PROGRESS_CALLBACK \
           -DSQLITE_OMIT_SHARED_CACHE \
           -DSQLITE_USE_ALLOCA
      }
      -testset -
      --testset {
        incr i
        set testset [lindex $argv $i]
      }
      -size -
      --size {
        incr i
        set newsize [lindex $argv $i]
        if {$newsize<1} {set newsize 1}
        set speedtestflags \
          [regsub {.-size \d+} $speedtestflags "-size $newsize"]
      }
      -n -
      -dryrun -
      --dryrun {
        set dryrun 1
      }
      -osmalloc -
      --osmalloc {
        set osmalloc 1
      }
      -? -
      -help -
      --help {
        puts $usage
        exit 0
      }
      -q -
      -quiet -
      --quiet {
        set quiet 1
      }
      default {
        lappend cflags $arg
      }
    }
    continue
  }
  if {[string match CC=* $arg]} {
    set cc [lrange $arg 3 end]
    continue
  }
  if {[string match *.c $arg]} {
    if {$srcfile!=""} {
      puts stderr "multiple source files: $srcfile $arg"
      exit 1
    }
    set srcfile $arg
    continue
  }
  if {[lsearch {main cte rtree orm fp json parsenumber mix1} $arg]>=0} {
    set testset $arg
    continue
  }
  if {$outfile==""} {
    set outfile $arg
    continue
  }
  if {$difffile==""} {
    set difffile $arg
    continue
  }
  puts stderr "unknown option: \"$arg\".  Use --help for more info."
  exit 1
}
if {[lsearch -glob $cflags -O*]<0} {
  lappend cflags -Os
}
if {!$osmalloc} {
  append speedtestflags { --heap 40000000 64}
}
if {!$osmalloc && [lsearch -glob $cflags {-DSQLITE_ENABLE_MEMSYS*}]<0} {
  lappend cflags -DSQLITE_ENABLE_MEMSYS5
}
if {[lsearch -glob $cflags {-DSQLITE_ENABLE_RTREE*}]<0} {
  lappend cflags -DSQLITE_ENABLE_RTREE
}
if {$srcfile==""} {
  puts stderr "no sqlite3.c source file specified"
  exit 1
}
if {![file readable $srcfile]} {
  puts stderr "source file \"$srcfile\" does not exist"
  exit 1
}
if {$outfile==""} {
  puts stderr "no output file specified"
  exit 1
}
if {![string match *.* [file tail $outfile]]} {
  append outfile .txt
}
if {$difffile!=""} {
  if {![file exists $difffile]} {
    if {[file exists $difffile.txt]} {
      append difffile .txt
    } else {
      puts stderr "No such file: \"$difffile\""
      exit 1
    }
  }
}

set cccmd [list $cc -g]
lappend cccmd -I[file dir $srcfile]
lappend cccmd {*}[lsort $cflags]
lappend cccmd [file dir $argv0]/speedtest1.c
lappend cccmd $srcfile
lappend cccmd -o speedtest1
puts $cccmd
if {!$dryrun} {
  exec {*}$cccmd
}
lappend speedtestflags --testset $testset
set stcmd [list valgrind --tool=cachegrind ./speedtest1 {*}$speedtestflags]
lappend stcmd speedtest1.db
lappend stcmd >valgrind-out.txt 2>valgrind-err.txt
puts $stcmd
if {!$dryrun} {
   foreach file {speedtest1.db speedtest1.db-journal speedtest1.db-wal
                 speedtest1.db-shm} {
     if {[file exists $file]} {file delete $file}
   }
   exec {*}$stcmd
}

set maxmtime 0
set cgfile {}
foreach cgout [glob -nocomplain cachegrind.out.*] {
  if {[file mtime $cgout]>$maxmtime} {
    set cgfile $cgout
    set maxmtime [file mtime $cgfile]
  }
}
if {$cgfile==""} {
  puts "no cachegrind output"
  exit 1
}

############# Process the cachegrind.out.# file ##########################
set fd [open $outfile wb]
set in [open "|cg_annotate --show=Ir --auto=yes --context=40 $cgfile" r]
set dest !
set out(!) {}
set linenum 0
set cntlines 0      ;# true to remember cycle counts on each line
set seenSqlite3 0   ;# true if we have seen the sqlite3.c file
while {![eof $in]} {
  set line [string map {\t {        }} [gets $in]]
  if {[regexp {^-- Auto-annotated source: (.*)} $line all name]} {
    set dest $name
    if {[string match */sqlite3.c $dest]} {
      set cntlines 1
      set seenSqlite3 1
    } else {
      set cntlines 0
    }
  } elseif {[regexp {^-- line (\d+) ------} $line all ln]} {
    set line [lreplace $line 2 2 {#}]
    set linenum [expr {$ln-1}]
  } elseif {[regexp {^The following files chosen for } $line]} {
    set dest !
  }
  append out($dest) $line\n
  if {$cntlines} {
    incr linenum
    if {[regexp {^ *([0-9,]+) } $line all x]} {
      set x [string map {, {}} $x]
      set cycles($linenum) $x
    }
  }
}
foreach x [lsort [array names out]] {
  puts $fd $out($x)
}
# If the sqlite3.c file has been seen, then output a summary of the
# cycle counts for each file that went into making up sqlite3.c
#
if {$seenSqlite3} {
  close $in
  set in [open sqlite3.c]
  set linenum 0
  set fn sqlite3.c
  set pattern1 {^/\*+ Begin file ([^ ]+) \*}
  set pattern2 {^/\*+ Continuing where we left off in ([^ ]+) \*}
  while {![eof $in]} {
    set line [gets $in]
    incr linenum
    if {[regexp $pattern1 $line all newfn]} {
      set fn $newfn
    } elseif {[regexp $pattern2 $line all newfn]} {
      set fn $newfn
    } elseif {[info exists cycles($linenum)]} {
      incr fcycles($fn) $cycles($linenum)
    }
  }
  close $in
  puts $fd \
     {**********************************************************************}
  set lx {}
  set sum 0
  foreach {fn cnt} [array get fcycles] {
    lappend lx [list $cnt $fn]
    incr sum $cnt
  }
  puts $fd [format {%20s %14d  %8.3f%%} TOTAL $sum 100]
  foreach entry [lsort -index 0 -integer -decreasing $lx] {
    foreach {cnt fn} $entry break
    puts $fd [format {%20s %14d  %8.3f%%} $fn $cnt [expr {$cnt*100.0/$sum}]]
  }
}
puts $fd "Executable size:"
close $fd
exec size speedtest1 >>$outfile
#
#  Processed cachegrind output should now be in the $outfile
#############################################################################

if {$quiet} {
  # Skip this last part of popping up a GUI viewer
} elseif {$difffile!=""} {
  set fossilcmd {fossil xdiff --tk -c 20}
  lappend fossilcmd $difffile
  lappend fossilcmd $outfile
  lappend fossilcmd &
  puts $fossilcmd
  if {!$dryrun} {
    exec {*}$fossilcmd
  }
} else {
  if {!$dryrun} {
    exec open $outfile
  }
}