exp changes
[csql.git] / test / performance / DMLThreadTest.c
blob120da372ab2fbb7aaebfd7ff662aa92034962c0b
1 #include<CSql.h>
2 #include<NanoTimer.h>
3 #define THREADS 5
4 #define RECORDS 100000
5 #define isoLevel READ_REPEATABLE
6 void* runInsTest(void *p);
7 void* runSelTest(void *p);
8 void* runDelTest(void *p);
9 int main()
12 Connection conn;
13 DbRetVal rv = conn.open("root", "manager");
14 if (rv != OK)
16 printf("Error during connection %d\n", rv);
17 return -1;
19 DatabaseManager *dbMgr = conn.getDatabaseManager();
20 if (dbMgr == NULL) { printf("Auth failed\n"); return -1;}
21 TableDef tabDef;
22 tabDef.addField("f1", typeInt, 0, NULL, true);
23 tabDef.addField("f2", typeString, 16);
24 rv = dbMgr->createTable("t1", tabDef);
25 if (rv != OK) { printf("Table creation failed\n"); return -1; }
26 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
27 strcpy(idxInfo->tableName, "t1");
28 idxInfo->list.append("f1");
29 idxInfo->indType = treeIndex;
30 rv = dbMgr->createIndex("indx1", idxInfo);
31 if (rv != OK) { printf("Index creation failed\n"); return -1; }
32 delete idxInfo;
33 pthread_t thr[THREADS];
34 int message[THREADS];
35 int status;
36 for (int i=0; i <THREADS; i++) {
37 message[i] = i;
38 pthread_create (&thr[i], NULL,
39 &runInsTest, (void *) &message[i]);
41 for (int i=0; i <THREADS; i++) {
42 pthread_join(thr[i], (void**)&status);
44 Table *table = dbMgr->openTable("t1");
45 if (table == NULL) { printf("Unable to open table\n"); return -1; }
46 table->setCondition(NULL);
47 rv = conn.startTransaction();
48 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
49 table->execute();
50 int count=0;
51 void *tuple = NULL;
52 while ((tuple = (char*) table->fetch())) {
53 count++;
55 printf("Tuples found: %d\n", count);
56 table->closeScan();
57 conn.commit();
60 Condition p1;
61 int valTerm = 0;
62 p1.setTerm("f1", OpEquals, &valTerm);
63 table->setCondition(&p1);
64 int icount=0;
65 for(int i = 0; i< THREADS * RECORDS; i++)
67 rv = conn.startTransaction();
68 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
69 valTerm = i;
70 rv = table->execute();
71 tuple = (char*)table->fetch() ;
72 //if (tuple == NULL) printf(" %d ", i); else icount++;
73 if (tuple == NULL) break; else icount++;
74 table->closeScan();
75 conn.commit();
77 printf("\nIndex Tuples found: %d\n", icount);
81 dbMgr->closeTable(table);
83 for (int i=0; i <THREADS; i++) {
84 message[i] = i;
85 pthread_create (&thr[i], NULL,
86 &runSelTest, (void *) &message[i]);
88 for (int i=0; i <THREADS; i++) {
89 pthread_join(thr[i], (void**)&status);
93 for (int i=0; i <THREADS; i++) {
94 message[i] = i;
95 pthread_create (&thr[i], NULL,
96 &runDelTest, (void *) &message[i]);
98 for (int i=0; i <THREADS; i++) {
99 pthread_join(thr[i], (void**)&status);
102 //dbMgr->dropTable("t1");
103 conn.close();
104 return 0;
106 void* runInsTest(void *message)
108 Connection conn;
109 DbRetVal rv = conn.open("root", "manager");
110 if (rv != OK)
112 printf("Error during connection %d\n", rv);
113 return NULL;
115 DatabaseManager *dbMgr = conn.getDatabaseManager();
116 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
117 Table *table = dbMgr->openTable("t1");
118 if (table == NULL) { printf("Unable to open table\n"); return NULL; }
119 int id = 0;
120 char name[200] = "PRABAKARAN";
121 table->bindFld("f1", &id);
122 table->bindFld("f2", name);
123 char *tuple;
124 int ret;
125 int i;
126 int icount =0;
127 NanoTimer timer;
128 int val = *(int*)message;
129 int retrycnt=0;
130 timer.start();
131 for(i = val * RECORDS; i< (val *RECORDS) +RECORDS; i++)
133 rv = conn.startTransaction();
134 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
135 id= i;
136 strcpy(name, "PRABAKARAN0123456750590");
137 ret = table->insertTuple();
138 retrycnt=0;
139 while (ret == ErrLockTimeOut)
141 rv = conn.rollback();
142 if (rv != OK) {
143 while (rv == ErrLockTimeOut)
145 printf("retrying abort\n");
146 rv = conn.rollback();
149 if (retrycnt == 10) { ret = 1; break;}
150 rv = conn.startTransaction();
151 if (rv != OK) {
152 while (rv == ErrLockTimeOut)
154 printf("retrying startTransaction\n");
155 rv = conn.startTransaction();
158 ret = table->insertTuple();
159 retrycnt++;
160 //ret =1; break; //temp to analyse issue
162 if (ret != 0) { printf("RETURNING EARLY: %d with i:%d\n", ret, i); break;}
163 icount++;
164 rv = conn.commit();
165 if (rv != OK) {
166 printf("COMMIT returned %d for record %d\n", rv, i);
167 if (rv != ErrLockTimeOut)
169 printf("RETURNING EARLY: with i:%d\n", i);
170 break;
173 os::usleep(500);
174 while (rv == ErrLockTimeOut) {
175 printf("retrying commit\n");
176 rv = conn.commit();
179 //if (icount %RECORDS ==0) printf("%d\n", i);
181 timer.stop();
182 char msgBuf[1024];
183 sprintf(msgBuf,"Insert: Thread %lu %d: Total rows :%d Time taken:%lld ms\n",os::getthrid(), val, icount, timer.avg()/1000/1000);
184 os::write(1,msgBuf,strlen(msgBuf));
185 dbMgr->closeTable(table);
186 rv = conn.close();
187 return NULL;
189 void* runSelTest(void *message)
191 Connection conn;
192 DbRetVal rv = conn.open("root", "manager");
193 if (rv != OK)
195 printf("Error during connection %d\n", rv);
196 return NULL;
198 DatabaseManager *dbMgr = conn.getDatabaseManager();
199 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
200 Table *table = dbMgr->openTable("t1");
201 if (table == NULL) { printf("Unable to open table\n"); return NULL; }
202 int id = 0;
203 char name[20] = "PRABAKARAN";
204 table->bindFld("f1", &id);
205 table->bindFld("f2", name);
206 char *tuple;
207 int i;
208 int icount =0;
209 NanoTimer timer;
210 int val = *(int*)message;
212 Condition p1;
213 int valTerm = 0;
214 p1.setTerm("f1", OpEquals, &valTerm);
215 table->setCondition(&p1);
217 timer.start();
218 for(i = val * RECORDS; i< (val *RECORDS) +RECORDS; i++)
220 rv = conn.startTransaction(isoLevel);
221 if (rv != OK) while (rv !=OK) rv = conn.startTransaction(isoLevel);
222 valTerm = i;
223 rv = table->execute();
224 if (rv != OK) {
225 printf("Execute returned %d\n", rv);
226 os::usleep(500);
227 int retry=5;
228 while (rv == ErrLockTimeOut) {
229 if (retry == 0) break;
230 printf("retrying execute\n");
231 rv = table->execute();
232 retry--;
235 tuple = (char*)table->fetch() ;
236 if (tuple == NULL) break;
237 icount++;
238 table->closeScan();
239 rv = conn.commit();
240 if (rv != OK) {
241 printf("Commit returned rv:%d for record:%d\n",rv, i);
242 while (rv == ErrLockTimeOut) {
243 printf("retrying commit\n");
244 rv = conn.commit();
249 timer.stop();
250 char msgBuf[1024];
251 sprintf(msgBuf,"Select: Thread %lu %d: Total rows :%d Time taken:%lld ms\n",os::getthrid(), val, icount, timer.avg()/1000/1000);
252 os::write(1,msgBuf,strlen(msgBuf));
253 dbMgr->closeTable(table);
254 rv = conn.close();
255 return NULL;
259 void* runDelTest(void *message)
261 Connection conn;
262 DbRetVal rv = conn.open("root", "manager");
263 if (rv != OK)
265 printf("Error during connection %d\n", rv);
266 return NULL;
268 DatabaseManager *dbMgr = conn.getDatabaseManager();
269 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
270 Table *table = dbMgr->openTable("t1");
271 if (table == NULL) { printf("Unable to open table\n"); return NULL; }
272 int id = 0;
273 char name[20] = "PRABAKARAN";
274 table->bindFld("f1", &id);
275 table->bindFld("f2", name);
276 char *tuple;
277 int i;
278 int ret;
279 int retrycnt=0;
280 int icount =0;
281 NanoTimer timer;
282 int val = *(int*)message;
284 Condition p1;
285 int valTerm = 0;
286 p1.setTerm("f1", OpEquals, &valTerm);
287 table->setCondition(&p1);
289 timer.start();
290 for(i = val * RECORDS; i< (val *RECORDS) +RECORDS; i++)
292 rv = conn.startTransaction();
293 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
294 id= i;
295 valTerm = i;
296 rv = table->execute();
297 if (rv != OK) {
298 printf("Execute returned %d\n", rv);
299 os::usleep(500);
300 int retry=5;
301 while (rv == ErrLockTimeOut) {
302 if (retry == 0) { printf("DELETE:execute failed\n"); break;}
303 printf("retrying execute\n");
304 rv = table->execute();
305 retry--;
308 tuple = (char*)table->fetch() ;
309 if (tuple == NULL) { printf("DELETE:fetch failed\n");break;}
310 ret = table->deleteTuple();
311 retrycnt=0;
312 while (ret == ErrLockTimeOut)
314 table->closeScan();
315 rv = conn.rollback();
316 if (rv != OK) {
317 while (rv == ErrLockTimeOut)
319 printf("retrying abort\n");
320 rv = conn.rollback();
323 if (retrycnt == 10) { ret = 1; break;}
324 rv = conn.startTransaction();
325 if (rv != OK) {
326 while (rv == ErrLockTimeOut)
328 printf("retrying startTransaction\n");
329 rv = conn.startTransaction();
332 rv = table->execute();
333 if (rv != OK) {
334 printf("Execute returned %d\n", rv);
335 os::usleep(500);
336 int retry=5;
337 while (rv == ErrLockTimeOut) {
338 if (retry == 0) { printf("DELETE:execute failed\n"); break;}
339 printf("retrying execute\n");
340 rv = table->execute();
341 retry--;
344 tuple = (char*)table->fetch() ;
345 if (tuple == NULL) { printf("DELETE:fetch failed\n");break;}
346 ret = table->deleteTuple();
347 retrycnt++;
348 //ret =1; break; //temp to analyse issue
350 if (ret != 0) { printf("RETURNING EARLY: %d with i:%d\n", ret, i); break;}
351 table->closeScan();
352 rv = conn.commit();
353 if (rv != OK) {
354 printf("COMMIT returned %d for record %d\n", rv, i);
355 if (rv != ErrLockTimeOut)
357 printf("RETURNING EARLY: with i:%d\n", i);
358 break;
361 os::usleep(500);
362 while (rv == ErrLockTimeOut) {
363 printf("retrying commit\n");
364 rv = conn.commit();
367 icount++;
368 //if (icount %RECORDS ==0) printf("%d\n", i);
370 timer.stop();
371 char msgBuf[1024];
372 sprintf(msgBuf,"Delete: Thread %lu %d: Total rows :%d Time taken:%lld ms\n",os::getthrid(), val, icount, timer.avg()/1000/1000);
373 os::write(1,msgBuf,strlen(msgBuf));
374 dbMgr->closeTable(table);
375 rv = conn.close();
376 return NULL;