csql 2.7 release
[csql.git] / demo / wisc / odbcBench.c
blob62f48c6f966f847192805cde87d4d8c4a0fc6bc1
2 #include <iostream>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
7 #include <sql.h>
8 #include <sqlext.h>
9 #include <NanoTimer.h>
10 using namespace std;
12 NanoTimer timer;
13 long long int timevalues[30]={0};
15 void extract_error(
16 char *fn,
17 SQLHANDLE handle,
18 SQLSMALLINT type)
20 SQLINTEGER i = 0;
21 SQLINTEGER native;
22 SQLCHAR state[ 7 ];
23 SQLCHAR text[256];
24 SQLSMALLINT len;
25 SQLRETURN ret;
27 fprintf(stderr,
28 "\n"
29 "The driver reported the following diagnostics whilst running "
30 "%s\n\n",
31 fn);
35 ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
36 sizeof(text), &len );
37 if (SQL_SUCCEEDED(ret))
38 printf("%s:%ld:%ld:%s\n", state, i, native, text);
40 while( ret == SQL_SUCCESS );
43 inline void
44 check_error (SQLSMALLINT handleType, SQLHANDLE handle, int rc, int line)
46 if (rc)
48 cout << "Error " << rc << " at line: " << line << endl;
49 SQLCHAR state[10];
50 SQLINTEGER native;
51 SQLCHAR mesg[300];
52 int i=0;
53 while (SQLGetDiagRec (handleType, handle, i++, state, &native, mesg, 300,
54 NULL) == SQL_SUCCESS)
56 mesg[299] = '\0';
57 cout << "state: " << state << endl;
58 cout << "native: " << native << endl;
59 cout << "mesg: " << mesg << endl;
61 exit (1);
66 inline void checkrc (int rc, int line)
68 if (rc)
70 printf("Error %d at line: %d\n", rc, line);
71 exit (1);
75 void readWriteTrans (SQLHDBC hdbc)
77 int rc = SQLSetConnectAttr (hdbc, SQL_ATTR_ACCESS_MODE,
78 (SQLPOINTER) SQL_MODE_READ_WRITE, 0);
79 checkrc (rc, __LINE__);
80 rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
81 checkrc (rc, __LINE__);
84 void readOnlyTrans (SQLHDBC hdbc)
86 int rc = SQLSetConnectAttr (hdbc, SQL_ATTR_ACCESS_MODE,
87 (SQLPOINTER) SQL_MODE_READ_ONLY, 0);
88 checkrc (rc, __LINE__);
89 rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
90 checkrc (rc, __LINE__);
93 void createHashIndex(SQLHANDLE hstmt,bool flag)
95 int rc;
96 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx1 ON big1(unique1) HASH;", SQL_NTS );
97 checkrc (rc, __LINE__);
98 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx2 ON big2(unique1) HASH;", SQL_NTS );
99 checkrc (rc, __LINE__);
100 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx3 ON small(unique1) HASH", SQL_NTS );
101 checkrc (rc, __LINE__);
102 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx4 ON big1(stringu1) HASH", SQL_NTS );
103 checkrc (rc, __LINE__);
104 return;
106 void createTreeIndex(SQLHANDLE hstmt,bool flag)
108 int rc;
109 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx5 ON big1(unique1) TREE;", SQL_NTS );
110 checkrc (rc, __LINE__);
111 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx6 ON big2(unique1) TREE;", SQL_NTS );
112 checkrc (rc, __LINE__);
113 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx7 ON small(unique1) TREE;", SQL_NTS );
114 checkrc (rc, __LINE__);
115 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE INDEX idx8 ON big1(stringu1) TREE;", SQL_NTS );
116 checkrc (rc, __LINE__);
117 return;
119 void dropHashIndex(SQLHANDLE hstmt,bool flag)
121 int rc;
122 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx1 ;", SQL_NTS );
123 checkrc (rc, __LINE__);
124 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx2 ;", SQL_NTS );
125 checkrc (rc, __LINE__);
126 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx3 ;", SQL_NTS );
127 checkrc (rc, __LINE__);
128 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx4 ;", SQL_NTS );
129 checkrc (rc, __LINE__);
130 return;
132 void dropTreeIndex(SQLHANDLE hstmt,bool flag)
134 int rc;
135 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx5 ;", SQL_NTS );
136 checkrc (rc, __LINE__);
137 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx6 ;", SQL_NTS );
138 checkrc (rc, __LINE__);
139 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx7 ;", SQL_NTS );
140 checkrc (rc, __LINE__);
141 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP INDEX idx8 ;", SQL_NTS );
142 checkrc (rc, __LINE__);
143 return;
146 long long int onePerSel(SQLHANDLE henv, SQLHANDLE hdbc, SQLHANDLE hstmt)
148 int uni1;
149 int uni2;
150 char str1[200];
151 char str2[200];
152 int val[] = {1, 5, 10, 50, 100, 500, 1000, 5000 ,7500, 9900};;
153 int tempTermVar=0;
154 int tempTermVar2=0;
155 char tempval[124]="Value";
156 int rc;
157 rc = SQLPrepare (hstmt, (unsigned char *) "SELECT unique1, unique2, stringu1 from big1 where unique1 between ? and ?;", SQL_NTS);
158 checkrc (rc, __LINE__);
159 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &tempTermVar, 0, NULL);
160 checkrc (rc, __LINE__);
161 rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &tempTermVar2, 0, NULL);
162 checkrc (rc, __LINE__);
164 rc = SQLBindCol (hstmt, 1, SQL_INTEGER, &uni1, 0, NULL);
165 checkrc (rc, __LINE__);
166 rc = SQLBindCol (hstmt, 2, SQL_INTEGER, &uni2, 0, NULL);
167 checkrc (rc, __LINE__);
168 rc = SQLBindCol (hstmt, 3, SQL_C_CHAR, str1, sizeof (str1), NULL);
169 checkrc (rc, __LINE__);
171 int i, j, k;
172 int Count;
174 timer.reset ();
176 for (i = 0; i < 10; i++)
178 Count=0;
179 strcpy(tempval,"Value");
180 tempTermVar = val[i] ;
181 tempTermVar2 = val[i]+99 ;
182 timer.start ();
183 rc = SQLExecute (hstmt);
184 checkrc (rc, __LINE__);
185 while(SQL_SUCCEEDED(rc = SQLFetch(hstmt)))
187 Count++;
189 // printf("Count=%d\n",Count);
190 rc = SQLCloseCursor (hstmt);
191 checkrc (rc, __LINE__);
192 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
193 checkrc (rc, __LINE__);
194 timer.stop ();
196 return timer.avg()/1000;
199 long long int runReadTest (SQLHANDLE henv, SQLHANDLE hdbc,SQLHANDLE hstmt,bool flag)
201 int uni1;
202 int uni2;
203 char str1[200];
204 char str2[200];
206 int val[] = {1, 10, 100, 500, 1000, 5000 ,7500, 8000, 9000, 9999};
207 int tempTermVar=0;
208 char tempval[124]="Value";
209 int rc;
211 if(flag){
212 rc = SQLPrepare (hstmt, (unsigned char *) "SELECT unique1, unique2, stringu1, stringu2 from big1 where unique1=?;", SQL_NTS);
214 else {
215 rc = SQLPrepare (hstmt, (unsigned char *) "SELECT unique1, unique2, stringu1, stringu2 from big1 where stringu1=?;", SQL_NTS);
217 checkrc (rc, __LINE__);
218 long sz = 0;
219 if(flag){
220 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &tempTermVar, 0, NULL);
221 checkrc (rc, __LINE__);
223 else{
224 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_C_CHAR, 196, 0, tempval, 0, NULL);
225 checkrc (rc, __LINE__);
228 rc = SQLBindCol (hstmt, 1, SQL_INTEGER, &uni1, 0, NULL);
229 checkrc (rc, __LINE__);
230 rc = SQLBindCol (hstmt, 2, SQL_INTEGER, &uni2, 0, NULL);
231 checkrc (rc, __LINE__);
232 rc = SQLBindCol (hstmt, 3, SQL_C_CHAR, str1, sizeof (str1), NULL);
233 checkrc (rc, __LINE__);
234 rc = SQLBindCol (hstmt, 4, SQL_C_CHAR, str2, sizeof (str2), NULL);
235 checkrc (rc, __LINE__);
237 int i, j, k;
238 int Count;
240 timer.reset ();
242 for (i = 0; i < 10; i++)
244 strcpy(tempval,"Value");
245 if(flag){
246 tempTermVar = val[i] ;}
247 else{
248 char buf[10];
249 sprintf(buf,"%d",val[i]);
250 strcat(tempval,buf);
252 timer.start ();
253 rc = SQLExecute (hstmt);
254 checkrc (rc, __LINE__);
255 rc = SQLFetch (hstmt);
256 checkrc (rc, __LINE__);
257 Count++;
258 rc = SQLCloseCursor (hstmt);
259 checkrc (rc, __LINE__);
260 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
261 checkrc (rc, __LINE__);
262 timer.stop ();
264 return timer.avg()/1000;
268 //aggregate
269 long long int aggregate(SQLHANDLE henv, SQLHANDLE hdbc,SQLHANDLE hstmt,bool flag)
271 int summin;
272 int rc;
273 if(flag){
274 rc = SQLPrepare (hstmt, (unsigned char *) "SELECT MIN(unique1) from big1;", SQL_NTS);
276 else {
277 rc = SQLPrepare (hstmt, (unsigned char *) "SELECT SUM(unique1) from big1;", SQL_NTS);
279 checkrc (rc, __LINE__);
280 rc = SQLBindCol (hstmt, 1, SQL_INTEGER, &summin, 0, NULL);
281 checkrc (rc, __LINE__);
283 int i, j, k;
284 int Count;
286 timer.reset ();
287 for (i = 0; i < 10; i++)
289 timer.start ();
290 rc = SQLExecute (hstmt);
291 checkrc (rc, __LINE__);
292 rc = SQLFetch (hstmt);
293 checkrc (rc, __LINE__);
294 Count++;
295 // printf("value=%d",summin);
296 rc = SQLCloseCursor (hstmt);
297 checkrc (rc, __LINE__);
298 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
299 checkrc (rc, __LINE__);
300 timer.stop ();
302 return timer.avg()/1000;
306 long long int dmlstatement(SQLHANDLE henv, SQLHANDLE hdbc,SQLHANDLE hstmt,int val)
308 int tempTermVar=10000,tempTermVar2=10000;
309 int rc;
310 if(val==1){
311 rc = SQLPrepare (hstmt, (unsigned char *) "insert into big1 values(?,?,0,2,0,10,50,688,1950,4950,9950,1,100,'MXXXXXXXXXXXXXXXXXXXXXXXXXGXXXXXXXXXXXXXXXXXXXXXXXXC','GXXXXXXXXXXXXXXXXXXXXXXXXXCXXXXXXXXXXXXXXXXXXXXXXXXA','OXXXXXXXXXXXXXXXXXXXXXXXXXOXXXXXXXXXXXXXXXXXXXXXXXXO');", SQL_NTS);
313 else if(val==2){
314 rc = SQLPrepare (hstmt, (unsigned char *) "UPDATE big1 SET two=1 WHERE unique1=?;", SQL_NTS);
315 }else
317 rc = SQLPrepare (hstmt, (unsigned char *) "DELETE FROM big1 WHERE unique1=?;", SQL_NTS);
319 checkrc (rc, __LINE__);
321 int i, j, k;
322 int Count;
324 timer.reset ();
325 for (i = 0; i < 100; i++)
326 { tempTermVar+=i;
327 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &tempTermVar, 0, NULL);
328 checkrc (rc, __LINE__);
329 if(val==1)
331 tempTermVar2+=i;
332 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &tempTermVar2, 0, NULL);
333 checkrc (rc, __LINE__);
335 timer.start ();
336 rc = SQLExecute (hstmt);
337 checkrc (rc, __LINE__);
338 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
339 checkrc (rc, __LINE__);
340 timer.stop ();
341 tempTermVar=10000;tempTermVar2=10000;
343 return timer.avg()/1000;
347 //Joining
348 long long int joining(SQLHANDLE henv, SQLHANDLE hdbc, SQLHANDLE hstmt,int flag)
350 int uni1;
351 int uni2,uni3,uni4,uni5;
352 char str1[200];
353 char str2[200];
355 int val[] = {1, 5, 10, 50, 100, 250, 500, 750, 900, 999};
356 int tempTermVar=0;
357 char tempval[124]="Value";
358 int rc;
360 if(flag==1){
361 rc = SQLPrepare (hstmt, (unsigned char *) "SELECT big1.unique1, big1.unique2, small.unique1, small.stringu1 FROM small,big1 WHERE big1.unique1=small.unique1 AND small.unique1=?;", SQL_NTS);
363 else {
364 rc = SQLPrepare (hstmt, (unsigned char *) "SELECT small.unique1, big1.unique1, big2.unique1, small.stringu1, big1.unique2, big2.unique2 FROM big1, big2, small WHERE small.unique1=big1.unique1 AND big1.unique1=big2.unique1 AND big1.unique1 = ?;", SQL_NTS);
366 checkrc (rc, __LINE__);
367 long sz = 0;
368 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &tempTermVar, 0, NULL);
369 checkrc (rc, __LINE__);
370 rc = SQLBindCol (hstmt, 1, SQL_INTEGER, &uni1, 0, NULL);
371 checkrc (rc, __LINE__);
372 rc = SQLBindCol (hstmt, 2, SQL_INTEGER, &uni2, 0, NULL);
373 checkrc (rc, __LINE__);
374 rc = SQLBindCol (hstmt, 2, SQL_INTEGER, &uni3, 0, NULL);
375 checkrc (rc, __LINE__);
376 rc = SQLBindCol (hstmt, 4, SQL_C_CHAR, str1, sizeof (str1), NULL);
377 checkrc (rc, __LINE__);
378 if(flag==2){
379 rc = SQLBindCol (hstmt, 5, SQL_INTEGER, &uni4, 0, NULL);
380 checkrc (rc, __LINE__);
381 rc = SQLBindCol (hstmt, 6, SQL_INTEGER, &uni5, 0, NULL);
382 checkrc (rc, __LINE__);
384 int i, j, k;
385 int Count;
387 timer.reset ();
389 for (i = 0; i < 10; i++)
391 tempTermVar = val[i] ;
392 timer.start ();
393 rc = SQLExecute (hstmt);
394 checkrc (rc, __LINE__);
395 rc = SQLFetch(hstmt);
396 Count++;
397 // printf("%d,%d\n",uni1,uni3);
398 rc = SQLCloseCursor (hstmt);
399 checkrc (rc, __LINE__);
400 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
401 checkrc (rc, __LINE__);
402 timer.stop ();
404 return timer.avg()/1000;
408 void printTime()
410 printf("Wisconsin Benchmark Report:\n");
411 printf("*******************************************************\n");
412 printf(" Statement \t NoIndex Hash\t Tree\n");
413 printf("*******************************************************\n");
414 printf(" SelectInt 1 \t %lld\t %lld \t %lld\n",timevalues[0],timevalues[1],timevalues[2]);
415 printf(" SelectStr 1 \t %lld\t %lld \t %lld\n",timevalues[3],timevalues[4],timevalues[5]);
416 printf(" 1Per Sel \t %lld\t - \t %lld\n\n",timevalues[6],timevalues[8]);
417 printf(" Min All \t %lld\t - \t %lld\n",timevalues[9],timevalues[11]);
418 printf(" Sum All \t %lld\t - \t - \n\n",timevalues[12]);
419 printf(" Insert 1 \t %lld\t %lld \t %lld\n",timevalues[15],timevalues[16],timevalues[17]);
420 printf(" Update 1 \t %lld\t %lld \t %lld\n",timevalues[18],timevalues[19],timevalues[20]);
421 printf(" Delete 1 \t %lld\t %lld \t %lld\n\n",timevalues[21],timevalues[22],timevalues[23]);
422 printf(" Join(10K*1K)1 \t %lld\t %lld \t %lld\n",timevalues[24],timevalues[25],timevalues[26]);
423 printf(" Join(10K*10K*1K)1 \t %lld\t %lld \t %lld\n",timevalues[27],timevalues[28],timevalues[29]);
424 printf("*******************************************************\n");
428 int main (int ac, char **av)
430 int rc;
431 SQLHENV henv;
432 SQLHDBC hdbc;
433 SQLHSTMT hstmt;
434 rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
435 checkrc (rc, __LINE__);
436 SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
438 rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
439 checkrc (rc, __LINE__);
440 rc = SQLConnect (hdbc,
441 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
442 (SQLCHAR *) "root",
443 (SQLSMALLINT) strlen ("root"),
444 (SQLCHAR *) "manager",
445 (SQLSMALLINT) strlen (""));
447 if (SQL_SUCCEEDED(rc)) {
448 printf("Connected\n");
450 checkrc (rc, __LINE__);
451 rc = SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
452 checkrc (rc, __LINE__);
453 rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
454 checkrc (rc, __LINE__);
455 int ins=1,upd=2,del=3;
456 //Query 1
457 timevalues[0] = runReadTest (henv, hdbc, hstmt,true);
458 timevalues[3] = runReadTest (henv, hdbc, hstmt,false);
459 timevalues[6] = onePerSel(henv, hdbc, hstmt);
460 timevalues[9] = aggregate(henv, hdbc, hstmt,true);
461 timevalues[12] = aggregate(henv, hdbc, hstmt,false);
462 timevalues[15] = dmlstatement(henv, hdbc, hstmt,ins);
463 timevalues[18] = dmlstatement(henv, hdbc, hstmt,upd);
464 timevalues[21] = dmlstatement(henv, hdbc, hstmt,del);
465 timevalues[24] = joining(henv, hdbc, hstmt,1);
466 timevalues[27] = joining(henv, hdbc, hstmt,2);
467 printf("NON INDEX OVER\n");
469 //Hash
470 createHashIndex( hstmt,true);
471 timevalues[1] = runReadTest (henv, hdbc, hstmt,true);
472 timevalues[4] = runReadTest (henv, hdbc, hstmt,false);
473 timevalues[7] = 0;
474 timevalues[10] = 0;
475 timevalues[13] = 0;
476 timevalues[16] = dmlstatement(henv, hdbc, hstmt,ins);
477 timevalues[19] = dmlstatement(henv, hdbc, hstmt,upd);
478 timevalues[22] = dmlstatement(henv, hdbc, hstmt,del);
479 timevalues[25] = joining(henv, hdbc, hstmt,1);
480 timevalues[28] = joining(henv, hdbc, hstmt,2);
481 dropHashIndex(hstmt,true);
482 printf("HASH INDEX OVER\n");
483 //printTime();
485 //TREE
486 createTreeIndex(hstmt,true);
487 timevalues[2] = runReadTest (henv, hdbc, hstmt,true);
488 timevalues[5] = runReadTest (henv, hdbc, hstmt,false);
489 timevalues[8] = onePerSel(henv, hdbc, hstmt);
490 timevalues[11] = aggregate(henv, hdbc, hstmt,true);
491 timevalues[14] = 0;
492 timevalues[17] = dmlstatement(henv, hdbc, hstmt,ins);
493 timevalues[20] = dmlstatement(henv, hdbc, hstmt,upd);
494 timevalues[23] = dmlstatement(henv, hdbc, hstmt,del);
495 timevalues[26] = joining(henv, hdbc,hstmt,1);
496 timevalues[29] = joining(henv, hdbc,hstmt,2);
497 dropTreeIndex(hstmt,true);
498 printf("TREE INDEX OVER\n");
500 printTime();
501 rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT );
502 checkrc (rc, __LINE__);
504 rc = SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
505 checkrc (rc, __LINE__);
506 rc = SQLDisconnect (hdbc);
507 checkrc (rc, __LINE__);
508 rc = SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
509 checkrc (rc, __LINE__);
510 rc = SQLFreeHandle (SQL_HANDLE_ENV, henv);
511 checkrc (rc, __LINE__);
512 return 0;