当前位置:网站首页>Database connection pool: stress testing

Database connection pool: stress testing

2022-06-22 15:05:00 _ Soren

Preface

The time taken to verify the insertion of data , The first test uses ordinary database access operations , The second test uses the database access operation with connection pool , Compare the time spent in two operations with the same amount of data .

Single thread test

One 、 Without connection pool

#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 Cyclic sequential measurement 1000 Data 、5000 Data 、10000 Data

1. Insert 1000 Data tests

 Insert picture description here
 Insert picture description here
 Insert picture description here

Average :11866ms

2. Insert 5000 Data

 Insert picture description here

 Insert picture description here

 Insert picture description here

Average :59581ms

3. Insert 10000 Data

 Insert picture description here
 Insert picture description here
 Insert picture description here

Average :120634ms

Two 、 With connection pool

#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. Insert 1000 Data

 Insert picture description here
 Insert picture description here
 Insert picture description here

Average. :3226ms

2. Insert 5000 Data

 Insert picture description here
 Insert picture description here

 Insert picture description here

Average :17498ms

3. Insert 10000 Data

 Insert picture description here

 Insert picture description here
 Insert picture description here

Average :34179ms

contrast

 Insert picture description here

Four thread test

One 、 Without connection pool

#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. Insert 1000 data

 Insert picture description here

 Insert picture description here

 Insert picture description here
Average :2882ms

2. Insert 5000 data

 Insert picture description here

 Insert picture description here

 Insert picture description here

Average :13465ms

3. Insert 10000 data

 Insert picture description here
 Insert picture description here

Average :26191ms

Two 、 With connection pool

#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. Insert 1000 data

 Insert picture description here
 Insert picture description here
 Insert picture description here
Average :1269ms

2. Insert 5000 data

 Insert picture description here

 Insert picture description here

 Insert picture description here

Average :6185ms

3. Insert 10000 data

 Insert picture description here
 Insert picture description here
 Insert picture description here

Average :11717ms

contrast

 Insert picture description here

原网站

版权声明
本文为[_ Soren]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221339138309.html