在ASP.NET Web应用程序code仪表 [英] Code Instrumentation on an ASP.NET Web Application

查看:108
本文介绍了在ASP.NET Web应用程序code仪表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般是相当新的.NET开发。我愿做一些仪器对我的web应用程序调整性能,特别是关系到缓存。我已经写了很多定制的动态生成的用户控件我想尝试用不同的方式缓存哪些 - 可能由ASPX页面声明或编程

我也有很多的Oracle查询依赖于对方,我想看看这些缓存结果,看看究竟会提供最佳的性能提升。

什么是去了解这一点的最好方法是什么?不知怎的,我不认为用秒表看,看久IE浏览器是如何进行的页面加载是最好的主意。我不知道如果我的缓存正从感知延迟击中或错过了一边。 VS2008是否有内置的工具来帮助?


解决方案

一个项目,我最近就需要检查我们的SQL查询的计时工作,并将其输出到在调试模式下调试监听器。通过这种方式,我们可以评估SQL查询的计时和他们花了多长时间来执行,以及调试我们的网站code。

我通过集中了SQL查询到一个包装方法的3种类型的SQL方法,我们用这个做


  • 的executeQuery

  • 的ExecuteNonQuery

  • 的ExecuteScalar

他们没有用秒表类,但也有code将查询转换成SQL语句,类似于SQL Server Profiler中看到。

每个方法类似于以下内容:

 受保护的静态对象的ExecuteScalar(COMM的SqlCommand,SqlParameter的[] sqlParameters)
{
    秒表ST =新的秒表();
    布尔errorDetected = FALSE;    尝试
    {
         comm.Parameters.Add(sqlParameters);         使用(通讯)
         {
              st.Start();
              对象的returnValue = comm.ExecuteScalar();
              st.Stop();
              返回的returnValue;
         }
    }
    赶上(例外)
    {
         errorDetected = TRUE;
         st.Stop();
         扔;
    }
    最后
    {
         字符串输出= GetSqlStringForParameters(sqlParameters,st.Elapsed,QueryType.Scalar);         如果(errorDetected)
         {
              的Debug.WriteLine(/ * SQL(误码)* /+输出DataAccess.SqlAdapter.ExecuteScalar);
         }
         其他
         {
              的Debug.WriteLine(/ * SQL * /+输出DataAccess.SqlAdapter.ExecuteScalar);
         }
    }
}

这会再输出DebugView.exe我们的SQL语句如下:

  / * SQL * / EXEC spsGetOrder @订单编号='234567' - 行返回= 1; 1 PARAMS |  - >完成NonQuery在0.0016144秒。

这样做的好处是,虽然,是有这些语句的一个瓶颈,我们可以直接粘贴到查询的SQL事件探查器,并得到查询的输出。这也意味着,如果你要查看的日志文件的工具,你可以使用普通的前pressions来监控拍摄的时间是一定范围内的。

所以,如果你想寻找以超过0.5秒则查询,您可以搜索术语:

在0 [5-9] \\ D +< - 所有超过0.5秒更大
要么
在[1-9] \\ D +< - 一切都超过1秒更大

这已经帮助我们极大地关注我们的努力。它还可以帮助我们确定如果问题是数据库相关的,或上述规定,一个ASP.NET问题。

最后,有一个叫做工具提琴手这也可以帮助你诊断页面,因为他们来到您的计算机。这给你喜欢的文件大小,将图像/ CSS参考,下载时间的信息。这也可作为诊断的ViewState大小的问题非常有用的。

我希望这有助于

I'm fairly new to .NET development in general. I would like to do some instrumentation on my web application to tune performance, especially in relation to caching. I've written a lot of custom dynamically generated UserControls which I'd like to try caching in different ways--possibly by ASPX Page declaration or programmatically.

I also have a lot of Oracle queries that depend on each other and I'd like to look into caching results of those to see what will offer the best performance gains.

What would be the best way to go about this? Somehow I don't think using a stopwatch watching to see how long IE takes the page to load is the best idea. I'll have no idea if my caching is getting hit or missed aside from perceived delay. Does VS2008 have tools built in to assist?

解决方案

A project I worked on recently needed to check the timings of our SQL queries, and output them to the Debug listener in Debug mode. This way we could assess the timings of the SQL queries and how long they took to execute, as well as debug our website code.

I did this by centralising out SQL queries into a wrapper method for the 3 types of SQL methods we used:

  • ExecuteQuery
  • ExecuteNonQuery
  • ExecuteScalar

They did use the Stopwatch class, but also there was code to transform the query into a SQL statement, similar to that seen in SQL Server Profiler.

Each method was similar to the following:

protected static object ExecuteScalar(SqlCommand comm, SqlParameter[] sqlParameters)
{
    Stopwatch st = new Stopwatch();
    bool errorDetected = false;

    try
    {
         comm.Parameters.Add(sqlParameters);

         using(comm)
         {
              st.Start();
              object returnValue = comm.ExecuteScalar();
              st.Stop();
              return returnValue;
         }
    }
    catch(Exception)
    {
         errorDetected = true;
         st.Stop();
         throw;
    }
    finally
    {
         string output = GetSqlStringForParameters(sqlParameters,st.Elapsed,QueryType.Scalar);

         if(errorDetected)
         {
              Debug.WriteLine("/*SQL (Errored)*/" + output,"DataAccess.SqlAdapter.ExecuteScalar");
         }
         else
         {
              Debug.WriteLine("/*SQL*/" + output,"DataAccess.SqlAdapter.ExecuteScalar");
         }
    } 
}

This would then output our SQL statements in DebugView.exe like this:

/*SQL*/ exec spsGetOrder @OrderNumber='234567' -- Rows returned = 1 ;  1 params |--> completed NonQuery in 0.0016144 seconds.

The beauty of this is that although, yes there is a bottleneck for these statements, we can paste the query directly into SQL profiler and get the output of the query. It also means that if you have a tool to view the log files, you can use regular expressions to monitor where the time taken is of a certain range.

So if you want to look for queries taking over 0.5 seconds, you could search for the term:

"in 0.[5-9]\d+" <-- Everything bigger than 0.5 seconds or "in [1-9].\d+" <-- Everything bigger than 1 second

This has helped us focus our efforts immensely. It also helps us with identifying if a problem is database related, or as provided above, an ASP.NET issue.

Finally, there is a tool called Fiddler which can also help you diagnose pages as they come to your computer. This gives you information like file size, references to images/css, download times. This is also very useful as diagnosing ViewState size issues.

I hope this helps

这篇关于在ASP.NET Web应用程序code仪表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆