当前位置:网站首页>数据库连接池:压力测试
数据库连接池:压力测试
2022-06-22 13:39:00 【_索伦】
前言
验证数据的插入操作所花费的时间,第一次测试使用普通的数据库访问操作,第二次测试使用带连接池的数据库访问操作,对比两次操作同样数据量所花费的时间。
文章目录
单线程测试
一、不带连接池
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "Connection.h"
#include "CommonConnectionPool.h"
using namespace std;
int main(void) {
clock_t begin = clock();
for (int i = 1; i <= 1000; ++i)
{
Connection conn;
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
clock_t end = clock();
cout << (end - begin) << "ms" << endl;
return 0;
}
for循环依次测1000个数据、5000个数据、10000个数据
1.插入1000个数据测试



平均值:11866ms
2.插入5000个数据



平均值:59581ms
3.插入10000个数据



平均值:120634ms
二、带连接池
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "Connection.h"
#include "CommonConnectionPool.h"
using namespace std;
int main(void) {
clock_t begin = clock();
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 1; i <= 1000; ++i)
{
shared_ptr<Connection> sp = cp->getConnection();
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
sp->update(sql);
}
clock_t end = clock();
cout << (end - begin) << "ms" << endl;
return 0;
}
1.插入1000个数据



取平均值:3226ms
2.插入5000个数据



平均值:17498ms
3.插入10000个数据



平均值:34179ms
对比

四线程测试
一、不带连接池
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "Connection.h"
#include "CommonConnectionPool.h"
using namespace std;
int main(void) {
Connection conn;
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
clock_t begin = clock();
thread t1([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 1; i <= 250; ++i)
{
Connection conn;
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
thread t2([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 1; i <= 250; ++i)
{
Connection conn;
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
thread t3([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 1; i <= 250; ++i)
{
Connection conn;
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
thread t4([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 1; i <= 250; ++i)
{
Connection conn;
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
conn.connect("127.0.0.1", 3306, "root", "123456", "chat");
conn.update(sql);
}
});
t1.join();
t2.join();
t3.join();
t4.join();
clock_t end = clock();
cout << (end - begin) << "ms" << endl;
return 0;
}
1.插入1000数据



平均值:2882ms
2.插入5000数据



平均值:13465ms
3.插入10000数据


平均:26191ms
二、带连接池
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "Connection.h"
#include "CommonConnectionPool.h"
using namespace std;
int main(void) {
clock_t begin = clock();
thread t1([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 1; i <= 250; ++i)
{
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
shared_ptr<Connection> sp = cp->getConnection();
sp->update(sql);
}
});
thread t2([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 251; i <= 500; ++i)
{
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
shared_ptr<Connection> sp = cp->getConnection();
sp->update(sql);
}
});
thread t3([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 501; i <= 750; ++i)
{
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
shared_ptr<Connection> sp = cp->getConnection();
sp->update(sql);
}
});
thread t4([]() {
ConnectionPool* cp = ConnectionPool::getConnectionPool();
for (int i = 751; i <= 1000; ++i)
{
char sql[1024] = {
0 };
sprintf(sql, "insert into user(id, name, age, sex) values(%d, '%s', %d, '%s')",
i, "zhangsan", 20, "male");
shared_ptr<Connection> sp = cp->getConnection();
sp->update(sql);
}
});
t1.join();
t2.join();
t3.join();
t4.join();
clock_t end = clock();
cout << (end - begin) << "ms" << endl;
return 0;
}
1.插入1000数据



平均值:1269ms
2.插入5000数据



平均值:6185ms
3.插入10000数据



平均值:11717ms
对比

边栏推荐
- U++编程 移动 学习笔记
- Le modèle de diffusion est encore fou! Cette fois - ci, la zone occupée était...
- 拜登簽署兩項旨在加强政府網絡安全的新法案
- S7-200SMART与FANUC机器人进行Profinet通信的具体方法和步骤
- Specific methods and steps of PROFINET communication between s7-200smart and Fanuc robot
- Maui uses Masa blazor component library
- Struggle, programmer chapter 45 tenderness is like water and a good time is like a dream
- C# 分页计算 总页数、当前页数据集合
- D damage and safety
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
猜你喜欢

The diffusion model is crazy again! This time the occupied area is

成都测试设备开发_单片机C语言之数组介绍

Basic usage and FAQs of jasperreport report report generation tool

开源SPL重新定义OLAP Server
![[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide

基于SSH框架甜品商城管理系统【源码+数据库】
![[introduction to postgraduate entrance examination] analysis of postgraduate entrance examination data of Cyberspace Security Major of Beijing Jiaotong University from 2018 to 2022](/img/84/b572b3b80cc0dd1489076116cf0638.png)
[introduction to postgraduate entrance examination] analysis of postgraduate entrance examination data of Cyberspace Security Major of Beijing Jiaotong University from 2018 to 2022

位置编码(PE)是如何在Transformers中发挥作用的

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-5

树结构二叉树
随机推荐
拜登簽署兩項旨在加强政府網絡安全的新法案
Thoroughly understand the factory mode
Common real machine debugging plug-ins for unity commercial games
一文彻底弄懂建造者模式(Builder)
验证码是自动化的天敌?看看大神是怎么解决的
Random forest of machine learning
C#泛型_泛型类
How to implement interface exception scenario testing? Exploration of test methods and implementation of test tools
难怪考研热度这么高,这些是研究生才有的“隐藏福利”!
【浙江大学】考研初试复试资料分享
Excuse me, is Zhongyan futures reliable? Is the fund safe?
Unity sub thread calls UI of main thread
Maui uses Masa blazor component library
Data collection: skillfully using Bloom filter to extract data summary
PHP内置协议(支持的协议和封装协议)
同花顺如何开户?在线开户安全么?
网络地址转换NAT
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
【Pr】基础流程
flutter video_player实现监听和自动播放下一首歌曲