11/*
2- *$PostgreSQL: pgsql/contrib/ pgbench/pgbench.c,v 1.77 2008/03/12 02:18:33 tgl Exp $
2+ * pgbench.c
33 *
4- *pgbench: a simple benchmark program for PostgreSQL
5- * written by Tatsuo Ishii
4+ *A simple benchmark program for PostgreSQL
5+ *Originally written by Tatsuo Ishii and enhanced by many contributors.
66 *
7- * Copyright (c) 2000-2007Tatsuo Ishii
7+ * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.78 2008/03/19 00:29:35 ishii Exp $
8+ * Copyright (c) 2000-2008, PostgreSQL Global Development Group
9+ * ALL RIGHTS RESERVED;
10+ *
11+ * Permission to use, copy, modify, and distribute this software and its
12+ * documentation for any purpose, without fee, and without a written agreement
13+ * is hereby granted, provided that the above copyright notice and this
14+ * paragraph and the following two paragraphs appear in all copies.
15+ *
16+ * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
17+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
18+ * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
19+ * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
20+ * POSSIBILITY OF SUCH DAMAGE.
21+ *
22+ * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
23+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24+ * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
25+ * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
26+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
827 *
9- * Permission to use, copy, modify, and distribute this software and
10- * its documentation for any purpose and without fee is hereby
11- * granted, provided that the above copyright notice appear in all
12- * copies and that both that copyright notice and this permission
13- * notice appear in supporting documentation, and that the name of the
14- * author not be used in advertising or publicity pertaining to
15- * distribution of the software without specific, written prior
16- * permission. The author makes no representations about the
17- * suitability of this software for any purpose. It is provided "as
18- * is" without express or implied warranty.
1928 */
2029#include "postgres_fe.h"
2130
@@ -184,6 +193,39 @@ static char *select_only = {
184193"SELECT abalance FROM accounts WHERE aid = :aid;\n"
185194};
186195
196+ /* Connection overhead time */
197+ static struct timeval conn_total_time = {0 ,0 };
198+
199+ /* Calculate total time */
200+ static void
201+ addTime (struct timeval * t1 ,struct timeval * t2 ,struct timeval * result )
202+ {
203+ int sec = t1 -> tv_sec + t2 -> tv_sec ;
204+ int usec = t1 -> tv_usec + t2 -> tv_usec ;
205+ if (usec >=1000000 )
206+ {
207+ usec -= 1000000 ;
208+ sec ++ ;
209+ }
210+ result -> tv_sec = sec ;
211+ result -> tv_usec = usec ;
212+ }
213+
214+ /* Calculate time difference */
215+ static void
216+ diffTime (struct timeval * t1 ,struct timeval * t2 ,struct timeval * result )
217+ {
218+ int sec = t1 -> tv_sec - t2 -> tv_sec ;
219+ int usec = t1 -> tv_usec - t2 -> tv_usec ;
220+ if (usec < 0 )
221+ {
222+ usec += 1000000 ;
223+ sec -- ;
224+ }
225+ result -> tv_sec = sec ;
226+ result -> tv_usec = usec ;
227+ }
228+
187229static void
188230usage (void )
189231{
@@ -564,6 +606,9 @@ doCustom(CState * state, int n, int debug)
564606
565607if (st -> con == NULL )
566608{
609+ struct timeval t1 ,t2 ,t3 ;
610+
611+ gettimeofday (& t1 ,NULL );
567612if ((st -> con = doConnect ())== NULL )
568613{
569614fprintf (stderr ,"Client %d aborted in establishing connection.\n" ,
@@ -573,6 +618,9 @@ doCustom(CState * state, int n, int debug)
573618st -> con = NULL ;
574619return ;
575620}
621+ gettimeofday (& t2 ,NULL );
622+ diffTime (& t2 ,& t1 ,& t3 );
623+ addTime (& conn_total_time ,& t3 ,& conn_total_time );
576624}
577625
578626if (use_log && st -> state == 0 )
@@ -1193,8 +1241,7 @@ process_builtin(char *tb)
11931241static void
11941242printResults (
11951243int ttype ,CState * state ,
1196- struct timeval * tv1 ,struct timeval * tv2 ,
1197- struct timeval * tv3 )
1244+ struct timeval * start_time ,struct timeval * end_time )
11981245{
11991246double t1 ,
12001247t2 ;
@@ -1205,10 +1252,11 @@ printResults(
12051252for (i = 0 ;i < nclients ;i ++ )
12061253normal_xacts += state [i ].cnt ;
12071254
1208- t1 = (tv3 -> tv_sec - tv1 -> tv_sec )* 1000000.0 + (tv3 -> tv_usec - tv1 -> tv_usec );
1255+ t1 = (end_time -> tv_sec - start_time -> tv_sec )* 1000000.0 + (end_time -> tv_usec - start_time -> tv_usec );
12091256t1 = normal_xacts * 1000000.0 /t1 ;
12101257
1211- t2 = (tv3 -> tv_sec - tv2 -> tv_sec )* 1000000.0 + (tv3 -> tv_usec - tv2 -> tv_usec );
1258+ t2 = (end_time -> tv_sec - start_time -> tv_sec - conn_total_time .tv_sec )* 1000000.0 +
1259+ (end_time -> tv_usec - start_time -> tv_usec - conn_total_time .tv_usec );
12121260t2 = normal_xacts * 1000000.0 /t2 ;
12131261
12141262if (ttype == 0 )
@@ -1244,10 +1292,8 @@ main(int argc, char **argv)
12441292
12451293CState * state ;/* status of clients */
12461294
1247- struct timeval tv1 ;/* start up time */
1248- struct timeval tv2 ;/* after establishing all connections to the
1249- * backend */
1250- struct timeval tv3 ;/* end time */
1295+ struct timeval start_time ;/* start up time */
1296+ struct timeval end_time ;/* end time */
12511297
12521298int i ;
12531299
@@ -1561,26 +1607,29 @@ main(int argc, char **argv)
15611607PQfinish (con );
15621608
15631609/* set random seed */
1564- gettimeofday (& tv1 ,NULL );
1565- srandom ((unsignedint )tv1 .tv_usec );
1610+ gettimeofday (& start_time ,NULL );
1611+ srandom ((unsignedint )start_time .tv_usec );
15661612
15671613/* get start up time */
1568- gettimeofday (& tv1 ,NULL );
1614+ gettimeofday (& start_time ,NULL );
15691615
15701616if (is_connect == 0 )
15711617{
1618+ struct timeval t ,now ;
1619+
15721620/* make connections to the database */
15731621for (i = 0 ;i < nclients ;i ++ )
15741622{
15751623state [i ].id = i ;
15761624if ((state [i ].con = doConnect ())== NULL )
15771625exit (1 );
15781626}
1627+ /* time after connections set up */
1628+ gettimeofday (& now ,NULL );
1629+ diffTime (& now ,& start_time ,& t );
1630+ addTime (& conn_total_time ,& t ,& conn_total_time );
15791631}
15801632
1581- /* time after connections set up */
1582- gettimeofday (& tv2 ,NULL );
1583-
15841633/* process bultin SQL scripts */
15851634switch (ttype )
15861635{
@@ -1627,8 +1676,8 @@ main(int argc, char **argv)
16271676{/* all done ? */
16281677disconnect_all (state );
16291678/* get end time */
1630- gettimeofday (& tv3 ,NULL );
1631- printResults (ttype ,state ,& tv1 ,& tv2 , & tv3 );
1679+ gettimeofday (& end_time ,NULL );
1680+ printResults (ttype ,state ,& start_time ,& end_time );
16321681if (LOGFILE )
16331682fclose (LOGFILE );
16341683exit (0 );
@@ -1728,7 +1777,7 @@ main(int argc, char **argv)
17281777
17291778if (state [i ].ecnt > prev_ecnt && commands [state [i ].state ]-> type == META_COMMAND )
17301779{
1731- fprintf (stderr ,"Client %d aborted in state %d. Execution meta-command failed.\n" ,i ,state [i ].state );
1780+ fprintf (stderr ,"Client %d aborted in state %d. Executionof meta-command failed.\n" ,i ,state [i ].state );
17321781remains -- ;/* I've aborted */
17331782PQfinish (state [i ].con );
17341783state [i ].con = NULL ;