summaryrefslogtreecommitdiff
path: root/tests/test9.cpp
blob: 07c1348ef4ea5579d39b9943864de9c4a0461008 (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
/*
 *  sqlite3xx - sqlite3 C++ layer, following the ideas of libpqxx
 *  Copyright (C) 2009  Andreas Baumann
 *
 *  This copyrighted material is made available to anyone wishing to use,
 *  modify, copy, or redistribute it subject to the terms and conditions of
 *  the GNU Lesser General Public License, as published by the Free Software
 *  Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY  or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this distribution; if not, write to:
 *  Free Software Foundation, Inc.
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02110-1301  USA
 *
 */

#include <iostream>
#include <cassert>
#include "sqlite3xx/sqlite3xx"
/* test needs 'unlink' from 'unistd.h' */
#if !defined _WIN32
#include <unistd.h>
#include <inttypes.h>
#endif /* !defined _WIN32 */
#include "threads.h"

using namespace sqlite3xx;
using namespace std;

const int NOF_PRODUCERS = 10;
const int NOF_CONSUMERS = 10;
const int NOF_PRODUCER_TRANSACTIONS = 100;
const int NOF_PRODUCER_OPS = 100;
const int NOF_CONSUMER_TRANSACTIONS = 100;
const int NOF_CONSUMER_OPS = 100;

#define UNUSED( x ) if( 0 && (x) ) { }

static MUTEX_TYPE cout_mutex;

static bool verbose = false;
static bool tracing = false;

static THREAD_FUNC_DECL produce( void *thread_data )
{
	uintptr_t no = (uintptr_t)thread_data;

	try {
		connection c( "test9.db" );
		if( tracing ) c.trace( true );
PRODUCER_PREPARE_AGAIN:
		try {
			c.prepare( "ins", "insert into x values( ? )" )( "integer", prepare::treat_direct );
		} catch( const database_locked& e ) {
			goto PRODUCER_PREPARE_AGAIN;
		}

		for( int i = 0; i < NOF_PRODUCER_TRANSACTIONS; i++ ) {
			work t( c, "ins" );
			if( verbose ) {
				MUTEX_LOCK( cout_mutex );
				cout << "producer " << no << " transaction " << i << " started" << endl;
				MUTEX_UNLOCK( cout_mutex );
			}
			for( int j = 0; j < NOF_PRODUCER_OPS; j++ ) {
PRODUCER_INS_AGAIN:
				try {
					result r = t.prepared( "ins" )( no * 100000 + i * 1000 + j ).exec( );
					assert( r.affected_rows( ) == 1 );
					t.commit( );
				} catch( const database_locked& e ) {
					goto PRODUCER_INS_AGAIN;
				}
			}
			if( verbose ) {
				MUTEX_LOCK( cout_mutex );
				cout << "producer " << no << " transaction " << i << " terminated" << endl;
				MUTEX_UNLOCK( cout_mutex );
			}
		}
	} catch( const sql_error& e ) {
		cerr << "producer error,  " << e.what( ) << ": " << e.query( ) << endl;
	}

	THREAD_FUNC_RETURN;
}

static THREAD_FUNC_DECL consume( void *thread_data )
{
	uintptr_t no = (uintptr_t)thread_data;

	try {
		connection c( "test9.db" );
CONSUMER_PREPARE_AGAIN:
		try {
			c.prepare( "sel", "select * from x" );
		} catch( const database_locked &e ) {
			goto CONSUMER_PREPARE_AGAIN;
		}
		if( tracing ) c.trace( true );

		for( int i = 0; i < NOF_CONSUMER_TRANSACTIONS; i++ ) {
			work t( c, "sel" );
			if( verbose ) {
				MUTEX_LOCK( cout_mutex );
				cout << "consumer " << no << " transaction " << i << " started" << endl;
				MUTEX_UNLOCK( cout_mutex );
			}
			for( int j = 0; j < NOF_CONSUMER_OPS; j++ ) {
CONSUMER_SELECT_AGAIN:
				try {
					result r = t.prepared( "sel" ).exec( );
					int nof_rows = 1;
					for( result::const_iterator it = r.begin( ); it < r.end( ); it++, nof_rows++ ) {
						int x;
						it["x"].to( x );
					}
					if( verbose ) {
						MUTEX_LOCK( cout_mutex );
						cout << "consumer " << no << " transaction " << i << " got " << nof_rows << " rows." << endl;
						MUTEX_UNLOCK( cout_mutex );
					}
					t.commit( );
				} catch( const database_locked& e ) {
					goto CONSUMER_SELECT_AGAIN;
				}
			}
			if( verbose ) {
				MUTEX_LOCK( cout_mutex );
				cout << "consumer " << no << " transaction " << i << " terminated" << endl;
				MUTEX_UNLOCK( cout_mutex );
			}
		}
	} catch( const sql_error& e ) {
		cerr << "consumer error,  " << e.what( ) << ": " << e.query( ) << endl;
	}

	THREAD_FUNC_RETURN;
}

int main( ) {
	(void)unlink( "test9.db" );

	try {
		cout << "creating DB.." << endl;
		connection c( "test9.db" );
		if( tracing ) c.trace( true );
                cout << "connection object is " << c << endl;

		cout << "create table.." << endl;
		c.exec( "create table x( x integer )" );
		
		// now have some threads creating data and some others
		// consuming it. stupid, but should nicely stress the thing
		THREAD_TYPE prod[NOF_PRODUCERS];
		THREAD_TYPE cons[NOF_CONSUMERS];

		MUTEX_SETUP( cout_mutex );

		for( int i = 0; i < NOF_PRODUCERS ; i++ ) {
			THREAD_CREATE( &prod[i], produce, (void *)i );
		}

		for( int i = 0; i < NOF_CONSUMERS; i++ ) {
			THREAD_CREATE( &cons[i], consume, (void *)i );
		}

		for( int i = 0; i < NOF_PRODUCERS; i++ ) {
			THREAD_JOIN( prod[i] );
		}

		for( int i = 0; i < NOF_CONSUMERS; i++ ) {
			THREAD_JOIN( cons[i] );
		}

		MUTEX_CLEANUP( cout_mutex );

	} catch( const sql_error& e ) {
		cerr << e.what( ) << ": " << e.query( ) << endl;
	}
}