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

边栏推荐
- Common real machine debugging plug-ins for unity commercial games
- Nansen Annual Report
- 『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
- Deadlock found when trying to get lock; Try restarting transaction
- Fight, programmer -- Chapter 42 will draw a bow like a full moon, look northwest and shoot Sirius
- At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- Nested assignment of D
- If you want to know the stock account opening discount link, how do you know? Is it safe to open an account online?
- C语言学生管理系统(开源)
猜你喜欢

基于SSM框架实现的甜品饮品店前后台管理系统甜品商城蛋糕店【源码+数据库】

Using pictures to locate app elements sikulix

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

那些令人懵逼的用户态&内核态

C# Winform 相册功能,图片缩放,拖拽,预览图分页

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

Cat agile team coaching workshops - August 20

Basic usage and FAQs of jasperreport report report generation tool

网站存在的价值是什么?为什么要搭建独立站

一文彻底弄懂建造者模式(Builder)
随机推荐
Vscode个性化设置:让一个小萌妹陪你敲代码
Unity's rich text color sets the color to be fully transparent
d安全可以调用系统
Sikulix选取相对位置的图片(进阶版)
拜登簽署兩項旨在加强政府網絡安全的新法案
Thoroughly understand the builder mode (builder)
一文彻底弄懂单例模式(Singleton)
【直播回顾】战码先锋第六期:共建测试子系统,赋能开发者提
Fight, programmer -- Chapter 42 will draw a bow like a full moon, look northwest and shoot Sirius
Biden signe deux nouvelles lois visant à renforcer la cybersécurité du Gouvernement
C language student management system (open source)
[untitled]
C generic_ Generic class
Unity sub thread calls UI of main thread
Struggle, programmer chapter 50: a bosom friend in the sea
Excuse me, is Zhongyan futures reliable? Is the fund safe?
Loot、USDT
Introduction to groovy syntax
验证码是自动化的天敌?看看大神是怎么解决的
推动制造业高效增长,用友U9 cloud不断精进背后的密码