• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 2008 March 21
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#*************************************************************************
11# This file implements regression tests for SQLite library.  The
12# focus of this script is measuring executing speed.
13#
14# This is a copy of speed1.test modified to user prepared statements.
15#
16# $Id: speed1p.test,v 1.7 2009/04/09 01:23:49 drh Exp $
17#
18
19sqlite3_shutdown
20#sqlite3_config_scratch 29000 1
21set old_lookaside [sqlite3_config_lookaside 2048 300]
22#sqlite3_config_pagecache 1024 11000
23set testdir [file dirname $argv0]
24source $testdir/tester.tcl
25speed_trial_init speed1
26
27# Set a uniform random seed
28expr srand(0)
29
30# The number_name procedure below converts its argment (an integer)
31# into a string which is the English-language name for that number.
32#
33# Example:
34#
35#     puts [number_name 123]   ->  "one hundred twenty three"
36#
37set ones {zero one two three four five six seven eight nine
38          ten eleven twelve thirteen fourteen fifteen sixteen seventeen
39          eighteen nineteen}
40set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
41proc number_name {n} {
42  if {$n>=1000} {
43    set txt "[number_name [expr {$n/1000}]] thousand"
44    set n [expr {$n%1000}]
45  } else {
46    set txt {}
47  }
48  if {$n>=100} {
49    append txt " [lindex $::ones [expr {$n/100}]] hundred"
50    set n [expr {$n%100}]
51  }
52  if {$n>=20} {
53    append txt " [lindex $::tens [expr {$n/10}]]"
54    set n [expr {$n%10}]
55  }
56  if {$n>0} {
57    append txt " [lindex $::ones $n]"
58  }
59  set txt [string trim $txt]
60  if {$txt==""} {set txt zero}
61  return $txt
62}
63
64# Create a database schema.
65#
66do_test speed1p-1.0 {
67  execsql {
68    PRAGMA page_size=1024;
69    PRAGMA cache_size=500;
70    PRAGMA locking_mode=EXCLUSIVE;
71    CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
72    CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
73    CREATE INDEX i2a ON t2(a);
74    CREATE INDEX i2b ON t2(b);
75  }
76  execsql {
77    SELECT name FROM sqlite_master ORDER BY 1;
78  }
79} {i2a i2b t1 t2}
80
81
82# 50000 INSERTs on an unindexed table
83#
84set list {}
85for {set i 1} {$i<=50000} {incr i} {
86  set r [expr {int(rand()*500000)}]
87  set x [number_name $r]
88  lappend list $i $r $x
89}
90set script {
91  foreach {i r x} $::list {
92    db eval {INSERT INTO t1 VALUES($i,$r,$x)}
93  }
94}
95db eval BEGIN
96speed_trial_tcl speed1p-insert1 50000 row $script
97db eval COMMIT
98
99# 50000 INSERTs on an indexed table
100#
101set list {}
102for {set i 1} {$i<=50000} {incr i} {
103  set r [expr {int(rand()*500000)}]
104  set x [number_name $r]
105  lappend list $i $r $x
106}
107set script {
108  foreach {i r x} $::list {
109    db eval {INSERT INTO t2 VALUES($i,$r,$x)}
110  }
111}
112db eval BEGIN
113speed_trial_tcl speed1p-insert2 50000 row $script
114db eval COMMIT
115
116
117
118# 50 SELECTs on an integer comparison.  There is no index so
119# a full table scan is required.
120#
121set list {}
122for {set i 0} {$i<50} {incr i} {
123  set lwr [expr {$i*100}]
124  set upr [expr {($i+10)*100}]
125  lappend list $lwr $upr
126}
127set script {
128  foreach {lwr upr} $::list {
129    db eval  {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
130  }
131}
132db eval BEGIN
133speed_trial_tcl speed1p-select1 [expr {50*50000}] row $script
134db eval COMMIT
135
136# 50 SELECTs on an LIKE comparison.  There is no index so a full
137# table scan is required.
138#
139set list {}
140for {set i 0} {$i<50} {incr i} {
141  lappend list "%[number_name $i]%"
142}
143set script {
144  foreach pattern $::list {
145    db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
146  }
147}
148db eval BEGIN
149speed_trial_tcl speed1p-select2 [expr {50*50000}] row $script
150db eval COMMIT
151
152# Create indices
153#
154db eval BEGIN
155speed_trial speed1p-createidx 150000 row {
156  CREATE INDEX i1a ON t1(a);
157  CREATE INDEX i1b ON t1(b);
158  CREATE INDEX i1c ON t1(c);
159}
160db eval COMMIT
161
162# 5000 SELECTs on an integer comparison where the integer is
163# indexed.
164#
165set list {}
166for {set i 0} {$i<5000} {incr i} {
167  set lwr [expr {$i*100}]
168  set upr [expr {($i+10)*100}]
169  lappend list $lwr $upr
170}
171set script {
172  foreach {lwr upr} $::list {
173    db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
174  }
175}
176db eval BEGIN
177speed_trial_tcl speed1p-select3 5000 stmt $script
178db eval COMMIT
179
180# 100000 random SELECTs against rowid.
181#
182set list {}
183for {set i 1} {$i<=100000} {incr i} {
184  set id [expr {int(rand()*50000)+1}]
185  lappend list $id
186}
187set script {
188  foreach id $::list {
189    db eval {SELECT c FROM t1 WHERE rowid=$id}
190  }
191}
192db eval BEGIN
193speed_trial_tcl speed1p-select4 100000 row $script
194db eval COMMIT
195
196# 100000 random SELECTs against a unique indexed column.
197#
198set list {}
199for {set i 1} {$i<=100000} {incr i} {
200  set id [expr {int(rand()*50000)+1}]
201  lappend list $id
202}
203set script {
204  foreach id $::list {
205    db eval {SELECT c FROM t1 WHERE a=$id}
206  }
207}
208db eval BEGIN
209speed_trial_tcl speed1p-select5 100000 row $script
210db eval COMMIT
211
212# 50000 random SELECTs against an indexed column text column
213#
214set list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}]
215set script {
216  foreach c $::list {
217    db eval {SELECT c FROM t1 WHERE c=$c}
218  }
219}
220db eval BEGIN
221speed_trial_tcl speed1p-select6 50000 row $script
222db eval COMMIT
223
224
225# Vacuum
226speed_trial speed1p-vacuum 100000 row VACUUM
227
228# 5000 updates of ranges where the field being compared is indexed.
229#
230set list {}
231for {set i 0} {$i<5000} {incr i} {
232  set lwr [expr {$i*2}]
233  set upr [expr {($i+1)*2}]
234  lappend list $lwr $upr
235}
236set script {
237  foreach {lwr upr} $::list {
238    db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
239  }
240}
241db eval BEGIN
242speed_trial_tcl speed1p-update1 5000 stmt $script
243db eval COMMIT
244
245# 50000 single-row updates.  An index is used to find the row quickly.
246#
247set list {}
248for {set i 0} {$i<50000} {incr i} {
249  set r [expr {int(rand()*500000)}]
250  lappend list $i $r
251}
252set script {
253  foreach {i r} $::list {
254    db eval {UPDATE t1 SET b=$r WHERE a=$i}
255  }
256}
257db eval BEGIN
258speed_trial_tcl speed1p-update2 50000 row $script
259db eval COMMIT
260
261# 1 big text update that touches every row in the table.
262#
263speed_trial speed1p-update3 50000 row {
264  UPDATE t1 SET c=a;
265}
266
267# Many individual text updates.  Each row in the table is
268# touched through an index.
269#
270set list {}
271for {set i 1} {$i<=50000} {incr i} {
272  set r [expr {int(rand()*500000)}]
273  lappend list $i [number_name $r]
274}
275set script {
276  foreach {i x} $::list {
277    db eval {UPDATE t1 SET c=$x WHERE a=$i}
278  }
279}
280db eval BEGIN
281speed_trial_tcl speed1p-update4 50000 row $script
282db eval COMMIT
283
284# Delete all content in a table.
285#
286speed_trial speed1p-delete1 50000 row {DELETE FROM t1}
287
288# Copy one table into another
289#
290speed_trial speed1p-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
291
292# Delete all content in a table, one row at a time.
293#
294speed_trial speed1p-delete2 50000 row {DELETE FROM t1 WHERE 1}
295
296# Refill the table yet again
297#
298speed_trial speed1p-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
299
300# Drop the table and recreate it without its indices.
301#
302db eval BEGIN
303speed_trial speed1p-drop1 50000 row {
304   DROP TABLE t1;
305   CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
306}
307db eval COMMIT
308
309# Refill the table yet again.  This copy should be faster because
310# there are no indices to deal with.
311#
312speed_trial speed1p-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
313
314# Select 20000 rows from the table at random.
315#
316speed_trial speed1p-random1 50000 row {
317  SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
318}
319
320# Delete 20000 random rows from the table.
321#
322speed_trial speed1p-random-del1 20000 row {
323  DELETE FROM t1 WHERE rowid IN
324    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
325}
326do_test speed1p-1.1 {
327  db one {SELECT count(*) FROM t1}
328} 30000
329
330# Delete 20000 more rows at random from the table.
331#
332speed_trial speed1p-random-del2 20000 row {
333  DELETE FROM t1 WHERE rowid IN
334    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
335}
336do_test speed1p-1.2 {
337  db one {SELECT count(*) FROM t1}
338} 10000
339speed_trial_summary speed1
340
341db close
342sqlite3_shutdown
343eval sqlite3_config_lookaside $old_lookaside
344sqlite3_initialize
345autoinstall_test_functions
346finish_test
347