|
| 1 | +#include <time.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdarg.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <inttypes.h> |
| 8 | +#include <sys/time.h> |
| 9 | +#include <pthread.h> |
| 10 | +#include <libpq-fe.h> |
| 11 | + |
| 12 | +#define MAX_THREADS 1024 |
| 13 | +#define TEXTOID 25 |
| 14 | + |
| 15 | +char const* connection = "dbname=regression host=localhost port=5432 sslmode=disable"; |
| 16 | +int n_records = 100; |
| 17 | +int n_clients = 100; |
| 18 | +int first_tid; |
| 19 | +long inserts[MAX_THREADS]; |
| 20 | +volatile int termination; |
| 21 | + |
| 22 | +void* worker(void* arg) |
| 23 | +{ |
| 24 | + PGconn* con = PQconnectdb(connection); |
| 25 | + PGresult *res; |
| 26 | + ConnStatusType status = PQstatus(con); |
| 27 | + Oid paramTypes[1] = { TEXTOID }; |
| 28 | + int id = (size_t)arg; |
| 29 | + int i; |
| 30 | + |
| 31 | + if (status != CONNECTION_OK) |
| 32 | + { |
| 33 | + fprintf(stderr, "Could not establish connection to server %s, error = %s", |
| 34 | + connection, PQerrorMessage(con)); |
| 35 | + exit(1); |
| 36 | + } |
| 37 | + PQprepare(con, |
| 38 | + "ins", |
| 39 | + "insert into t values ($1)", |
| 40 | + 1, |
| 41 | + paramTypes); |
| 42 | + |
| 43 | + for (i = 0; !termination; i++) { |
| 44 | + char key[64]; |
| 45 | + char const* paramValues[] = {key}; |
| 46 | + sprintf(key, "%d.%d", first_tid + id, i); |
| 47 | + res = PQexecPrepared(con, "ins", 1, paramValues, NULL, NULL, 0); |
| 48 | + if (PQresultStatus(res) != PGRES_COMMAND_OK) { |
| 49 | + fprintf(stderr, "Insert %d failed: %s\n", i, PQresultErrorMessage(res)); |
| 50 | + exit(1); |
| 51 | + } |
| 52 | + if (strcmp(PQcmdTuples(res), "1") != 0) { |
| 53 | + fprintf(stderr, "Insert affect wrong number of tuples: %s\n", PQcmdTuples(res)); |
| 54 | + exit(1); |
| 55 | + } |
| 56 | + inserts[id] += 1; |
| 57 | + PQclear(res); |
| 58 | + } |
| 59 | + return NULL; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +int main (int argc, char* argv[]) |
| 65 | +{ |
| 66 | + int i; |
| 67 | + pthread_t threads[MAX_THREADS]; |
| 68 | + long thread_inserts[MAX_THREADS]; |
| 69 | + int test_duration = 10; |
| 70 | + time_t finish; |
| 71 | + int iteration = 0; |
| 72 | + int initialize = 0; |
| 73 | + long total; |
| 74 | + PGconn* con; |
| 75 | + |
| 76 | + if (argc == 1) { |
| 77 | + fprintf(stderr, "Use -h to show usage options\n"); |
| 78 | + return 1; |
| 79 | + } |
| 80 | + |
| 81 | + for (i = 1; i < argc; i++) { |
| 82 | + if (argv[i][0] == '-') { |
| 83 | + switch (argv[i][1]) { |
| 84 | + case 'c': |
| 85 | + n_clients = atoi(argv[++i]); |
| 86 | + continue; |
| 87 | + case 't': |
| 88 | + test_duration = atoi(argv[++i]); |
| 89 | + continue; |
| 90 | + case 'd': |
| 91 | + connection = argv[++i]; |
| 92 | + continue; |
| 93 | + case 'i': |
| 94 | + initialize = 1; |
| 95 | + continue; |
| 96 | + case 'f': |
| 97 | + first_tid = atoi(argv[++i]); |
| 98 | + continue; |
| 99 | + } |
| 100 | + } |
| 101 | + printf("Options:\n" |
| 102 | + "\t-i\tinitialize database\n" |
| 103 | + "\t-f\tfirst thread ID (default 0)\n" |
| 104 | + "\t-c\tnumber of client (default 100)\n" |
| 105 | + "\t-t\ttest duration (default 10 sec)\n" |
| 106 | + "\t-d\tconnection string ('host=localhost port=5432')\n"); |
| 107 | + return 1; |
| 108 | + } |
| 109 | + con = PQconnectdb(connection); |
| 110 | + if (initialize) { |
| 111 | + PQexec(con, "drop table if exists t"); |
| 112 | + } |
| 113 | + PQexec(con, "create table if not exists t(k text primary key)"); |
| 114 | + |
| 115 | + for (i = 0; i < n_clients; i++) { |
| 116 | + thread_inserts[i] = 0; |
| 117 | + pthread_create(&threads[i], NULL, worker, (void*)(size_t)i); |
| 118 | + } |
| 119 | + finish = time(NULL) + test_duration; |
| 120 | + do { |
| 121 | + total = 0; |
| 122 | + sleep(1); |
| 123 | + for (i = 0; i < n_clients; i++) { |
| 124 | + total += inserts[i] - thread_inserts[i]; |
| 125 | + thread_inserts[i] = inserts[i]; |
| 126 | + } |
| 127 | + printf("%d: %ld\n", ++iteration, total); |
| 128 | + } while (time(NULL) < finish); |
| 129 | + |
| 130 | + total = 0; |
| 131 | + for (i = 0; i < n_clients; i++) { |
| 132 | + total += inserts[i]; |
| 133 | + } |
| 134 | + printf("Total %ld inserts, %ld inserts per second\n", total, total/test_duration); |
| 135 | + |
| 136 | + termination = 1; |
| 137 | + |
| 138 | + for (i = 0; i < n_clients; i++) { |
| 139 | + pthread_join(threads[i], NULL); |
| 140 | + } |
| 141 | + return 0; |
| 142 | +} |
0 commit comments