当前位置:网站首页>数据库连接池:压力测试
数据库连接池:压力测试
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
对比

边栏推荐
- Unity 子线程调用主线程的UI
- 网络地址转换NAT
- unity防止按钮btn被连续点击
- Struggle, programmer chapter 45 tenderness is like water and a good time is like a dream
- If you want to know the stock account opening discount link, how do you know? Is it safe to open an account online?
- Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
- 拜登签署两项旨在加强政府网络安全的新法案
- 阿里云发布CIPU,对于企业客户意味着什么?
- Nested assignment of D
- 擴散模型又殺瘋了!這一次被攻占的領域是...
猜你喜欢
![[Software Engineering] planning and project management](/img/93/4b5b5034fbfb76adef1b4fe93a85cb.png)
[Software Engineering] planning and project management

【Pr】基础流程

C语言学生管理系统(开源)

作为程序员,职业规划需要注意的四个阶段

Zhongshanshan: engineers after being blasted will take off | ONEFLOW u

一文彻底弄懂建造者模式(Builder)

How location coding (PE) works in transformers

Basic usage and FAQs of jasperreport report report generation tool

Biden signe deux nouvelles lois visant à renforcer la cybersécurité du Gouvernement

The diffusion model is crazy again! This time the occupied area is
随机推荐
Biden signs two new laws aimed at strengthening government cyber security
2022orace database installation and use
Unity商业游戏常用真机调试插件
拜登簽署兩項旨在加强政府網絡安全的新法案
OpenVINO CPU加速调研
d安全可以调用系统
D damage and safety
UE4 通过蓝图获取本地文件
基于SSM框架实现的甜品饮品店前后台管理系统甜品商城蛋糕店【源码+数据库】
How maxscale handles event status after MariaDB master-slave switchover -handle_ events
S7-200SMART与FANUC机器人进行Profinet通信的具体方法和步骤
位置编码(PE)是如何在Transformers中发挥作用的
[PR] basic process
『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
剑指Offer46——把数字翻译成字符串
U++编程 数组学习笔记
直播出海 | 国内直播间再出爆品,「外卷」全球如何致胜
Excuse me, is Zhongyan futures reliable? Is the fund safe?
[Zhejiang University] information sharing of the first and second postgraduate entrance examinations
一文彻底弄懂单例模式(Singleton)