static inline int process_json_response(BUFFER *b) { return discard_response(b, "json"); }
static inline int process_graphite_response(BUFFER *b) { return discard_response(b, "graphite"); }
static inline int process_opentsdb_response(BUFFER *b) { return discard_response(b, "opentsdb"); }
static void doCustom(CState * state, int n, int debug) { PGresult *res; CState *st = &state[n]; Command **commands; top: commands = sql_files[st->use_file]; if (st->listen) { /* are we receiver? */ if (commands[st->state]->type == SQL_COMMAND) { if (debug) fprintf(stderr, "client %d receiving\n", n); if (!PQconsumeInput(st->con)) { /* there's something wrong */ fprintf(stderr, "Client %d aborted in state %d. Probably the backend died while processing.\n", n, st->state); remains--; /* I've aborted */ PQfinish(st->con); st->con = NULL; return; } if (PQisBusy(st->con)) return; /* don't have the whole result yet */ } /* * transaction finished: record the time it took in the log */ if (use_log && commands[st->state + 1] == NULL) { double diff; struct timeval now; gettimeofday(&now, NULL); diff = (int) (now.tv_sec - st->txn_begin.tv_sec) * 1000000.0 + (int) (now.tv_usec - st->txn_begin.tv_usec); fprintf(LOGFILE, "%d %d %.0f\n", st->id, st->cnt, diff); } if (commands[st->state]->type == SQL_COMMAND) { res = PQgetResult(st->con); if (pg_strncasecmp(commands[st->state]->argv[0], "select", 6) != 0) { if (check(state, res, n, PGRES_COMMAND_OK)) return; } else { if (check(state, res, n, PGRES_TUPLES_OK)) return; } PQclear(res); discard_response(st); } if (commands[st->state + 1] == NULL) { if (is_connect) { PQfinish(st->con); st->con = NULL; } if (++st->cnt >= nxacts) { remains--; /* I've done */ if (st->con != NULL) { PQfinish(st->con); st->con = NULL; } return; } } /* increment state counter */ st->state++; if (commands[st->state] == NULL) { st->state = 0; st->use_file = getrand(0, num_files - 1); commands = sql_files[st->use_file]; } } if (st->con == NULL) { if ((st->con = doConnect()) == NULL) { fprintf(stderr, "Client %d aborted in establishing connection.\n", n); remains--; /* I've aborted */ PQfinish(st->con); st->con = NULL; return; } } if (use_log && st->state == 0) gettimeofday(&(st->txn_begin), NULL); if (commands[st->state]->type == SQL_COMMAND) { char *sql; if ((sql = strdup(commands[st->state]->argv[0])) == NULL || (sql = assignVariables(st, sql)) == NULL) { fprintf(stderr, "out of memory\n"); st->ecnt++; return; } if (debug) fprintf(stderr, "client %d sending %s\n", n, sql); if (PQsendQuery(st->con, sql) == 0) { if (debug) fprintf(stderr, "PQsendQuery(%s)failed\n", sql); st->ecnt++; } else { st->listen = 1; /* flags that should be listened */ } free(sql); } else if (commands[st->state]->type == META_COMMAND) { int argc = commands[st->state]->argc, i; char **argv = commands[st->state]->argv; if (debug) { fprintf(stderr, "client %d executing \\%s", n, argv[0]); for (i = 1; i < argc; i++) fprintf(stderr, " %s", argv[i]); fprintf(stderr, "\n"); } if (pg_strcasecmp(argv[0], "setrandom") == 0) { char *var; int min, max; char res[64]; if (*argv[2] == ':') { if ((var = getVariable(st, argv[2] + 1)) == NULL) { fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[2]); st->ecnt++; return; } min = atoi(var); } else min = atoi(argv[2]); #ifdef NOT_USED if (min < 0) { fprintf(stderr, "%s: invalid minimum number %d\n", argv[0], min); st->ecnt++; return; } #endif if (*argv[3] == ':') { if ((var = getVariable(st, argv[3] + 1)) == NULL) { fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[3]); st->ecnt++; return; } max = atoi(var); } else max = atoi(argv[3]); if (max < min || max > MAX_RANDOM_VALUE) { fprintf(stderr, "%s: invalid maximum number %d\n", argv[0], max); st->ecnt++; return; } #ifdef DEBUG printf("min: %d max: %d random: %d\n", min, max, getrand(min, max)); #endif snprintf(res, sizeof(res), "%d", getrand(min, max)); if (putVariable(st, argv[1], res) == false) { fprintf(stderr, "%s: out of memory\n", argv[0]); st->ecnt++; return; } st->listen = 1; } else if (pg_strcasecmp(argv[0], "set") == 0) { char *var; int ope1, ope2; char res[64]; if (*argv[2] == ':') { if ((var = getVariable(st, argv[2] + 1)) == NULL) { fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[2]); st->ecnt++; return; } ope1 = atoi(var); } else ope1 = atoi(argv[2]); if (argc < 5) snprintf(res, sizeof(res), "%d", ope1); else { if (*argv[4] == ':') { if ((var = getVariable(st, argv[4] + 1)) == NULL) { fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[4]); st->ecnt++; return; } ope2 = atoi(var); } else ope2 = atoi(argv[4]); if (strcmp(argv[3], "+") == 0) snprintf(res, sizeof(res), "%d", ope1 + ope2); else if (strcmp(argv[3], "-") == 0) snprintf(res, sizeof(res), "%d", ope1 - ope2); else if (strcmp(argv[3], "*") == 0) snprintf(res, sizeof(res), "%d", ope1 * ope2); else if (strcmp(argv[3], "/") == 0) { if (ope2 == 0) { fprintf(stderr, "%s: division by zero\n", argv[0]); st->ecnt++; return; } snprintf(res, sizeof(res), "%d", ope1 / ope2); } else { fprintf(stderr, "%s: unsupported operator %s\n", argv[0], argv[3]); st->ecnt++; return; } } if (putVariable(st, argv[1], res) == false) { fprintf(stderr, "%s: out of memory\n", argv[0]); st->ecnt++; return; } st->listen = 1; } goto top; } }