test_prog.c
text/x-csrc
Filename: test_prog.c
Type: text/x-csrc
Part: 3
#include <libpq-fe.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define USEC 1000000
static void die(const char *msg)
{
fprintf(stderr, "fatal: %s\n", msg);
exit(1);
}
static uint64_t get_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_sec * USEC + tv.tv_usec;
}
int main(int argc, char *argv[])
{
const char *connstr = "host=localhost port=9100 dbname=marko";
PGconn *db;
PGresult *res;
char q[128];
int i;
uint64_t start, end;
const char *tbl;
int cnt;
if (argc != 3) {
printf("usage: prog table cnt\n");
return 1;
}
tbl = argv[1];
cnt = atoi(argv[2]);
db = PQconnectdb(connstr);
if (!db || PQstatus(db) == CONNECTION_BAD)
die("connect");
snprintf(q, sizeof(q), "select * from %s", argv[1]);
start = get_time();
for (i = 0; i < cnt; i++) {
res = PQexec(db, q);
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
die("select");
PQclear(res);
}
end = get_time();
printf("time: %.2f\n", (end - start) / (USEC * 1.0));
PQfinish(db);
return 0;
}