Whilst doing some profiling of the server I found it useful to add an
option to pgbench to introduce a delay between client connection setup
and the start of the benchmark itself to allow me time to attach the
profiler to one of the backends.

Attached is the patch in case anyone finds a use for it, or if it's
deemed to be generally useful enough for inclusion in 8.4.

Regards, Dave.
Index: doc/src/sgml/pgbench.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/pgbench.sgml,v
retrieving revision 1.4
diff -c -r1.4 pgbench.sgml
*** doc/src/sgml/pgbench.sgml	10 Dec 2007 05:32:51 -0000	1.4
--- doc/src/sgml/pgbench.sgml	10 Dec 2007 19:05:13 -0000
***************
*** 250,255 ****
--- 250,264 ----
        </entry>
       </row>
       <row>
+       <entry><literal>-w</literal> <replaceable>startup_delay</></entry>
+       <entry>
+        Pause for the specified number of seconds after creating the
+        client connections. This is useful to allow time to connect
+        a debugger or profiler to a backend server process before the
+        benchmark is run.
+       </entry>
+      </row>
+      <row>
        <entry><literal>-d</literal></entry>
        <entry>
         Print debugging output.
Index: contrib/pgbench/pgbench.c
===================================================================
RCS file: /projects/cvsroot/pgsql/contrib/pgbench/pgbench.c,v
retrieving revision 1.74
diff -c -r1.74 pgbench.c
*** contrib/pgbench/pgbench.c	15 Nov 2007 21:14:31 -0000	1.74
--- contrib/pgbench/pgbench.c	10 Dec 2007 18:58:55 -0000
***************
*** 188,194 ****
  static void
  usage(void)
  {
! 	fprintf(stderr, "usage: pgbench [-h hostname][-p port][-c nclients][-t ntransactions][-s scaling_factor][-D varname=value][-n][-C][-v][-S][-N][-f filename][-l][-U login][-P password][-d][dbname]\n");
  	fprintf(stderr, "(initialize mode): pgbench -i [-h hostname][-p port][-s scaling_factor] [-F fillfactor] [-U login][-P password][-d][dbname]\n");
  }

--- 188,194 ----
  static void
  usage(void)
  {
! 	fprintf(stderr, "usage: pgbench [-h hostname][-p port][-c nclients][-t ntransactions][-s scaling_factor][-D varname=value][-n][-C][-v][-S][-N][-f filename][-l][-U login][-P password][-w startup_delay][-d][dbname]\n");
  	fprintf(stderr, "(initialize mode): pgbench -i [-h hostname][-p port][-s scaling_factor] [-F fillfactor] [-U login][-P password][-d][dbname]\n");
  }

***************
*** 1163,1169 ****
  printResults(
  			 int ttype, CState * state,
  			 struct timeval * tv1, struct timeval * tv2,
! 			 struct timeval * tv3)
  {
  	double		t1,
  				t2;
--- 1163,1169 ----
  printResults(
  			 int ttype, CState * state,
  			 struct timeval * tv1, struct timeval * tv2,
! 			 struct timeval * tv3, int startup_delay)
  {
  	double		t1,
  				t2;
***************
*** 1174,1183 ****
  	for (i = 0; i < nclients; i++)
  		normal_xacts += state[i].cnt;

! 	t1 = (tv3->tv_sec - tv1->tv_sec) * 1000000.0 + (tv3->tv_usec - tv1->tv_usec);
  	t1 = normal_xacts * 1000000.0 / t1;

! 	t2 = (tv3->tv_sec - tv2->tv_sec) * 1000000.0 + (tv3->tv_usec - tv2->tv_usec);
  	t2 = normal_xacts * 1000000.0 / t2;

  	if (ttype == 0)
--- 1174,1183 ----
  	for (i = 0; i < nclients; i++)
  		normal_xacts += state[i].cnt;

! 	t1 = (tv3->tv_sec - (tv1->tv_sec + startup_delay)) * 1000000.0 + (tv3->tv_usec - tv1->tv_usec);
  	t1 = normal_xacts * 1000000.0 / t1;

! 	t2 = (tv3->tv_sec - (tv2->tv_sec + startup_delay)) * 1000000.0 + (tv3->tv_usec - tv2->tv_usec);
  	t2 = normal_xacts * 1000000.0 / t2;

  	if (ttype == 0)
***************
*** 1217,1222 ****
--- 1217,1224 ----
  	struct timeval tv2;			/* after establishing all connections to the
  								 * backend */
  	struct timeval tv3;			/* end time */
+
+ 	int         startup_delay = 0; /* Post-connect delay */

  	int			i;

***************
*** 1258,1264 ****

  	memset(state, 0, sizeof(*state));

! 	while ((c = getopt(argc, argv, "ih:nvp:dc:t:s:U:P:CNSlf:D:F:")) != -1)
  	{
  		switch (c)
  		{
--- 1260,1266 ----

  	memset(state, 0, sizeof(*state));

! 	while ((c = getopt(argc, argv, "ih:nvp:dc:t:s:U:P:CNSlf:D:F:w:")) != -1)
  	{
  		switch (c)
  		{
***************
*** 1371,1376 ****
--- 1373,1386 ----
  					exit(1);
  				}
  				break;
+ 			case 'w':
+                 startup_delay = atoi(optarg);
+    				if (startup_delay < 0)
+ 				{
+ 					fprintf(stderr, "invalid startup_delay: %d\n", startup_delay);
+ 					exit(1);
+ 				}
+                 break;
  			default:
  				usage();
  				exit(1);
***************
*** 1553,1558 ****
--- 1563,1580 ----
  	/* time after connections set up */
  	gettimeofday(&tv2, NULL);

+ 	if (startup_delay)
+ 	{
+         fprintf(stderr, "pausing for %d seconds...", startup_delay);
+  #ifndef WIN32
+         sleep(startup_delay);
+  #else
+         Sleep(startup_delay * 1000);
+  #endif
+         fprintf(stderr, "end.\n");
+
+     }
+
  	/* process bultin SQL scripts */
  	switch (ttype)
  	{
***************
*** 1600,1606 ****
  			disconnect_all(state);
  			/* get end time */
  			gettimeofday(&tv3, NULL);
! 			printResults(ttype, state, &tv1, &tv2, &tv3);
  			if (LOGFILE)
  				fclose(LOGFILE);
  			exit(0);
--- 1622,1628 ----
  			disconnect_all(state);
  			/* get end time */
  			gettimeofday(&tv3, NULL);
! 			printResults(ttype, state, &tv1, &tv2, &tv3, startup_delay);
  			if (LOGFILE)
  				fclose(LOGFILE);
  			exit(0);
---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to [EMAIL PROTECTED] so that your
       message can get through to the mailing list cleanly

Reply via email to