未来已来

LoadRunner MySql 自定义监控器开发

上一篇 / 下一篇  2008-10-01 20:05:34 / 个人分类:性能测试

 

    来深圳3个多月,每天工作都很紧张,好不容易等到十一放假,正好把以前答应朋友和学员提出的问题解答下。记得上次有人问过如何监控MySql,我提供了一种SiteScope和Lr整合的办法,最近在网络上搜索下发现我那篇文章blog网上有很多转载,那么另外一种监控mysql的方法怎么处理呢?

    首先确认mysql需要监控的性能指标,然后确认如何采集这些性能数据。这里我以采集hits值为例子,第一步利用vc开发dll,然后在lr中开发监控器,这里我不是写教程,所以很多细节就不罗嗦了(以下脚本代码在winxp sp2 vc 6.0 lr9.1中调试)

   vc 6.0 dll中的代码:

// mysql_dll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "stdlib.h"

MYSQL *conn=NULL;
MYSQL_RES *p_res_ptr=NULL;
MYSQL_ROW sqlrows;


BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    return TRUE;
}

extern "C" int _declspec(dllexport) init_mysql_connection(char *str_server,char *str_username,char *str_pwd,char *str_Table)
{
 conn=mysql_init(NULL);

    if(!conn)
 {
  printf("\nFailed to initate MySQL connection");
  return 1;
  exit(0);
 }
    else
 {
  printf("\nSuccess to initate MySQL connection");
  if (!mysql_real_connect(conn,str_server,str_username,str_pwd,str_Table,0,NULL,0))
  {
   printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(conn));
  }
  else
  {
   printf("\nLogged on to %s sucessfully",str_server);
   return 0;
  }
  return 0;
 }
}

extern "C" int _declspec(dllexport) close_mysql_connection()
{
 if(conn=NULL)
 {
  printf("\nConnection is Null");
  return 1;
  exit(0);
 }
 else
 {
     mysql_free_result(p_res_ptr);
  printf("\nClose connection");
  mysql_close(conn);   
  return 0;
 }
}

//"show status like \'qcache%\'"

extern "C" int _declspec(dllexport) get_mysql_table_query(char *str_query)
{
 int res=0;
 res=mysql_query(conn,str_query);
 if(res)
 {
  printf("Failed to mysql query: Error: %s\n", mysql_error(conn));
  return 1;
 }
 else
 {
  printf("\nSucess in Mysql Query");
  return 0;

 }

}

 

extern "C" int _declspec(dllexport) get_mysql_query_data(char *str_query,char *str_data)
{
    unsigned long u1_numrow=0;
    unsigned int i_index = 0;
 p_res_ptr=mysql_use_result(conn);

 if(p_res_ptr){
 
  while((sqlrows=mysql_fetch_row(p_res_ptr))){
  
   if(*sqlrows[0]=*str_query)
   {
    strcpy(str_data,sqlrows[1]);
   
    
   }
  }
 }

 return NULL;

}

 

lr 9.1中代码:

Action()
{

        int i=0; 
        double x;
        char *str_data;
 
 
        str_data=(char *)malloc(20*sizeof(char));
        lr_load_dll("D:\\vc\\mysql_dll\\Debug\\mysql_dll.dll"); 
        i= init_mysql_connection("localhost","root","123456","mysql");
        lr_output_message("%d",i);
    
       for(;;)
       {
            get_mysql_query_data("Qcache_hits",str_data);
            i=get_mysql_table_query("show status like \'qcache%\'");
            lr_output_message("%d",i);
            x = atof(str_data);
            lr_user_data_point("hits",x);
            lr_think_time(5);
       }
        lr_output_message("%d",x);
     close_mysql_connection();
 return 0;
}
 
   

TAG: 性能测试

xinqidian123的个人空间 引用 删除 xinqidian123   /   2010-10-15 09:53:26
我是复制的源码啊?怎么编译通不过?
飘哥 引用 删除 pcl2004_27   /   2008-10-04 00:51:44
其实想过要写的详细点,写一篇文章关于mysql c api以及性能分析的mysql计数器,还有mysql分析方面的东西,不过时间很有限,这里就把代码贴出来,给那些需要的人,做个研究
自动化测试 引用 删除 chenyunjun169   /   2008-10-03 23:43:09
我觉得朴老师要是把每个函数的作用都加以说明,效果会更好一些,这样代码也会更加规范一些。
 

评分:0

我来说两句

Open Toolbar