【转自网络】利用loadrunner做mysql压力测试
最近跟测试组学习loadrunner的使用,测试组的姑娘们习惯用界面进行操作,而习惯linux平台使用的我很多功能都使用代码来实现了。最近用loadrunner写了一个模拟社区用户压测论坛数据库的一个脚本,进行数据库的压力测试和优化工作。用户行为分析:在社区中,看帖的人是发帖人的10倍以上,而看帖人大概80%以上都在看新帖,20以下的用户有挖坟行为。因此,设定如此的比率:每11个用户,1个发帖,8个看最近30%的帖,两个看老的70%的帖。我的论坛帖子回复表大概是:973505个帖子的回复,两千多万的回帖。根据这些数据,配合mysql的c api,写如下脚本:注:脚本的my_mysql_insert()函数是有问题的,多线程下有一个资源符没处理好,因为还不太了解loadrunner的线程机制,所以留下了一个bug。在做完这个脚本后,我发现我们测试机性能都不错,很难在一个5G大小的单表上主键查询造成很大的压力,所以,计划把dz论坛架设,用php+mysql真实环境下进行压测,这样可以顺便练习http函数下的loadrunner编程。[*]globals.h
[*]#ifndef _GLOBALS_H
[*]#define _GLOBALS_H
[*]#include "lrun.h"
[*]#include "web_api.h"
[*]#include "lrw_custom_body.h"
[*]#include "modal_dialog_callbacks.h"
[*]#define random(x) (rand()%x)
[*]#include <mysql .h>
[*]#include <mysql_com .h>
[*]#include <mysql_time .h>
[*]#include <mysql_version .h>
[*]#include <typelib .h>
[*]#include <my_list .h>
[*]#include <my_alloc .h>
[*]#endif // _GLOBALS_H
[*]</my_alloc></my_list></typelib></mysql_version></mysql_time></mysql_com></mysql>
[*]vuser_init.c
[*]MYSQL *db;
[*]MYSQL_ROW record;
[*]vuser_init()
[*]{
[*]char sql;
[*]MYSQL_RES *results1;
[*] lr_load_dll("libmysql.dll");
[*] db = mysql_init(NULL);
[*]mysql_real_connect(db, "192.168.8.32", "root", "123456", "sunboyu_test", 3311, "/tmp/mysql3311.sock", 1);
[*]
[*]sprintf(sql, "show tables");
[*]mysql_query(db, sql);
[*] results1 = mysql_store_result(db);
[*] while((record = mysql_fetch_row(results1)))
[*]{
[*] lr_log_message("table = %s", record);
[*] }
[*]mysql_free_result(results1);
[*]return 0;
[*]}
[*]vuser_end.c
[*]vuser_end()
[*]{
[*] mysql_close(db);
[*] mysql_server_end();
[*]return 0;
[*]}
[*]Action.c
[*]Action()
[*]{
[*]int r1 = 0;
[*]char sql;
[*]r1 = random(11); //10种用户,8种正常访问,两种在挖坟
[*]lr_log_message(" %d", r1);
[*] if(r1==11)
[*] {
[*] my_mysql_insert();
[*] }
[*] else
[*] {
[*] my_mysql_query( r1 );
[*] }
[*]return 0;
[*]}
[*]
[*]int my_mysql_query( int randid )
[*]{
[*]int r2 = 0;
[*]int j = 0;
[*]int count = 0;
[*]int page = 0;
[*]char sql;
[*]MYSQL_RES *results2;
[*]if(randid<2)//老数据,小部分 20%
[*]{
[*]r2 = random(700000);
[*]sprintf(sql, "SELECT COUNT(*) AS count FROM posts_jx3 WHERE tid = %d;",r2);
[*]lr_log_message("%s",sql);
[*] mysql_query(db, sql);
[*] results2 = mysql_store_result(db);
[*] while((record = mysql_fetch_row(results2)))
[*]{
[*] lr_log_message("count = %d", record);
[*] }
[*]mysql_free_result(results2);
[*]page = count/10;
[*]lr_log_message("page = %d", page);
[*]for(j=0;j< =page;j++)
[*]{
[*]sprintf(sql, "SELECT *FROM posts_jx3 WHERE tid = %d LIMIT %d,10;",r2,j);
[*]mysql_query(db, sql);
[*]lr_log_message("%s", sql);
[*]if (j>0) {
[*]results2 = mysql_store_result(db);
[*] while((record = mysql_fetch_row(results2)))
[*] {
[*] lr_log_message("pid = %d", record);
[*] }
[*] mysql_free_result(results2);
[*]}
[*]}
[*]}
[*]else //新数据,大部分 80%
[*]{
[*] r2 = random(273505);
[*]r2 = r2+700000;
[*]sprintf(sql, "SELECT COUNT(*) AS count FROM posts_jx3 WHERE tid = %d;",r2);
[*]lr_log_message("%s",sql);
[*] mysql_query(db, sql);
[*] results2 = mysql_store_result(db);
[*] while((record = mysql_fetch_row(results2)))
[*]{
[*]lr_log_message("count = %d", record);
[*] }
[*]mysql_free_result(results2);
[*]page = count/10;
[*]lr_log_message("page = %d", page);
[*]
[*]for(j=0;j< =page;j++)
[*]{
[*]sprintf(sql, "SELECT *FROM posts_jx3 WHERE tid = %d LIMIT %d,10;",r2,j);
[*]lr_log_message("%s", sql);
[*]mysql_query(db, sql);
[*]if (j>0) {
[*]results2 = mysql_store_result(db);
[*] while((record = mysql_fetch_row(results2)))
[*] {
[*] lr_log_message("pid = %d", record);
[*] }
[*] mysql_free_result(results2);
[*]}
[*]}
[*]}
[*]mysql_free_result(results2);
[*]return 0;
[*]}
[*]
[*]
[*]
[*]int my_mysql_insert()
[*]{
[*]int t;
[*] char sql;
[*]MYSQL_RES *results3;
[*]sprintf(sql,"%s","INSERT INTO posts_jx3 ( fid, tid, first, author, authorid, subject, dateline, message, useip, invisible, anonymous, usesig, htmlon, bbcodeoff, smileyoff, parseurloff, attachment, rate, ratetimes, status) VALUES ( 12345, 215053, 0, 'fdsafsdfsd', 2265065, '', 1240535917, 'fdsfsdfdsfd', '118.239.49.229', 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0);");
[*]lr_log_message("%s", sql);
[*] t = mysql_query(db, sql);
[*]if(t)
[*]{
[*]lr_log_message("%s", mysql_error(db));
[*]}
[*]else
[*]{
[*]lr_log_message("%s", "suc");
[*]}
[*]}
关注,感谢分享。 感谢分享 是在自己录制的代码中添加这段代码?? 怎么弄呢?没写过代码
页:
[1]