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
|
/*
** 2014 December 9
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
**
*/
/*
** Thread 1. CREATE and DROP a table.
*/
static char *stress_thread_1(int iTid, int iArg){
Error err = {0}; /* Error code and message */
Sqlite db = {0}; /* SQLite database connection */
opendb(&err, &db, "test.db", 0);
while( !timetostop(&err) ){
sql_script(&err, &db, "CREATE TABLE IF NOT EXISTS t1(a PRIMARY KEY, b)");
clear_error(&err, SQLITE_LOCKED);
sql_script(&err, &db, "DROP TABLE IF EXISTS t1");
clear_error(&err, SQLITE_LOCKED);
}
closedb(&err, &db);
print_and_free_err(&err);
return sqlite3_mprintf("ok");
}
/*
** Thread 2. Open and close database connections.
*/
static char *stress_thread_2(int iTid, int iArg){
Error err = {0}; /* Error code and message */
Sqlite db = {0}; /* SQLite database connection */
while( !timetostop(&err) ){
opendb(&err, &db, "test.db", 0);
sql_script(&err, &db, "SELECT * FROM sqlite_master;");
clear_error(&err, SQLITE_LOCKED);
closedb(&err, &db);
}
print_and_free_err(&err);
return sqlite3_mprintf("ok");
}
/*
** Thread 3. Attempt many small SELECT statements.
*/
static char *stress_thread_3(int iTid, int iArg){
Error err = {0}; /* Error code and message */
Sqlite db = {0}; /* SQLite database connection */
int i1 = 0;
int i2 = 0;
opendb(&err, &db, "test.db", 0);
while( !timetostop(&err) ){
sql_script(&err, &db, "SELECT * FROM t1 ORDER BY a;");
i1++;
if( err.rc ) i2++;
clear_error(&err, SQLITE_LOCKED);
clear_error(&err, SQLITE_ERROR);
}
closedb(&err, &db);
print_and_free_err(&err);
return sqlite3_mprintf("read t1 %d/%d attempts", i2, i1);
}
/*
** Thread 5. Attempt INSERT statements.
*/
static char *stress_thread_4(int iTid, int iArg){
Error err = {0}; /* Error code and message */
Sqlite db = {0}; /* SQLite database connection */
int i1 = 0;
int i2 = 0;
opendb(&err, &db, "test.db", 0);
while( !timetostop(&err) ){
if( iArg ){
closedb(&err, &db);
opendb(&err, &db, "test.db", 0);
}
sql_script(&err, &db,
"WITH loop(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM loop LIMIT 200) "
"INSERT INTO t1 VALUES(randomblob(60), randomblob(60));"
);
i1++;
if( err.rc ) i2++;
clear_error(&err, SQLITE_LOCKED);
clear_error(&err, SQLITE_ERROR);
}
closedb(&err, &db);
print_and_free_err(&err);
return sqlite3_mprintf("wrote t1 %d/%d attempts", i2, i1);
}
/*
** Thread 6. Attempt DELETE operations.
*/
static char *stress_thread_5(int iTid, int iArg){
Error err = {0}; /* Error code and message */
Sqlite db = {0}; /* SQLite database connection */
int i1 = 0;
int i2 = 0;
opendb(&err, &db, "test.db", 0);
while( !timetostop(&err) ){
i64 i = (i1 % 4);
if( iArg ){
closedb(&err, &db);
opendb(&err, &db, "test.db", 0);
}
execsql(&err, &db, "DELETE FROM t1 WHERE (rowid % 4)==:i", &i);
i1++;
if( err.rc ) i2++;
clear_error(&err, SQLITE_LOCKED);
clear_error(&err, SQLITE_ERROR);
}
closedb(&err, &db);
print_and_free_err(&err);
return sqlite3_mprintf("deleted from t1 %d/%d attempts", i2, i1);
}
static void stress1(int nMs){
Error err = {0};
Sqlite db = {0};
Threadset threads = {0};
setstoptime(&err, nMs);
sqlite3_enable_shared_cache(1);
launch_thread(&err, &threads, stress_thread_1, 0);
launch_thread(&err, &threads, stress_thread_1, 0);
launch_thread(&err, &threads, stress_thread_2, 0);
launch_thread(&err, &threads, stress_thread_2, 0);
launch_thread(&err, &threads, stress_thread_3, 0);
launch_thread(&err, &threads, stress_thread_3, 0);
launch_thread(&err, &threads, stress_thread_4, 0);
launch_thread(&err, &threads, stress_thread_4, 0);
launch_thread(&err, &threads, stress_thread_5, 0);
launch_thread(&err, &threads, stress_thread_5, 1);
join_all_threads(&err, &threads);
sqlite3_enable_shared_cache(0);
print_and_free_err(&err);
}
|