*** empty log message ***
[csql.git] / test / performance / DMLThreadTest.c
blob3c62353eec84359bc4ed17a80f395c72a24cb90c
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< 20000; 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 timer.start();
117 for(i = val * 10000; i< (val *10000) +10000; i++)
119 rv = conn.startTransaction();
120 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
121 id= i;
122 strcpy(name, "PRABAKARAN0123456750590");
123 ret = table->insertTuple();
124 while (ret == ErrLockTimeOut)
126 conn.rollback();
127 conn.startTransaction();
128 ret = table->insertTuple();
130 if (ret != 0) { printf("RETURNING EARLY: %d\n", ret); break;}
131 icount++;
132 conn.commit();
133 //if (icount %10000 ==0) printf("%d\n", i);
135 timer.stop();
136 char msgBuf[1024];
137 sprintf(msgBuf,"Insert: Thread %lu %d: Total rows :%d Time taken:%lld ms\n",os::getthrid(), val, icount, timer.avg()/1000/1000);
138 os::write(1,msgBuf,strlen(msgBuf));
139 dbMgr->closeTable(table);
140 rv = conn.close();
141 return NULL;
143 void* runSelTest(void *message)
145 Connection conn;
146 DbRetVal rv = conn.open("root", "manager");
147 if (rv != OK)
149 printf("Error during connection %d\n", rv);
150 return NULL;
152 DatabaseManager *dbMgr = conn.getDatabaseManager();
153 if (dbMgr == NULL) { printf("Auth failed\n"); return NULL;}
154 Table *table = dbMgr->openTable("t1");
155 if (table == NULL) { printf("Unable to open table\n"); return NULL; }
156 int id = 0;
157 char name[1020] = "PRABAKARAN";
158 table->bindFld("f1", &id);
159 table->bindFld("f2", name);
160 char *tuple;
161 int i;
162 int icount =0;
163 NanoTimer timer;
164 int val = *(int*)message;
166 Condition p1;
167 int valTerm = 0;
168 p1.setTerm("f1", OpEquals, &valTerm);
169 table->setCondition(&p1);
171 timer.start();
172 for(i = val * 10000; i< (val *10000) +10000; i++)
174 rv = conn.startTransaction();
175 if (rv != OK) while (rv !=OK) rv = conn.startTransaction();
176 valTerm = i;
177 rv = table->execute();
178 tuple = (char*)table->fetch() ;
179 if (tuple == NULL) break;
180 icount++;
181 table->close();
182 conn.commit();
185 timer.stop();
186 char msgBuf[1024];
187 sprintf(msgBuf,"Select: Thread %d: Total rows :%d Time taken:%lld ms\n",val, icount, timer.avg()/1000/1000);
188 os::write(1,msgBuf,strlen(msgBuf));
189 dbMgr->closeTable(table);
190 rv = conn.close();
191 return NULL;