| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]> |
| 4 | // |
| 5 | // Distributed under: |
| 6 | // |
| 7 | // the Boost Software License, Version 1.0. |
| 8 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | // |
| 11 | // or (at your opinion) under: |
| 12 | // |
| 13 | // The MIT License |
| 14 | // (See accompanying file MIT.txt or a copy at |
| 15 | // http://www.opensource.org/licenses/mit-license.php) |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | #include <cppdb/frontend.h> |
| 19 | #include <iostream> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | #if defined WIN32 || defined _WIN32 || defined __WIN32 || defined(__CYGWIN__) |
| 23 | |
| 24 | #ifndef NOMINMAX |
| 25 | #define NOMINMAX |
| 26 | #endif |
| 27 | #include <windows.h> |
| 28 | |
| 29 | class timer { |
| 30 | public: |
| 31 | timer() |
| 32 | { |
| 33 | QueryPerformanceFrequency(&freq_); |
| 34 | } |
| 35 | void start() |
| 36 | { |
| 37 | QueryPerformanceCounter(&start_); |
| 38 | } |
| 39 | void stop() |
| 40 | { |
| 41 | QueryPerformanceCounter(&stop_); |
| 42 | } |
| 43 | double diff() const |
| 44 | { |
| 45 | return double(stop_.QuadPart - start_.QuadPart) / freq_.QuadPart; |
| 46 | } |
| 47 | private: |
| 48 | LARGE_INTEGER freq_; |
| 49 | LARGE_INTEGER start_; |
| 50 | LARGE_INTEGER stop_; |
| 51 | }; |
| 52 | |
| 53 | #else |
| 54 | |
| 55 | #include <sys/time.h> |
| 56 | |
| 57 | class timer { |
| 58 | public: |
| 59 | timer() {} |
| 60 | void start() |
| 61 | { |
| 62 | gettimeofday(&start_,0); |
| 63 | } |
| 64 | void stop() |
| 65 | { |
| 66 | gettimeofday(&stop_,0); |
| 67 | } |
| 68 | double diff() const |
| 69 | { |
| 70 | double udiff = (stop_.tv_sec - start_.tv_sec) * 1000000LL + stop_.tv_usec - start_.tv_usec; |
| 71 | return udiff * 1e-6; |
| 72 | } |
| 73 | private: |
| 74 | struct timeval start_; |
| 75 | struct timeval stop_; |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | #endif |
| 80 | |
| 81 | int main(int argc,char **argv) |
| 82 | { |
| 83 | if(argc!=2) { |
| 84 | std::cerr << "conn string required" << std::endl; |
| 85 | return 1; |
| 86 | } |
| 87 | try { |
| 88 | static const int max_val = 10000; |
| 89 | cppdb::session sql(argv[1]); |
| 90 | |
| 91 | try { sql << "DROP TABLE test" << cppdb::exec; } catch(...) {} |
| 92 | |
| 93 | if(sql.engine() == "mysql") |
| 94 | sql << "create table test ( id integer primary key, val varchar(100)) Engine=innodb" << cppdb::exec; |
| 95 | else |
| 96 | sql << "create table test ( id integer primary key, val varchar(100))" << cppdb::exec; |
| 97 | |
| 98 | { |
| 99 | cppdb::transaction tr(sql); |
| 100 | for(int i=0;i<max_val;i++) { |
| 101 | sql << "insert into test values(?,?)" << i << "Hello World" << cppdb::exec; |
| 102 | } |
| 103 | tr.commit(); |
| 104 | } |
| 105 | |
| 106 | timer tm; |
| 107 | |
| 108 | tm.start(); |
| 109 | |
| 110 | for(int j=0;j<max_val * 10;j++) { |
| 111 | std::string v; |
| 112 | sql << "select val from test where id = ?" << (rand() % max_val)<< cppdb::row >> v; |
| 113 | if(v!="Hello World") |
| 114 | throw std::runtime_error("Wrong"); |
| 115 | } |
| 116 | tm.stop(); |
| 117 | std::cout << "Passed " << tm.diff() << " seconds" << std::endl; |
| 118 | } |
| 119 | catch(std::exception const &e) { |
| 120 | std::cerr << e.what() << std::endl; |
| 121 | return 1; |
| 122 | } |
| 123 | std::cout << "Ok" << std::endl; |
| 124 | return 0; |
| 125 | } |