~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/test/test_thread_implicit.pgc

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      Thread test program
 
3
 *      by Lee Kindness.
 
4
 */
 
5
 
 
6
/* #define ECPGDEBUG */
 
7
 
 
8
#include <pthread.h>
 
9
#include <stdlib.h>
 
10
 
 
11
void *test_thread(void *arg);
 
12
 
 
13
EXEC SQL BEGIN DECLARE SECTION;
 
14
char *l_dbname;
 
15
EXEC SQL END DECLARE SECTION;
 
16
int nthreads   =  2;
 
17
int iterations = 10;
 
18
 
 
19
int main(int argc, char *argv[])
 
20
{
 
21
#ifdef ECPGDEBUG
 
22
  char debugfilename[] = "thread_test_implicit.log";
 
23
  FILE *debugfile;
 
24
#endif
 
25
  pthread_t *threads;
 
26
  int n;
 
27
  EXEC SQL BEGIN DECLARE SECTION;
 
28
  int l_rows;
 
29
  EXEC SQL END DECLARE SECTION;
 
30
 
 
31
  /* parse command line arguments */
 
32
  if( (argc < 2) || (argc > 4) )
 
33
    {
 
34
      fprintf(stderr, "Usage: %s dbname [threads] [iterations_per_thread]\n", argv[0]);
 
35
      return( 1 );
 
36
    }
 
37
  l_dbname = argv[1];
 
38
  if( argc >= 3 )
 
39
    nthreads = atoi(argv[2]);
 
40
  if( argc == 4 )
 
41
    iterations = atoi(argv[3]);
 
42
 
 
43
  /* open ECPG debug log? */
 
44
#ifdef ECPGDEBUG
 
45
  debugfile = fopen(debugfilename, "w");
 
46
  if( debugfile != NULL )
 
47
    ECPGdebug(1, debugfile);
 
48
  else
 
49
    fprintf(stderr, "Cannot open ECPG debug log: %s\n", debugfilename);
 
50
#endif
 
51
 
 
52
  /* setup test_thread table */
 
53
  EXEC SQL CONNECT TO:l_dbname;
 
54
  EXEC SQL DROP TABLE test_thread; /* DROP might fail */
 
55
  EXEC SQL COMMIT;
 
56
  EXEC SQL CREATE TABLE
 
57
    test_thread(tstamp    TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
 
58
                thread    TEXT      NOT NULL,
 
59
                iteration INTEGER   NOT NULL,
 
60
                PRIMARY KEY(thread, iteration));
 
61
  EXEC SQL COMMIT;
 
62
  EXEC SQL DISCONNECT;
 
63
 
 
64
  /* create, and start, threads */
 
65
  threads = calloc(nthreads, sizeof(pthread_t));
 
66
  if( threads == NULL )
 
67
    {
 
68
      fprintf(stderr, "Cannot alloc memory\n");
 
69
      return( 1 );
 
70
    }
 
71
  for( n = 0; n < nthreads; n++ )
 
72
    {
 
73
      pthread_create(&threads[n], NULL, test_thread, (void *)n + 1);
 
74
    }
 
75
 
 
76
  /* wait for thread completion */
 
77
  for( n = 0; n < nthreads; n++ )
 
78
    {
 
79
      pthread_join(threads[n], NULL);
 
80
    }
 
81
  free(threads);
 
82
 
 
83
  /* and check results */
 
84
  EXEC SQL CONNECT TO :l_dbname;
 
85
  EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
 
86
  EXEC SQL COMMIT;
 
87
  EXEC SQL DISCONNECT;
 
88
  if( l_rows == (nthreads * iterations) )
 
89
    printf("\nSuccess.\n");
 
90
  else
 
91
    printf("\nERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
 
92
 
 
93
  /* close ECPG debug log? */
 
94
#ifdef ECPGDEBUG
 
95
  if( debugfile != NULL )
 
96
    {
 
97
      ECPGdebug(0, debugfile);
 
98
      fclose(debugfile);
 
99
    }
 
100
#endif
 
101
 
 
102
  return( 0 );
 
103
}
 
104
 
 
105
void *test_thread(void *arg)
 
106
{
 
107
  long threadnum = (long)arg;
 
108
  EXEC SQL BEGIN DECLARE SECTION;
 
109
  int  l_i;
 
110
  char l_connection[128];
 
111
  EXEC SQL END DECLARE SECTION;
 
112
 
 
113
  /* build up connection name, and connect to database */
 
114
  snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
 
115
  EXEC SQL WHENEVER sqlerror sqlprint;
 
116
  EXEC SQL CONNECT TO :l_dbname AS :l_connection;
 
117
  if( sqlca.sqlcode != 0 )
 
118
    {
 
119
      printf("%s: ERROR: cannot connect to database!\n", l_connection);
 
120
      return( NULL );
 
121
    }
 
122
  EXEC SQL BEGIN;
 
123
 
 
124
  /* insert into test_thread table */
 
125
  for( l_i = 1; l_i <= iterations; l_i++ )
 
126
    {
 
127
      printf("%s: inserting %d\n", l_connection, l_i);
 
128
      EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
 
129
      if( sqlca.sqlcode == 0 )
 
130
        printf("%s: insert done\n", l_connection);
 
131
      else
 
132
        printf("%s: ERROR: insert failed!\n", l_connection);
 
133
    }
 
134
 
 
135
  /* all done */
 
136
  EXEC SQL COMMIT;
 
137
  EXEC SQL DISCONNECT :l_connection;
 
138
  printf("%s: done!\n", l_connection);
 
139
  return( NULL );
 
140
}