adding test scripts
[csql.git] / test / performance / ODBCTest.c
blob709a56b6b91ffa9c40a671527e443d5075b6ac4b
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 #define ITERATION 100
11 using namespace std;
13 #define DSN
15 NanoTimer timer;
17 void extract_error(
18 char *fn,
19 SQLHANDLE handle,
20 SQLSMALLINT type)
22 SQLINTEGER i = 0;
23 SQLINTEGER native;
24 SQLCHAR state[ 7 ];
25 SQLCHAR text[256];
26 SQLSMALLINT len;
27 SQLRETURN ret;
29 fprintf(stderr,
30 "\n"
31 "The driver reported the following diagnostics whilst running "
32 "%s\n\n",
33 fn);
37 ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
38 sizeof(text), &len );
39 if (SQL_SUCCEEDED(ret))
40 printf("%s:%ld:%ld:%s\n", state, i, native, text);
42 while( ret == SQL_SUCCESS );
45 inline void
46 check_error (SQLSMALLINT handleType, SQLHANDLE handle, int rc, int line)
48 if (rc)
50 cout << "Error " << rc << " at line: " << line << endl;
51 SQLCHAR state[10];
52 SQLINTEGER native;
53 SQLCHAR mesg[300];
54 int i=0;
55 while (SQLGetDiagRec (handleType, handle, i++, state, &native, mesg, 300,
56 NULL) == SQL_SUCCESS)
58 mesg[299] = '\0';
59 cout << "state: " << state << endl;
60 cout << "native: " << native << endl;
61 cout << "mesg: " << mesg << endl;
63 exit (1);
68 inline void
69 checkrc (int rc, int line)
71 if (rc)
73 cout << "Error " << rc << " at line: " << line << endl;
74 exit (1);
78 void
79 readWriteTrans (SQLHDBC hdbc)
81 int rc = SQLSetConnectAttr (hdbc, SQL_ATTR_ACCESS_MODE,
82 (SQLPOINTER) SQL_MODE_READ_WRITE, 0);
83 checkrc (rc, __LINE__);
84 rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
85 checkrc (rc, __LINE__);
88 void
89 readOnlyTrans (SQLHDBC hdbc)
91 int rc = SQLSetConnectAttr (hdbc, SQL_ATTR_ACCESS_MODE,
92 (SQLPOINTER) SQL_MODE_READ_ONLY, 0);
93 checkrc (rc, __LINE__);
94 rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
95 checkrc (rc, __LINE__);
98 int
99 runInsertTest (SQLHANDLE henv, SQLHANDLE hdbc, SQLHANDLE hstmt)
101 int rc;
102 int iHolder;
103 char sHolder[200];
104 char sData[200];
106 iHolder = 0;
107 strcpy (sData, "0123456789012345678901234567890123456789");
108 rc = SQLPrepare (hstmt, (unsigned char *) "INSERT INTO t1 VALUES (?, ?)",
109 SQL_NTS);
110 checkrc (rc, __LINE__);
111 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
112 0, 0, &iHolder, 0, NULL);
113 checkrc (rc, __LINE__);
114 SQLINTEGER sLen = SQL_NTS;
115 rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
116 196, 0, (void *) sHolder, 0, &sLen);
117 checkrc (rc, __LINE__);
118 int i, j, k;
119 int tCount;
121 timer.reset ();
123 /* Run 1per Test */
124 for (i = 0; i < ITERATION; i++)
126 timer.start ();
127 iHolder = i ;
128 strcpy (sHolder, sData);
129 rc = SQLExecute (hstmt);
130 checkrc (rc, __LINE__);
131 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
132 checkrc (rc, __LINE__);
133 timer.stop ();
136 printf ("Insert: 1 %lld %lld %lld\n", timer.min (), timer.max (),
137 timer.avg ());
139 return 0;
144 runReadTest (SQLHANDLE henv, SQLHANDLE hdbc, SQLHANDLE hstmt)
146 int iHolder;
147 char sHolder[200];
148 char sData[200];
150 int tempTermVar = 0;
151 int tempTermVal = 0;
152 int rc;
154 /* Set read-only transaction type */
155 readOnlyTrans (hdbc);
156 rc = SQLPrepare (hstmt, (unsigned char *)
157 "SELECT f1, f2 FROM t1 where f1 = ?", SQL_NTS);
158 checkrc (rc, __LINE__);
159 long sz = 0;
160 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
161 0, 0, &tempTermVar, 0, NULL);
162 checkrc (rc, __LINE__);
163 rc = SQLBindCol (hstmt, 1, SQL_INTEGER, &iHolder, 0, NULL);
164 checkrc (rc, __LINE__);
165 rc = SQLBindCol (hstmt, 2, SQL_C_CHAR, sHolder, sizeof (sHolder), NULL);
166 checkrc (rc, __LINE__);
167 iHolder = 0;
168 strcpy (sData, "0123456789012345678901234567890123456789");
170 int i, j, k;
171 int tCount;
173 timer.reset ();
175 /* Run 1per Test */
176 for (i = 0; i < ITERATION; i++)
178 timer.start ();
179 tempTermVar = i ;
180 rc = SQLExecute (hstmt);
181 checkrc (rc, __LINE__);
182 rc = SQLFetch (hstmt);
183 checkrc (rc, __LINE__);
184 tempTermVal = iHolder;
185 strcpy (sData, sHolder);
186 rc = SQLCloseCursor (hstmt);
187 checkrc (rc, __LINE__);
188 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
189 checkrc (rc, __LINE__);
190 timer.stop ();
192 printf ("Read: 1 %lld %lld %lld\n", timer.min (), timer.max (),
193 timer.avg ());
194 /* Set read-write transaction type */
195 readWriteTrans (hdbc);
196 return rc;
200 runUpdateTest (SQLHANDLE henv, SQLHANDLE hdbc, SQLHANDLE hstmt)
202 int rc;
203 int iHolder;
204 char sHolder[200];
205 char sData[200];
207 rc = SQLPrepare (hstmt,
208 (unsigned char *) "UPDATE t1 SET f2 = ? WHERE f1 = ?",
209 SQL_NTS);
210 checkrc (rc, __LINE__);
211 SQLINTEGER sLen = SQL_NTS;
212 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
213 196, 0, (void *) sHolder, 0, &sLen);
214 checkrc (rc, __LINE__);
215 rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
216 0, 0, &iHolder, 0, NULL);
217 checkrc (rc, __LINE__);
218 iHolder = 0;
219 strcpy (sData, "9876543210987654321098765432109876543210");
221 int i, j, k;
222 int tCount;
224 timer.reset ();
226 /* Run 1per Test */
227 for (i = 0; i < ITERATION; i++)
229 timer.start ();
230 iHolder = i ;
231 strcpy (sHolder, sData);
232 rc = SQLExecute (hstmt);
233 checkrc (rc, __LINE__);
234 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
235 checkrc (rc, __LINE__);
236 timer.stop ();
238 printf ("Update: 1 %lld %lld %lld\n", timer.min (), timer.max (),
239 timer.avg ());
241 return 0;
245 runDeleteTest (SQLHANDLE henv, SQLHANDLE hdbc, SQLHANDLE hstmt)
247 int rc;
248 int iHolder;
250 rc = SQLPrepare (hstmt, (unsigned char *)
251 "DELETE FROM t1 WHERE f1 = ?", SQL_NTS);
252 checkrc (rc, __LINE__);
253 rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
254 0, 0, &iHolder, 0, NULL);
255 checkrc (rc, __LINE__);
256 iHolder = 0;
257 int i, j, k;
258 int tCount;
259 timer.reset ();
260 /* Run 1per Test */
261 for (i = 0; i < ITERATION; i++)
263 timer.start ();
264 iHolder = i ;
265 rc = SQLExecute (hstmt);
266 checkrc (rc, __LINE__);
267 rc = SQLTransact (henv, hdbc, SQL_COMMIT);
268 checkrc (rc, __LINE__);
269 timer.stop ();
271 printf ("Delete: 1 %lld %lld %lld\n", timer.min (), timer.max (),
272 timer.avg ());
273 return 0;
278 main (int ac, char **av)
280 int rc;
281 char *dsn;
282 //SQLHANDLE henv, hdbc, hstmt;
283 SQLHENV henv;
284 SQLHDBC hdbc;
285 SQLHSTMT hstmt;
287 dsn = (char *) "DSN=test;";
288 //dsn = (char *)"DSN=mycsql;MODE=CSQL;SERVER=192.168.1.103;PORT=5678;";
290 rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
291 checkrc (rc, __LINE__);
292 SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
294 rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
295 checkrc (rc, __LINE__);
296 rc = SQLConnect (hdbc,
297 (SQLCHAR *) dsn, SQL_NTS,
298 (SQLCHAR *) "root",
299 (SQLSMALLINT) strlen ("root"),
300 (SQLCHAR *) "manager",
301 (SQLSMALLINT) strlen (""));
303 SQLCHAR outstr[1024];
304 SQLSMALLINT outstrlen;
307 rc = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)"DSN=myodbc3;", SQL_NTS,
308 outstr, sizeof(outstr), &outstrlen,
309 SQL_DRIVER_NOPROMPT);
312 if (SQL_SUCCEEDED(rc)) {
313 printf("Connected\n");
314 //printf("Returned connection string was:\n\t%s\n", outstr);
315 if (rc == SQL_SUCCESS_WITH_INFO) {
316 printf("Driver reported the following diagnostics\n");
317 extract_error("SQLDriverConnect", hdbc, SQL_HANDLE_DBC);
319 } else {
320 fprintf(stderr, "Failed to connect\n");
321 extract_error("SQLDriverConnect", hdbc, SQL_HANDLE_DBC);
324 //check_error (SQL_HANDLE_DBC, hdbc, rc, __LINE__);
325 checkrc (rc, __LINE__);
326 rc = SQLSetConnectOption (hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
327 checkrc (rc, __LINE__);
328 rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
329 checkrc (rc, __LINE__);
330 rc = SQLExecDirect(hstmt, (SQLCHAR*) "CREATE TABLE t1 (f1 INTEGER, f2 char(196), primary key (f1));", SQL_NTS );
331 checkrc (rc, __LINE__);
333 runInsertTest (henv, hdbc, hstmt);
334 runReadTest (henv, hdbc, hstmt);
335 runUpdateTest (henv, hdbc, hstmt);
336 runDeleteTest (henv, hdbc, hstmt);
338 rc = SQLExecDirect(hstmt, (SQLCHAR*) "DROP TABLE t1;", SQL_NTS );
339 checkrc (rc, __LINE__);
340 rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT );
341 checkrc (rc, __LINE__);
343 rc = SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
344 checkrc (rc, __LINE__);
345 rc = SQLDisconnect (hdbc);
346 checkrc (rc, __LINE__);
347 rc = SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
348 checkrc (rc, __LINE__);
349 rc = SQLFreeHandle (SQL_HANDLE_ENV, henv);
350 checkrc (rc, __LINE__);
351 exit (0);