File: test_perf.cpp

package info (click to toggle)
cppdb 0.3.1+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 668 kB
  • sloc: cpp: 7,373; sh: 133; ansic: 72; makefile: 7
file content (125 lines) | stat: -rw-r--r-- 2,897 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
///////////////////////////////////////////////////////////////////////////////
//                                                                             
//  Copyright (C) 2010-2011  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
//                                                                             
//  Distributed under:
//
//                   the Boost Software License, Version 1.0.
//              (See accompanying file LICENSE_1_0.txt or copy at 
//                     http://www.boost.org/LICENSE_1_0.txt)
//
//  or (at your opinion) under:
//
//                               The MIT License
//                 (See accompanying file MIT.txt or a copy at
//              http://www.opensource.org/licenses/mit-license.php)
//
///////////////////////////////////////////////////////////////////////////////
#include <cppdb/frontend.h>
#include <iostream>
#include <stdlib.h>

#if defined WIN32  || defined _WIN32 || defined __WIN32 || defined(__CYGWIN__)

#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

class timer {
public:
	timer()
	{
		QueryPerformanceFrequency(&freq_);
	}
	void start()
	{
		QueryPerformanceCounter(&start_);
	}
	void stop()
	{
		QueryPerformanceCounter(&stop_);
	}
	double diff() const
	{
		return double(stop_.QuadPart - start_.QuadPart) / freq_.QuadPart;
	}
private:
	LARGE_INTEGER freq_;
	LARGE_INTEGER start_;
	LARGE_INTEGER stop_;
};

#else 

#include <sys/time.h>

class timer {
public:
	timer() {}
	void start()
	{
		gettimeofday(&start_,0);
	}
	void stop()
	{
		gettimeofday(&stop_,0);
	}
	double diff() const
	{
		double udiff = (stop_.tv_sec  - start_.tv_sec) * 1000000LL + stop_.tv_usec - start_.tv_usec;
		return udiff * 1e-6;
	}
private:
	struct timeval start_;
	struct timeval stop_;
};


#endif

int main(int argc,char **argv)
{
	if(argc!=2) {
		std::cerr << "conn string required" << std::endl;
		return 1;
	}
	try {
		static const int max_val = 10000;
		cppdb::session sql(argv[1]);

		try { sql << "DROP TABLE test" << cppdb::exec; } catch(...) {}

		if(sql.engine() == "mysql")
			sql << "create table test ( id integer primary key, val varchar(100)) Engine=innodb" << cppdb::exec;
		else
			sql << "create table test ( id integer primary key, val varchar(100))" << cppdb::exec;
		
		{
			cppdb::transaction tr(sql);
			for(int i=0;i<max_val;i++) {
				sql << "insert into test values(?,?)" << i << "Hello World" << cppdb::exec;
			}
			tr.commit();
		}
		
		timer tm;

		tm.start();
		
		for(int j=0;j<max_val * 10;j++) {
			std::string v;
			sql << "select val from test where id = ?" << (rand() % max_val)<< cppdb::row >> v;
			if(v!="Hello World")
				throw std::runtime_error("Wrong");
		}
		tm.stop();
		std::cout << "Passed " << tm.diff() << " seconds" << std::endl;
	}
	catch(std::exception const &e) {
		std::cerr << e.what() << std::endl;
		return 1;
	}
	std::cout << "Ok" << std::endl;
	return 0;
}