fiddler自动保存mysql_用Fiddler 和 SQL Server Profiler 测试数据库瓶颈 | 学步园

某国外论坛的文章,先收藏以后看有空再考虑翻译

话说如果熟悉这2个工具直接看没啥问题吧~~~

How to Use Fiddler and SQL Server Profiler to Look for Database Performance Bottlenecks

Posted on 2/4/2009 12:47:35 PM in #.NET

Here I want to describe the steps you can use to search for database performance bottlenecks before you deploy your web application to production server.

1. Modify Fiddler Rules

We want to add to Fiddler the option that would allow us to execute a sequence of requests and then tell it to repeat the same sequence N number of times.

First of all if you don’t have Fiddler yet, install the latest version from http://www.fiddlertool.com/Fiddler2/version.asp then start it and go to Rules - Customize Rules…

Scroll to the point where it says static function OnExecAction(sParams: String[]){ and add there the following code right after switch (sAction){

// Custom code starts here

case "ptest":

if (sParams.Length<3) {

FiddlerObject.alert(

"USE AS: ptest URL_STARTS_WITH NUMBER_REPETITIONS for example ptest loc:8080 10");

return;

}

var server = sParams[1];

var repetitions = parseInt(sParams[2]);

var oSessions = FiddlerObject.UI.GetAllSessions()

var num = 0;

for (var r = 0; r < repetitions; r++){

for (var x = 0; x < oSessions.length; x++){

if(oSessions[x].url.ToLower().StartsWith(server)){

var ct = oSessions[x].oResponse["Content-Type"];

if(ct == "text/html; charset=utf-8" ||

ct == "text/xml; charset=utf-8" ||

ct == "text/css; charset=utf-8"){

FiddlerObject.utilIssueRequest(

oSessions[x].oRequest.headers.ToString(true, true) +

System.Text.Encoding.UTF8.GetString(oSessions[x].requestBodyBytes)

);

num++;

}

}

}

}

FiddlerObject.StatusText="ptest was completed #executed requests: "+num

break;

// Custom code ends here

2. Prepare SQL Server Profiler Trace

Now start the Sql Server Profiler (Start – All Programs – Microsoft SQL Server 2008 or 2005 – Performance Tools – Sql Server Profiler). Then choose File – New Trace… Click on the Event Selection tab and unselect all the events except RPC:Completed and SQL:BatchCompleted then select the following columns TextData, CPU, Duration, ClientProcessID, SPID, DatabaseName, ObjectName and unselect the others. Of course you can also look at other parameters I just show you here the basic once. You may also want to define column filter if you want to avoid traces from calls from other clients. For example I have a specific database that only I use for testing so I define that to be a filter for DatabaseName LIKE 'MyName%', you can also use the ClientProcessID column.

3. Perform a sequence of sample user actions

Open a web browser that is detectable by Fiddler. In the Fiddler window choose Tools – Clear WinINET Cache and Tools – Clear WinINET Cookies. Then press Ctrl – X to remove the existing sessions in Fiddler. Now use your browser to perform the sequence of test actions. If your site is located at localhost here is a tip how to make it visible with Fiddler http://www.coderjournal.com/2008/03/localhost-http-debugging-with-fiddler/

4. Run ptest command to simulate multiple site visitors.

Now click on the command line text field of Fiddler. You can find it at the bottom of the left side – a black line. Type ptest MYSITE 50 where MYSITE will be your web site (for example ptest loc:8080 50) and you will execute the sequence of test steps 50 times

5. Stop and save SQL Trace.

Wait for the executions to complete and then go back to SQL Server Profiler and choose File – Stop Trace. Now save it by File – Save As… - Trace Table. I have a test database where I store all the tables for future reference. Give the table a meaningful name, for example TestFeb042009_01

6. Query the test table to find of your bottlenecks.

Now you can execute the following database queries against the database where you stored your test table. The first one shows what queries use most CPU:

SELECT

CAST(TextData as VARCHAR(8000)),

DatabaseName,

SUM(CPU) AgrCpu,

COUNT(*) TimesCalled

FROM TestFeb042009_01

WHERE CPU IS NOT NULL

GROUP BY CAST(TextData as VARCHAR(8000)), DatabaseName

ORDER BY AgrCpu DESC

The second shows what queries take more time to execute:

SELECT

CAST(TextData as VARCHAR(8000)),

DatabaseName,

SUM(Duration) AgrDuration,

COUNT(*) TimesCalled

FROM TestFeb042009_01

WHERE Duration IS NOT NULL

GROUP BY CAST(TextData as VARCHAR(8000)), DatabaseName

ORDER BY AgrDuration DESC

You may notice that I use the sum of CPUs or duration and not the average value. This is because my concern is with general performance footprint of a particular query and a specific query performance. If you have a query that takes 100 ms but runs only ones during the normal workflow in your site it is not as bad as a query that takes 50 ms but runs 100 times.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值