summaryrefslogtreecommitdiff
path: root/tests/test9.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test9.cpp')
-rw-r--r--tests/test9.cpp64
1 files changed, 42 insertions, 22 deletions
diff --git a/tests/test9.cpp b/tests/test9.cpp
index 5b57a87..62e17dc 100644
--- a/tests/test9.cpp
+++ b/tests/test9.cpp
@@ -26,26 +26,29 @@
/* 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 = 2;
-const int NOF_CONSUMERS = 2;
-const int NOF_PRODUCER_TRANSACTIONS = 10;
-const int NOF_PRODUCER_OPS = 10;
-const int NOF_CONSUMER_TRANSACTIONS = 10;
-const int NOF_CONSUMER_OPS = 10;
+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) ) { }
-MUTEX_TYPE cout_mutex;
+static MUTEX_TYPE cout_mutex;
+
+static bool verbose = false;
static THREAD_FUNC_DECL produce( void *thread_data )
{
- UNUSED( thread_data );
+ uintptr_t no = (uintptr_t)thread_data;
try {
connection c( "test9.db" );
@@ -53,14 +56,21 @@ static THREAD_FUNC_DECL produce( void *thread_data )
for( int i = 0; i < NOF_PRODUCER_TRANSACTIONS; i++ ) {
work t( c, "ins" );
- MUTEX_LOCK( cout_mutex );
- cout << "producer transaction " << i << " started" << endl;
- MUTEX_UNLOCK( cout_mutex );
+ 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++ ) {
- result r = t.prepared( "ins" )( i * 1000 + j ).exec( );
+ result r = t.prepared( "ins" )( no * 100000 + i * 1000 + j ).exec( );
assert( r.affected_rows( ) == 1 );
}
t.commit( );
+ if( verbose ) {
+ MUTEX_LOCK( cout_mutex );
+ cout << "producer " << no << " transaction " << i << " terminated" << endl;
+ MUTEX_UNLOCK( cout_mutex );
+ }
}
} catch( sql_error& e ) {
cerr << "producer error, " << e.msg( ) << ": " << e.query( ) << endl;
@@ -71,7 +81,7 @@ static THREAD_FUNC_DECL produce( void *thread_data )
static THREAD_FUNC_DECL consume( void *thread_data )
{
- UNUSED( thread_data );
+ uintptr_t no = (uintptr_t)thread_data;
try {
connection c( "test9.db" );
@@ -79,20 +89,30 @@ static THREAD_FUNC_DECL consume( void *thread_data )
for( int i = 0; i < NOF_CONSUMER_TRANSACTIONS; i++ ) {
work t( c, "sel" );
- MUTEX_LOCK( cout_mutex );
- cout << "consumer transaction " << i << " started" << endl;
- MUTEX_UNLOCK( cout_mutex );
+ 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++ ) {
result r = t.prepared( "sel" ).exec( );
- for( result::const_iterator it = r.begin( ); it < r.end( ); it++ ) {
+ 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( );
+ if( verbose ) {
MUTEX_LOCK( cout_mutex );
- cout << "consumer transaction got " << r.size( ) << " rows." << endl;
+ cout << "consumer " << no << " transaction " << i << " terminated" << endl;
MUTEX_UNLOCK( cout_mutex );
}
- t.commit( );
}
} catch( sql_error& e ) {
cerr << "consumer error, " << e.msg( ) << ": " << e.query( ) << endl;
@@ -119,12 +139,12 @@ int main( ) {
MUTEX_SETUP( cout_mutex );
- for( int i = 0; i < NOF_PRODUCERS; i++ ) {
- THREAD_CREATE( &prod[i], produce, NULL );
+ 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, NULL );
+ THREAD_CREATE( &cons[i], consume, (void *)i );
}
for( int i = 0; i < NOF_PRODUCERS; i++ ) {