1
1
/*
2
- * $Header: /cvsroot/pgsql/contrib/pgbench/pgbench.c,v 1.16 2002/02/24 00:17:57 ishii Exp $
2
+ * $Header: /cvsroot/pgsql/contrib/pgbench/pgbench.c,v 1.17 2002/07/20 03:02:01 ishii Exp $
3
3
*
4
4
* pgbench: a simple TPC-B like benchmark program for PostgreSQL
5
5
* written by Tatsuo Ishii
39
39
40
40
/* for getrlimit */
41
41
#include <sys/resource.h>
42
- #endif /* WIN32 */
42
+ #endif /* ! WIN32 */
43
43
44
44
/********************************************************************
45
45
* some configurable parameters */
@@ -64,10 +64,14 @@ int tps = 1;
64
64
#define ntellers 10
65
65
#define naccounts 100000
66
66
67
- int remains ; /* number of remained clients */
67
+ FILE * LOGFILE = NULL ;
68
+
69
+ bool use_log ; /* log transaction latencies to a file */
70
+
71
+ int remains ; /* number of remaining clients */
68
72
69
73
int is_connect ; /* establish connection for each
70
- * transactoin */
74
+ * transaction */
71
75
72
76
char * pghost = "" ;
73
77
char * pgport = NULL ;
@@ -80,22 +84,24 @@ char *dbName;
80
84
typedef struct
81
85
{
82
86
PGconn * con ; /* connection handle to DB */
87
+ int id ; /* client No. */
83
88
int state ; /* state No. */
84
89
int cnt ; /* xacts count */
85
90
int ecnt ; /* error count */
86
- int listen ; /* none 0 indicates that an async query
91
+ int listen ; /* 0 indicates that an async query
87
92
* has been sent */
88
93
int aid ; /* account id for this transaction */
89
94
int bid ; /* branch id for this transaction */
90
95
int tid ; /* teller id for this transaction */
91
96
int delta ;
92
97
int abalance ;
98
+ struct timeval txn_begin ; /* used for measuring latencies */
93
99
} CState ;
94
100
95
101
static void
96
102
usage ()
97
103
{
98
- fprintf (stderr , "usage: pgbench [-h hostname][-p port][-c nclients][-t ntransactions][-s scaling_factor][-n][-C][-v][-S][-N][-U login][-P password][-d][dbname]\n" );
104
+ fprintf (stderr , "usage: pgbench [-h hostname][-p port][-c nclients][-t ntransactions][-s scaling_factor][-n][-C][-v][-S][-N][-l][- U login][-P password][-d][dbname]\n" );
99
105
fprintf (stderr , "(initialize mode): pgbench -i [-h hostname][-p port][-s scaling_factor][-U login][-P password][-d][dbname]\n" );
100
106
}
101
107
@@ -235,6 +241,19 @@ doOne(CState * state, int n, int debug, int ttype)
235
241
discard_response (st );
236
242
break ;
237
243
case 6 : /* response to "end" */
244
+ /* transaction finished: record the time it took in the log */
245
+ if (use_log )
246
+ {
247
+ long long diff ;
248
+ struct timeval now ;
249
+
250
+ gettimeofday (& now , 0 );
251
+ diff = (now .tv_sec - st -> txn_begin .tv_sec ) * 1000000 +
252
+ (now .tv_usec - st -> txn_begin .tv_usec );
253
+
254
+ fprintf (LOGFILE , "%d %d %lld\n" , st -> id , st -> cnt , diff );
255
+ }
256
+
238
257
res = PQgetResult (st -> con );
239
258
if (check (state , res , n , PGRES_COMMAND_OK ))
240
259
return ;
@@ -249,7 +268,7 @@ doOne(CState * state, int n, int debug, int ttype)
249
268
250
269
if (++ st -> cnt >= nxacts )
251
270
{
252
- remains -- ; /* I've done */
271
+ remains -- ; /* I'm done */
253
272
if (st -> con != NULL )
254
273
{
255
274
PQfinish (st -> con );
@@ -287,6 +306,8 @@ doOne(CState * state, int n, int debug, int ttype)
287
306
st -> bid = getrand (1 , nbranches * tps );
288
307
st -> tid = getrand (1 , ntellers * tps );
289
308
st -> delta = getrand (1 , 1000 );
309
+ if (use_log )
310
+ gettimeofday (& (st -> txn_begin ), 0 );
290
311
break ;
291
312
case 1 :
292
313
sprintf (sql , "update accounts set abalance = abalance + %d where aid = %d\n" , st -> delta , st -> aid );
@@ -326,7 +347,7 @@ doOne(CState * state, int n, int debug, int ttype)
326
347
}
327
348
else
328
349
{
329
- st -> listen ++ ; /* flags that should be listned */
350
+ st -> listen ++ ; /* flags that should be listened */
330
351
}
331
352
}
332
353
@@ -420,7 +441,7 @@ doSelectOnly(CState * state, int n, int debug)
420
441
}
421
442
else
422
443
{
423
- st -> listen ++ ; /* flags that should be listned */
444
+ st -> listen ++ ; /* flags that should be listened */
424
445
}
425
446
}
426
447
@@ -439,7 +460,7 @@ disconnect_all(CState * state)
439
460
440
461
/* create tables and setup data */
441
462
static void
442
- init ()
463
+ init (void )
443
464
{
444
465
PGconn * con ;
445
466
PGresult * res ;
@@ -616,8 +637,8 @@ printResults(
616
637
printf ("number of clients: %d\n" , nclients );
617
638
printf ("number of transactions per client: %d\n" , nxacts );
618
639
printf ("number of transactions actually processed: %d/%d\n" , normal_xacts , nxacts * nclients );
619
- printf ("tps = %f(including connections establishing)\n" , t1 );
620
- printf ("tps = %f(excluding connections establishing)\n" , t2 );
640
+ printf ("tps = %f (including connections establishing)\n" , t1 );
641
+ printf ("tps = %f (excluding connections establishing)\n" , t2 );
621
642
}
622
643
623
644
@@ -634,11 +655,10 @@ main(int argc, char **argv)
634
655
* testing? */
635
656
int is_full_vacuum = 0 ; /* do full vacuum before testing? */
636
657
int debug = 0 ; /* debug flag */
637
- int ttype = 0 ; /* transaction type. 0: TPC-B, 1: SELECT
638
- * only
639
- 2: skip updation of branches and tellers */
658
+ int ttype = 0 ; /* transaction type. 0: TPC-B, 1: SELECT only,
659
+ * 2: skip update of branches and tellers */
640
660
641
- static CState state [ MAXCLIENTS ]; /* clients status */
661
+ static CState * state ; /* status of clients */
642
662
643
663
struct timeval tv1 ; /* start up time */
644
664
struct timeval tv2 ; /* after establishing all connections to
@@ -658,7 +678,7 @@ main(int argc, char **argv)
658
678
PGconn * con ;
659
679
PGresult * res ;
660
680
661
- while ((c = getopt (argc , argv , "ih:nvp:dc:t:s:U:P:CNS " )) != -1 )
681
+ while ((c = getopt (argc , argv , "ih:nvp:dc:t:s:U:P:CNSl " )) != -1 )
662
682
{
663
683
switch (c )
664
684
{
@@ -690,7 +710,7 @@ main(int argc, char **argv)
690
710
nclients = atoi (optarg );
691
711
if (nclients <= 0 || nclients > MAXCLIENTS )
692
712
{
693
- fprintf (stderr , "wrong number of clients: %d\n" , nclients );
713
+ fprintf (stderr , "invalid number of clients: %d\n" , nclients );
694
714
exit (1 );
695
715
}
696
716
#ifndef __CYGWIN__
@@ -719,15 +739,15 @@ main(int argc, char **argv)
719
739
tps = atoi (optarg );
720
740
if (tps <= 0 )
721
741
{
722
- fprintf (stderr , "wrong scaling factor: %d\n" , tps );
742
+ fprintf (stderr , "invalid scaling factor: %d\n" , tps );
723
743
exit (1 );
724
744
}
725
745
break ;
726
746
case 't' :
727
747
nxacts = atoi (optarg );
728
748
if (nxacts <= 0 )
729
749
{
730
- fprintf (stderr , "wrong number of transactions: %d\n" , nxacts );
750
+ fprintf (stderr , "invalid number of transactions: %d\n" , nxacts );
731
751
exit (1 );
732
752
}
733
753
break ;
@@ -737,6 +757,9 @@ main(int argc, char **argv)
737
757
case 'P' :
738
758
pwd = optarg ;
739
759
break ;
760
+ case 'l' :
761
+ use_log = true;
762
+ break ;
740
763
default :
741
764
usage ();
742
765
exit (1 );
@@ -761,6 +784,23 @@ main(int argc, char **argv)
761
784
762
785
remains = nclients ;
763
786
787
+ state = (CState * ) malloc (sizeof (* state ) * nclients );
788
+ memset (state , 0 , sizeof (* state ));
789
+
790
+ if (use_log )
791
+ {
792
+ char logpath [64 ];
793
+
794
+ snprintf (logpath , 64 , "pgbench_log.%d" , getpid ());
795
+ LOGFILE = fopen (logpath , "w" );
796
+
797
+ if (LOGFILE == NULL )
798
+ {
799
+ fprintf (stderr , "Couldn't open logfile \"%s\": %s" , logpath , strerror (errno ));
800
+ exit (1 );
801
+ }
802
+ }
803
+
764
804
if (debug )
765
805
{
766
806
printf ("pghost: %s pgport: %s nclients: %d nxacts: %d dbName: %s\n" ,
@@ -860,6 +900,7 @@ main(int argc, char **argv)
860
900
/* make connections to the database */
861
901
for (i = 0 ; i < nclients ; i ++ )
862
902
{
903
+ state [i ].id = i ;
863
904
if ((state [i ].con = doConnect ()) == NULL )
864
905
exit (1 );
865
906
}
@@ -868,7 +909,7 @@ main(int argc, char **argv)
868
909
/* time after connections set up */
869
910
gettimeofday (& tv2 , 0 );
870
911
871
- /* send start up quries in async manner */
912
+ /* send start up queries in async manner */
872
913
for (i = 0 ; i < nclients ; i ++ )
873
914
{
874
915
if (ttype == 0 || ttype == 2 )
@@ -885,6 +926,8 @@ main(int argc, char **argv)
885
926
/* get end time */
886
927
gettimeofday (& tv3 , 0 );
887
928
printResults (ttype , state , & tv1 , & tv2 , & tv3 );
929
+ if (LOGFILE )
930
+ fclose (LOGFILE );
888
931
exit (0 );
889
932
}
890
933
@@ -899,7 +942,7 @@ main(int argc, char **argv)
899
942
900
943
if (sock < 0 )
901
944
{
902
- fprintf (stderr , "Client %d: PQsock failed\n" , i );
945
+ fprintf (stderr , "Client %d: PQsocket failed\n" , i );
903
946
disconnect_all (state );
904
947
exit (1 );
905
948
}
0 commit comments