/* my_tpcb_nsp tpc-B SQL v. 1.0.0 Programma per effettuare la transazione standard (TPC-B) Il suo utilizzo consente di effettuare prove parametriche sull'utilizzo di MySQL in parallelo da parte di piu' utenti. Copyright (c) mail@meo.bogliolo.name */ #include /* #include */ #include "mysql.h" int Bid; int Tid; int Aid; int delta; double Abalance; char username[40]; char passwd[40]; char hostname[40]; char query[256]; MYSQL m; MYSQL_RES *results; MYSQL_ROW record; int i; int ntimes; int sleeptime; int logging; int scale; main(argc, argv) int argc; char **argv; { if (argc != 7) { if (argc != 8) { printf("TPC-B for MySQL database: version 1.0.1 - SQL\n"); printf("Usage: mysubmit user passwd hostname sleeptime times log [scale] \n"); exit(2); } } strcpy(username, argv[1]); strcpy(passwd, argv[2]); strcpy(hostname, argv[3]); sscanf(argv[4], "%d", &sleeptime); sscanf(argv[5], "%d", &ntimes); sscanf(argv[6], "%d", &logging); if (argc == 7) { scale=1; } else { sscanf(argv[7], "%d", &scale); } if ( ntimes < 1 ) { printf("Usage: mysubmit user passwd hostname sleeptime times log [scale] \n"); printf("times must be more than 0\n"); exit(2); } if ( sleeptime < 0 ) { printf("Usage: mysubmit user passwd hostname sleeptime times log [scale] \n"); printf("sleeptime must be more or equal than 0\n"); exit(2); } if ( scale < 1 ) { printf("Usage: mysubmit user passwd hostname sleeptime times log [scale] \n"); printf("scale must be more than 0\n"); exit(2); } srand(getpid()); mysql_init(&m); if (!mysql_real_connect(&m, hostname, username, passwd, "bench", 0,NULL,0)) { fprintf(stderr, "Error in connect: %s\n", mysql_error(&m)); exit(1); } mysql_autocommit(&m, 0); if (mysql_query(&m, "set transaction isolation level read committed")) { fprintf(stderr, "Error in set isolation: %s\n", mysql_error(&m)); } while (ntimes--) { sleep(sleeptime); Bid = rand(); Bid = Bid - Bid/scale*scale; Tid = rand(); Tid = Tid - Tid/10*10; Tid = Tid + 10*Bid; Aid = rand(); Aid = Aid - Aid/100000*100000; Aid = Aid + 100000*Bid; delta = rand(); delta = delta - delta/2000000*2000000; delta = delta - 1000000; sprintf(query, "UPDATE ACCOUNTS SET Abalance = Abalance + %d WHERE Aid = %d", delta, Aid); if (mysql_query(&m, query)) { fprintf(stderr, "Error in UPD Acc: %s\n", mysql_error(&m)); } sprintf(query,"SELECT Abalance FROM ACCOUNTS WHERE Aid = %d", Aid); if (mysql_query(&m, query)) { fprintf(stderr, "Error in SEL acc: %s\n", mysql_error(&m)); } results = mysql_store_result(&m); record = mysql_fetch_row(results); mysql_free_result(results); sprintf(query,"UPDATE TELLERS SET Tbalance = Tbalance + %d WHERE Tid = %d", delta, Tid); if (mysql_query(&m, query)) { fprintf(stderr, "Error in UPD tel: %s\n", mysql_error(&m)); } sprintf(query,"UPDATE BRANCHES SET Bbalance = Bbalance + %d WHERE Bid = %d", delta, Bid); if (mysql_query(&m, query)) { fprintf(stderr, "Error in UPD bra: %s\n", mysql_error(&m)); } sprintf(query,"INSERT INTO HISTORY (Tid,Bid,Aid,delta) VALUES (%d, %d, %d, %d);", Tid, Bid, Aid, delta); if (mysql_query(&m, query)) { fprintf(stderr, "Error in UPD his: %s\n", mysql_error(&m)); } mysql_commit(&m); } mysql_close(&m); exit(0); }