adding test scripts
[csql.git] / test / performance / SQLAdapterTest.c
blobebc372403ccf19046e5ed9df5a950cb174e1d7e9
1 #include<AbsSqlStatement.h>
2 #include<SqlFactory.h>
3 #include<NanoTimer.h>
4 #define ITERATIONS 100
5 //NOTE CREATE TABLE t1 (f1 int, f2 char(196), primary key(f1));
6 //add 1:t1 in csqltable.conf and do csqlserver -c
7 int main()
9 DbRetVal rv = OK;
10 AbsSqlConnection *con = SqlFactory::createConnection(CSqlAdapter);
11 rv = con->connect("root", "manager");
12 if (rv != OK) return 1;
13 AbsSqlStatement *stmt = SqlFactory::createStatement(CSqlAdapter);
14 stmt->setConnection(con);
15 char statement[1024];
17 strcpy(statement, "INSERT INTO t1 (f1, f2) VALUES (?, ?);");
18 int id1 =10;
19 char name[196];
20 strcpy(name, "Rithish");
21 NanoTimer timer;
22 rv = stmt->prepare(statement);
23 if (rv != OK) {delete stmt; delete con; return -1; }
24 int count =0, rows=0;
25 for (int i = 0 ; i < ITERATIONS ; i++)
27 timer.start();
28 rv = con->beginTrans();
29 if (rv != OK) break;
30 id1 = i;
31 stmt->setIntParam(1, id1);
32 strcpy(name, "Gopika");
33 stmt->setStringParam(2, name);
34 rv = stmt->execute(rows);
35 if (rv != OK) break;
36 rv = con->commit();
37 if (rv != OK) break;
38 timer.stop();
39 count++;
41 printf("Total Rows Inserted %d %lld %lld %lld\n", count, timer.min(),
42 timer.max(), timer.avg());
43 stmt->free();
45 strcpy(statement, "SELECT * FROM t1 where f1 = ?;");
46 rv = stmt->prepare(statement);
47 if (rv != OK) {delete stmt; delete con; return -1; }
48 stmt->bindField(1, &id1);
49 stmt->bindField(2, name);
50 timer.reset();
51 count =0;
52 for (int i = 0 ; i < ITERATIONS ; i++)
54 timer.start();
55 rv = con->beginTrans();
56 if (rv != OK) break;
57 stmt->setIntParam(1, i);
58 stmt->execute(rows);
59 if (stmt->fetch() == NULL) { printf("unable to read record\n"); break; }
60 stmt->close();
61 rv = con->commit();
62 if (rv != OK) break;
63 timer.stop();
64 count++;
66 stmt->free();
67 printf("Total Rows Selected %d %lld %lld %lld\n", count, timer.min(),
68 timer.max(), timer.avg());
70 strcpy(statement, "UPDATE t1 set f2=? where f1=?;");
71 rv = stmt->prepare(statement);
72 if (rv != OK) {delete stmt; delete con; return -1; }
73 stmt->bindField(1, &id1);
74 stmt->bindField(2, name);
75 timer.reset();
76 count =0;
77 for (int i = 0 ; i < ITERATIONS ; i++)
79 timer.start();
80 rv = con->beginTrans();
81 if (rv != OK) break;
82 stmt->setIntParam(2, i);
83 stmt->setStringParam(1, "ChangedValue");
84 rv = stmt->execute(rows);
85 if (rv != OK && rows !=1) break;
86 rv = con->commit();
87 if (rv != OK) break;
88 timer.stop();
89 count++;
91 stmt->free();
92 printf("Total Rows Updated %d %lld %lld %lld\n", count, timer.min(),
93 timer.max(), timer.avg());
95 strcpy(statement, "DELETE FROM t1 where f1=?;");
96 rv = stmt->prepare(statement);
97 if (rv != OK) {delete stmt; delete con; return -1; }
98 stmt->bindField(1, &id1);
99 timer.reset();
100 count =0;
101 for (int i = 0 ; i < ITERATIONS ; i++)
103 timer.start();
104 rv = con->beginTrans();
105 if (rv != OK) break;
106 stmt->setIntParam(1, i);
107 rv = stmt->execute(rows);
108 if (rv != OK && rows != 1) break;
109 rv = con->commit();
110 if (rv != OK) break;
111 timer.stop();
112 count++;
114 stmt->free();
115 printf("Total Rows Deleted %d %lld %lld %lld\n", count, timer.min(),
116 timer.max(), timer.avg());
118 delete stmt;
119 delete con;
120 return 0;