📄 obench.c
字号:
} rc = SQLGetData(s, 1, SQL_C_LONG, &aBalance, sizeof (aBalance), NULL); if (!SQL_SUCCEEDED(rc)) { if (verbose) { fprintf(stderr, "doOne: fail#6 rc=%d\n", rc); } goto transfail; } SQLFreeStmt(s, SQL_CLOSE); sprintf(sqlbuf, "UPDATE tellers " "SET Tbalance = Tbalance + %d WHERE Tid = %d", delta, tid); rc = SQLExecDirect(s, (SQLCHAR *) sqlbuf, SQL_NTS); if (!SQL_SUCCEEDED(rc)) { if (verbose) { fprintf(stderr, "doOne: fail#7 rc=%d\n", rc); } goto transfail; } SQLFreeStmt(s, SQL_CLOSE); sprintf(sqlbuf, "UPDATE branches " "SET Bbalance = Bbalance + %d WHERE Bid = %d", delta, bid); rc = SQLExecDirect(s, (SQLCHAR *) sqlbuf, SQL_NTS); if (!SQL_SUCCEEDED(rc)) { if (verbose) { fprintf(stderr, "doOne: fail#8 rc=%d\n", rc); } goto transfail; } SQLFreeStmt(s, SQL_CLOSE); sprintf(sqlbuf, "INSERT INTO history" "(Tid, Bid, Aid, delta) VALUES" "(%d, %d, %d, %d)", tid, bid, aid, delta); rc = SQLExecDirect(s, (SQLCHAR *) sqlbuf, SQL_NTS); if (!SQL_SUCCEEDED(rc)) { if (verbose) { fprintf(stderr, "doOne: fail#9 rc=%d\n", rc); } goto transfail; } SQLFreeStmt(s, SQL_CLOSE); if (transactions > 0) { rc = SQLTransact(NULL, dbc, SQL_COMMIT); if (!SQL_SUCCEEDED(rc)) { if (verbose) { fprintf(stderr, "doOne: fail#10 rc=%d\n", rc); } goto transfail; } } SQLFreeStmt(s, SQL_DROP); return;transfail: incrementFailedTransactionCount(); SQLTransact(NULL, dbc, SQL_ROLLBACK); SQLFreeStmt(s, SQL_DROP);}#ifdef _WIN32static unsigned __stdcall runClientThread(void *args)#elsestatic int runClientThread(void *args)#endif{ SQLHENV env; SQLHDBC dbc; SQLRETURN rc; int ntrans = n_txn_per_client; rc = SQLAllocEnv(&env); if (!SQL_SUCCEEDED(rc)) { fprintf(stderr, "AllocEnv failed\n"); return 1; } rc = SQLAllocConnect(env, &dbc); if (!SQL_SUCCEEDED(rc)) { fprintf(stderr, "AllocConnect failed\n"); return 1; } rc = SQLDriverConnect(dbc, NULL, (SQLCHAR *) dsn, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE | SQL_DRIVER_NOPROMPT); if (!SQL_SUCCEEDED(rc)) { fprintf(stderr, "DriverConnect failed\n"); return 1; } if (transactions) { rc = SQLSetConnectOption(dbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF); if (!SQL_SUCCEEDED(rc)) { fprintf(stderr, "SetConnectOption(autocommit=off) failed\n"); return 1; } } while (ntrans-- > 0) { int account = getRandomID(ACCOUNT); int branch = getRandomID(BRANCH); int teller = getRandomID(TELLER); int delta = getRandomInt(0,1000); doOne(dbc, branch, teller, account, delta); if (transactions < -1 && ntrans > 0 && ntrans % 100 == 0) { rc = SQLTransact(NULL, dbc, SQL_COMMIT); if (!SQL_SUCCEEDED(rc)) { if (verbose) { fprintf(stderr, "runClientThread: COMMIT failed rc=%d\n", rc); } incrementFailedTransactionCount(); SQLTransact(NULL, dbc, SQL_ROLLBACK); } } incrementTransactionCount(); } if (transactions < 0) { rc = SQLTransact(NULL, dbc, SQL_COMMIT); if (!SQL_SUCCEEDED(rc)) { if (verbose) { fprintf(stderr, "runClientThread: final COMMIT failed rc=%d\n", rc); } incrementFailedTransactionCount(); SQLTransact(NULL, dbc, SQL_ROLLBACK); } } SQLDisconnect(dbc); SQLFreeConnect(dbc); SQLFreeEnv(env); return 0;}int main(int argc, char **argv){ int init_db = 0, i;#ifdef _WIN32 HANDLE *pids;#else pid_t *pids;#endif for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-clients") == 0) { if (i + 1 < argc) { i++; n_clients = strtol(argv[i], NULL, 0); } } else if (strcmp(argv[i], "-dsn") == 0) { if (i + 1 < argc) { i++; dsn = argv[i]; } } else if (strcmp(argv[i], "-tpc") == 0) { if (i + 1 < argc) { i++; n_txn_per_client = strtol(argv[i], NULL, 0); } } else if (strcmp(argv[i], "-init") == 0) { init_db = 1; } else if (strcmp(argv[i], "-tps") == 0) { if (i + 1 < argc) { i++; tps = strtol(argv[i], NULL, 0); } } else if (strcmp(argv[i], "-v") == 0) { verbose++; } } if (dsn == NULL) { fprintf(stderr, "usage: %s -dsn DSN [-v] [-init] " "[-tpc n] [-clients]\n\n", argv[0]); fprintf(stderr, "-v verbose error messages\n"); fprintf(stderr, "-init initialize the tables\n"); fprintf(stderr, "-tpc transactions per client\n"); fprintf(stderr, "-clients number of simultaneous clients\n"); exit(1); } fprintf(stdout, "Scale factor value: %d\n", tps); fprintf(stdout, "Number of clients: %d\n", n_clients); fprintf(stdout, "Number of transactions per client: %d\n\n", n_txn_per_client); fflush(stdout); if (init_db) { fprintf(stdout, "Initializing dataset...\n"); createDatabase(); fprintf(stdout, "done.\n\n"); fflush(stdout); }#ifndef _WIN32 shmid = shmget(IPC_PRIVATE, 2 * sizeof (int), IPC_CREAT | 0666); shm = shmat(shmid, NULL, 0);#endif transaction_count = &shm[0]; failed_transactions = &shm[1]; *transaction_count = 0; *failed_transactions = 0;#ifdef _WIN32 pids = malloc(n_clients * sizeof (HANDLE));#else pids = malloc(n_clients * sizeof (pid_t));#endif if (pids == NULL) { fprintf(stderr, "malloc failed\n"); exit(2); } fprintf(stdout, "Starting Benchmark Run\n"); transactions = 0;#ifdef _WIN32 start_time = GetTickCount();#else gettimeofday(&start_time, NULL);#endif if (n_clients < 2) { runClientThread(NULL); } else {#ifdef _WIN32 for (i = 0; i < n_clients; i++) { unsigned tid; pids[i] = (HANDLE) _beginthreadex(NULL, 0,runClientThread, NULL, 0, &tid); } for (i = 0; i < n_clients; i++) { WaitForSingleObject(pids[i], INFINITE); CloseHandle(pids[i]); }#else for (i = 0; i < n_clients; i++) { pid_t child = fork(); switch (child) { case 0: return runClientThread(NULL); default: pids[i] = child; } } for (i = 0; i < n_clients; i++) { int status; waitpid(pids[i], &status, 0); }#endif } reportDone(); transactions = 1;#ifdef _WIN32 start_time = GetTickCount();#else gettimeofday(&start_time, NULL);#endif if (n_clients < 2) { runClientThread(NULL); } else {#ifdef _WIN32 for (i = 0; i < n_clients; i++) { unsigned tid; pids[i] = (HANDLE) _beginthreadex(NULL, 0,runClientThread, NULL, 0, &tid); } for (i = 0; i < n_clients; i++) { WaitForSingleObject(pids[i], INFINITE); CloseHandle(pids[i]); }#else for (i = 0; i < n_clients; i++) { pid_t child = fork(); switch (child) { case 0: return runClientThread(NULL); default: pids[i] = child; } } for (i = 0; i < n_clients; i++) { int status; waitpid(pids[i], &status, 0); }#endif } reportDone(); transactions = -1;#ifdef _WIN32 start_time = GetTickCount();#else gettimeofday(&start_time, NULL);#endif if (n_clients < 2) { runClientThread(NULL); } else {#ifdef _WIN32 for (i = 0; i < n_clients; i++) { unsigned tid; pids[i] = (HANDLE) _beginthreadex(NULL, 0,runClientThread, NULL, 0, &tid); } for (i = 0; i < n_clients; i++) { WaitForSingleObject(pids[i], INFINITE); CloseHandle(pids[i]); }#else for (i = 0; i < n_clients; i++) { pid_t child = fork(); switch (child) { case 0: return runClientThread(NULL); default: pids[i] = child; } } for (i = 0; i < n_clients; i++) { int status; waitpid(pids[i], &status, 0); }#endif } reportDone(); transactions = -2;#ifdef _WIN32 start_time = GetTickCount();#else gettimeofday(&start_time, NULL);#endif if (n_clients < 2) { runClientThread(NULL); } else {#ifdef _WIN32 for (i = 0; i < n_clients; i++) { unsigned tid; pids[i] = (HANDLE) _beginthreadex(NULL, 0,runClientThread, NULL, 0, &tid); } for (i = 0; i < n_clients; i++) { WaitForSingleObject(pids[i], INFINITE); CloseHandle(pids[i]); }#else for (i = 0; i < n_clients; i++) { pid_t child = fork(); switch (child) { case 0: return runClientThread(NULL); default: pids[i] = child; } } for (i = 0; i < n_clients; i++) { int status; waitpid(pids[i], &status, 0); }#endif } reportDone(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -