-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (141 loc) · 4.91 KB
/
Copy pathmain.cpp
File metadata and controls
165 lines (141 loc) · 4.91 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <memory>
#include "connectionMysql.h"
#include "connectionPool.h"
#include <thread>
using namespace std;
//测试数据库连接池性能,不使用数据库连接池
//单线程 5000个连接用时为: 51036304657 纳秒 即 51036 毫秒
//多线程 5000个连接用时为: 21062800477 纳秒 即 21062 毫秒
void op1(int begin,int end)
{
//创建数据库连接,并进行数据插入
for(int i=begin;i<end;i++)
{
MysqlConn conn;
//连接数据库
conn.connect("root","159612","book","192.168.18.135");
char sql[1024]={0};
//插入
sprintf(sql,"insert into t_user values(%d,'test','test','[email protected]')",i);
//执行sql语句
conn.update(sql);
}
}
//测试数据库连接池性能,使用数据库连接池
//单线程 5000个连接用时为: 9164084935 纳秒 即 9164 毫秒
//多线程 5000个连接用时为: 2762778513 纳秒 即 2762 毫秒
void op2(ConnectionPool* pool, int begin,int end) //传入数据库连接池实例
{
//创建数据库连接,并进行数据插入
for(int i=begin;i<end;i++)
{
//由 shared_ptr 调用数据库连接池的析构函数,实现资源回收
shared_ptr<MysqlConn> conn=pool->getConnection();
char sql[1024]={0};
//插入
sprintf(sql,"insert into t_user values(%d,'test','test','[email protected]')",i);
conn->update(sql);
}
}
void testMysql1()
{
//条件编译
#if 1
//单线程,不使用数据库连接池
steady_clock::time_point begin=steady_clock::now();
op1(0,5000);
steady_clock::time_point end=steady_clock::now();
//精确到纳秒的 时间段duration
auto length = end - begin;
cout<<" 非数据库连接池,单线程情况下,5000个连接用时为: "<<length.count()<<" 纳秒 "
<<"即 "<<length.count()/1000000<<" 毫秒\n";
# else
//单线程,使用数据库连接池
ConnectionPool* pool=ConnectionPool::getConnectPool();
steady_clock::time_point begin=steady_clock::now();
op2(pool, 0, 5000);
steady_clock::time_point end=steady_clock::now();
// //精确到纳秒的 时间段duration
auto length = end - begin;
cout<<" 使用数据库连接池,单线程情况下,5000个连接用时为: "<<length.count()<<" 纳秒 "
<<"即 "<<length.count()/1000000<<" 毫秒\n";
# endif
}
void testMysql2()
{
# if 1
//5000个连接用时为: 21062800477 纳秒 即 21062 毫秒
//在创建子线程时,添加一个额外的数据库连接,这样就不会因为多个线程同时连接数据库,数据库丢弃连接了
MysqlConn conn;
conn.connect("root","159612","book","192.168.18.135");
steady_clock::time_point begin=steady_clock::now();
//添加线程
thread t1(op1,0,1000);
thread t2(op1,1000,2000);
thread t3(op1,2000,3000);
thread t4(op1,3000,4000);
thread t5(op1,4000,5000);
//阻塞主线程
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
steady_clock::time_point end=steady_clock::now();
//精确到纳秒的 时间段duration
auto length = end - begin;
cout<<" 非数据库连接池,多线程情况下,5000个连接用时为: "<<length.count()<<" 纳秒 "
<<"即 "<<length.count()/1000000<<" 毫秒\n";
# else
//5000个连接用时为: 2762778513 纳秒 即 2762 毫秒
ConnectionPool* pool = ConnectionPool::getConnectPool();
steady_clock::time_point begin=steady_clock::now();
//添加线程
thread t1(op2,pool,0,1000);
thread t2(op2,pool,1000,2000);
thread t3(op2,pool,2000,3000);
thread t4(op2,pool,3000,4000);
thread t5(op2,pool,4000,5000);
//阻塞主线程
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
steady_clock::time_point end=steady_clock::now();
//精确到纳秒的 时间段duration
auto length = end - begin;
cout<<" 数据库连接池,多线程情况下,5000个连接用时为: "<<length.count()<<" 纳秒 "
<<"即 "<<length.count()/1000000<<" 毫秒\n";
# endif
}
void query()
{
MysqlConn conn;
//连接数据库
conn.connect("root","159612","book","192.168.18.135");
//插入
string insertSql="insert into t_user values(3,'test','test','[email protected]')";
//执行sql语句
bool flag = conn.update(insertSql);
cout<<"Insert SQL flag value is "<<flag<<"\n";
//查询
string querySql="select * from t_user";
conn.query(querySql);
//遍历得到的结果集
while (conn.next())
{
cout<<conn.value(0)<<", " <<conn.value(1)<<", "<<conn.value(2)<<", "<<conn.value(3)<<"\n";
}
}
int main()
{
//测试插入查询数据
// query();
//测试性能 单线程下使用功能 数据库连接池与不使用数据库连接池
testMysql1();
// 多线程下使用数据库连接池与不使用数据库连接池
//testMysql2();
return 0;
}