|
| 1 | +#include <poll.h> |
| 2 | +#include <pthread.h> |
| 3 | +#include <signal.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <sys/time.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +#include <cdb2api.h> |
| 10 | + |
| 11 | +static char *dbname = "akdb"; |
| 12 | +static char *tier = "default"; |
| 13 | +static int flags; |
| 14 | +static int conns = 10; |
| 15 | + |
| 16 | +static void *work(void *data) |
| 17 | +{ |
| 18 | + cdb2_hndl_tp *dbs[conns]; |
| 19 | + int rc, bad_open = 0; |
| 20 | + for (int i = 0; i < conns; ++i) { |
| 21 | + while ((rc = cdb2_open(&dbs[i], dbname, tier, flags)) != 0) { |
| 22 | + ++bad_open; |
| 23 | + } |
| 24 | + } |
| 25 | + if (bad_open) { |
| 26 | + printf("cdb2_open retries:%d", bad_open); |
| 27 | + } |
| 28 | + while (1) { |
| 29 | + for (int i = 0; i < conns; ++i) { |
| 30 | + cdb2_hndl_tp *db = dbs[i]; |
| 31 | + rc = cdb2_run_statement(db, "select value from generate_series(1, 100)"); |
| 32 | + if (rc != 0) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + while ((rc = cdb2_next_record(db)) == CDB2_OK) |
| 36 | + ; |
| 37 | + if (rc != CDB2_OK_DONE) { |
| 38 | + fprintf(stderr, "cdb2_next_record rc:%d err:%s\n", rc, cdb2_errstr(db)); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return NULL; |
| 43 | +} |
| 44 | +int main(int argc, char **argv) |
| 45 | +{ |
| 46 | + int sigignore(int); |
| 47 | + sigignore(SIGPIPE); |
| 48 | + int thds; |
| 49 | + switch (argc) { |
| 50 | + case 5: tier = argv[4]; |
| 51 | + case 4: dbname = argv[3]; |
| 52 | + case 3: conns = atoi(argv[2]); |
| 53 | + case 2: thds = atoi(argv[1]); |
| 54 | + break; |
| 55 | + default: |
| 56 | + fprintf(stderr, "usage: foo <thds> <conns-per-thd> [dbname] [tier]\n"); |
| 57 | + exit(1); |
| 58 | + } |
| 59 | + printf("thds:%d conns-per-thd:%d dbname:%s tier:%s\n", thds, conns, dbname, tier); |
| 60 | + if (strcmp(tier, "default") != 0) { |
| 61 | + flags |= CDB2_DIRECT_CPU; |
| 62 | + } |
| 63 | + pthread_t t[thds - 1]; |
| 64 | + for (int i = 0; i < thds - 1; ++i) { |
| 65 | + if (pthread_create(&t[i], NULL, work, NULL) != 0) { |
| 66 | + perror("pthread_create"); |
| 67 | + exit(2); |
| 68 | + } |
| 69 | + } |
| 70 | + work("main"); |
| 71 | + void *ret; |
| 72 | + for (int i = 0; i < thds - 1; ++i) { |
| 73 | + pthread_join(t[i], &ret); |
| 74 | + } |
| 75 | +} |
0 commit comments