您的位置:首页 > 数据库 > MySQL

mysql 并发测试

2015-08-20 12:06 781 查看
针对上一节做一些针对公司业务的测试。

我们来做一些压力测试。

服务器配置:

操作系统: centos 5.6-64

CPU: 8核

内存: 8G

硬盘:sas

文件系统:linux

MySQL:5.6.

网卡: 100M

网络环境: 良好

数据库表:

c_account_customer,这里用来根据id查询、更新某些字段

CREATE TABLE `c_account_customer` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
`customer_id` int(10) unsigned NOT NULL COMMENT '客户号',
`item_id` int(10) unsigned NOT NULL COMMENT '科目号',
`ref_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '业务识别号',
`debit_balance` decimal(14,2) DEFAULT NULL COMMENT '借方余额',
`credit_balance` decimal(14,2) DEFAULT NULL COMMENT '贷方余额',
`last_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `customer_id` (`customer_id`,`item_id`,`ref_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

c_entry_company用来插入借、贷科目信息

CREATE TABLE `c_entry_company` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
`entity_id` int(10) unsigned NOT NULL COMMENT '机构号',
`item_id` int(10) unsigned NOT NULL COMMENT '科目号',
`ref_id` int(11) NOT NULL DEFAULT '0' COMMENT '业务识别号',
`direction` tinyint(3) unsigned NOT NULL COMMENT '记账方向',
`amount` decimal(10,0) NOT NULL COMMENT '记账金额',
`operation` int(11) DEFAULT NULL COMMENT '操作类型号',
`operation_id` int(11) DEFAULT NULL COMMENT '操作流水号',
`note` varchar(200) DEFAULT NULL COMMENT '业务备注',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '数据创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

testvalue表,用来记录事务中查询到的字段值,进行结果分析:

CREATE TABLE `testvalue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`val` decimal(14,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

针对上述三个数据表的事务操作:

CREATE DEFINER=`root`@`localhost` PROCEDURE `Test`(out debitb decimal(14,2))
BEGIN

START TRANSACTION ;

select @db:=debit_balance from c_account_customer where id=1 for update;
set debitb=@db;
insert into abacus.testvalue (val) values (@db);
insert into abacus.c_entry_customer (customer_id,item_id,ref_id,direction,amount,operation,operation_id,note) values (1,1,1,1,1,1,1,1);
insert into abacus.c_entry_customer (customer_id,item_id,ref_id,direction,amount,operation,operation_id,note) values (1,2,1,1,1,1,1,1);
update abacus.c_account_customer set debit_balance=@db+1 where id=1;
commit;

END

C#测试代码

public static void TestComplicating()
{
Console.WriteLine("开始:"+DateTime.Now);
bool b = true;
int num = 0;
DateTime start = DateTime.Now;
while (b)
{
num++;
Complicating();

if (DateTime.Now.AddMinutes(-30)>start)
{
b = false;
}
}
Console.WriteLine("结束:" + DateTime.Now+";循环次数:"+num);
}

public static void Complicating()
{
for (int i = 0; i < 10; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(Execution));
thread.Start(i);

}
}

public static void Execution(object i)
{
MySqlParameter debitb = new MySqlParameter("?debitb", MySqlDbType.Decimal);
debitb.Direction = ParameterDirection.Output;

IDataParameter[] parameters = new IDataParameter[] {
debitb
};
mysqlhelper.RunProcedure("Test", parameters, "test");

//Task.Factory.StartNew(() =>Logs.jobStatus.Info("更新前值:"+debitb.Value+";当前线程id:" + Thread.CurrentThread.ManagedThreadId + ";循环:" + (int)i));

}

第一组测试结果

Mysql 服务器最大连接数: 1024

.net 客户端连接池最大连接数:500

不模拟耗时操作

测试前先插入一条数据:insert into abacus.c_account_customer (customer_id,item_id,ref_id,debit_balance,credit_balance)values(1,1,1,10000,10000);

第1次: 100个并发 ,完成耗时3秒,无数据丢失,无数据错误。
第2次: 500个并发 ,完成耗时10秒,无数据丢失,无数据错误。
第3次: 5个并发,while循环中持续1分钟,完成耗时1分钟,事务次数171540,无数据丢失,无数据错误。
第4次: 10并发,while循环中持续10分钟,完成耗时10分钟,事务1885640次数 ,无丢数据( c_entry_company中3771280条数据),无数据错误(debit_balance=1895640.00 =1885640+10000)。
第5次: 10并发,while循环中持续30分钟,完成耗时30分钟,循环次数(694861),事务6948610次数 ,无丢数据( c_entry_company中13897220条数据),无数据错误(debit_balance=6958610.00 =6948610+10000)。

图例:









上图中 ,

图1,2 为第4次测试时mysql服务器截图。

图3为第4次测试时mysql服务器每秒执行事务次数。

图4为第5次测试时mysql服务器每分钟执行事务次数。

注:欢迎提出更优、合理的方案。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: