1959716 DMLThreadTest fails to read records sometimes
[csql.git] / test / performance / DMLThreadTest.c
blobbec11ffa9f79450490b0ca6a645b116b12797c97
1 #include<CSql.h>
2 #include<NanoTimer.h>
3 #define THREADS 2
4 void* runInsTest(void *p);
5 void* runSelTest(void *p);
6 int main()
9 Connection conn;
10 DbRetVal rv = conn.open("root", "manager");
11 if (rv != OK)
13 printf("Error during connection %d\n", rv);
14 return -1;
16 DatabaseManager *dbMgr = conn.getDatabaseManager();
17 if (dbMgr == NULL) { printf("Auth failed\n"); return -1;}
18 TableDef tabDef;
19 tabDef.addField("f1", typeInt, 0, NULL, true);
20 tabDef.addField("f2", typeString, 196);
21 rv = dbMgr->createTable("t1", tabDef);
22 if (rv != OK) { printf("Table creation failed\n"); return -1; }
23 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
24 strcpy(idxInfo->tableName, "t1");
25 idxInfo->list.append("f1");
26 idxInfo->indType = hashIndex;
27 rv = dbMgr->createIndex("indx1", idxInfo);
28 if (rv != OK) { printf("Index creation failed\n"); return -1; }
30 pthread_t thr[THREADS];
31 int message[THREADS];
32 int status;
33 for (int i=0; i <THREADS; i++) {
34 message[i] = i;
35 pthread_create (&thr[i], NULL,
36 &runInsTest, (void *) &message[i]);
38 for (int i=0; i <THREADS; i++) {
39 pthread_join(thr[i], (void**)&status);
41 Table *table = dbMgr->openTable("t1");
42 if (table == NULL) { printf("Unable to open table\n"); return -1; }
43 table->setCondition(NULL);
44 rv = conn.startTransaction();
45 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
46 table->execute();
47 int count=0;
48 void *tuple = NULL;
49 while ((tuple = (char*) table->fetch())) {
50 count++;
52 printf("Tuples found: %d\n", count);
53 table->close();
54 conn.commit();
57 Condition p1;
58 int valTerm = 0;
59 p1.setTerm("f1", OpEquals, &valTerm);
60 table->setCondition(&p1);
61 int icount=0;
62 for(int i = 0; i< 50000; i++)
64 rv = conn.startTransaction();
65 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
66 valTerm = i;
67 rv = table->execute();
68 tuple = (char*)table->fetch() ;
69 if (tuple == NULL) break;
70 icount++;
71 table->close();
72 conn.commit();
74 printf("Index Tuples found: %d\n", icount);
78 dbMgr->closeTable(table);
80 for (int i=0; i <THREADS; i++) {
81 message[i] = i;
82 pthread_create (&thr[i], NULL,
83 &runSelTest, (void *) &message[i]);
85 for (int i=0; i <THREADS; i++) {
86 pthread_join(thr[i], (void**)&status);
89 dbMgr->dropTable("t1");
90 conn.close();
91 return 0;
93 void* runInsTest(void *message)
95 Connection conn;
96 DbRetVal rv = conn.open("root", "manager");
97 if (rv != OK)
99 printf("Error during connection %d\n", rv);
100 return NULL;
102 DatabaseManager *dbMgr = conn.getDatabaseManager();
103 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
104 Table *table = dbMgr->openTable("t1");
105 if (table == NULL) { printf("Unable to open table\n"); return NULL; }
106 int id = 0;
107 char name[1020] = "PRABAKARAN";
108 table->bindFld("f1", &id);
109 table->bindFld("f2", name);
110 char *tuple;
111 int ret;
112 int i;
113 int icount =0;
114 NanoTimer timer;
115 int val = *(int*)message;
116 int retrycnt=0;
117 timer.start();
118 for(i = val * 10000; i< (val *10000) +10000; i++)
120 rv = conn.startTransaction();
121 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
122 id= i;
123 strcpy(name, "PRABAKARAN0123456750590");
124 ret = table->insertTuple();
125 retrycnt=0;
126 while (ret == ErrLockTimeOut)
128 rv = conn.rollback();
129 if (rv != OK) {
130 while (rv == ErrLockTimeOut)
132 printf("retrying abort\n");
133 rv = conn.rollback();
136 if (retrycnt == 3) { ret = 1; break;}
137 rv = conn.startTransaction();
138 if (rv != OK) {
139 while (rv == ErrLockTimeOut)
141 printf("retrying startTransaction\n");
142 rv = conn.startTransaction();
145 ret = table->insertTuple();
146 retrycnt++;
149 if (ret != 0) { printf("RETURNING EARLY: %d\n", ret); break;}
150 icount++;
151 rv = conn.commit();
152 if (rv != OK) {
153 printf("COMMIT returned %d\n", rv);
154 os::usleep(500);
155 while (rv == ErrLockTimeOut) {
156 printf("retrying commit\n");
157 rv = conn.commit();
160 //if (icount %10000 ==0) printf("%d\n", i);
162 timer.stop();
163 char msgBuf[1024];
164 sprintf(msgBuf,"Insert: Thread %lu %d: Total rows :%d Time taken:%lld ms\n",os::getthrid(), val, icount, timer.avg()/1000/1000);
165 os::write(1,msgBuf,strlen(msgBuf));
166 dbMgr->closeTable(table);
167 rv = conn.close();
168 return NULL;
170 void* runSelTest(void *message)
172 Connection conn;
173 DbRetVal rv = conn.open("root", "manager");
174 if (rv != OK)
176 printf("Error during connection %d\n", rv);
177 return NULL;
179 DatabaseManager *dbMgr = conn.getDatabaseManager();
180 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
181 Table *table = dbMgr->openTable("t1");
182 if (table == NULL) { printf("Unable to open table\n"); return NULL; }
183 int id = 0;
184 char name[1020] = "PRABAKARAN";
185 table->bindFld("f1", &id);
186 table->bindFld("f2", name);
187 char *tuple;
188 int i;
189 int icount =0;
190 NanoTimer timer;
191 int val = *(int*)message;
193 Condition p1;
194 int valTerm = 0;
195 p1.setTerm("f1", OpEquals, &valTerm);
196 table->setCondition(&p1);
198 timer.start();
199 for(i = val * 10000; i< (val *10000) +10000; i++)
201 rv = conn.startTransaction();
202 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
203 valTerm = i;
204 rv = table->execute();
205 if (rv != OK) {printf("Execute returned failure\n"); break;}
206 tuple = (char*)table->fetch() ;
207 if (tuple == NULL) break;
208 icount++;
209 table->close();
210 rv = conn.commit();
211 if (rv != OK) {
212 while (rv == ErrLockTimeOut) {
213 printf("retrying commit\n");
214 rv = conn.commit();
219 timer.stop();
220 char msgBuf[1024];
221 sprintf(msgBuf,"Select: Thread %d: Total rows :%d Time taken:%lld ms\n",val, icount, timer.avg()/1000/1000);
222 os::write(1,msgBuf,strlen(msgBuf));
223 dbMgr->closeTable(table);
224 rv = conn.close();
225 return NULL;