ASP.NET Web 应用程序上的代码检测

发布于 2024-07-29 17:48:32 字数 318 浏览 4 评论 0原文

总的来说,我对 .NET 开发还很陌生。 我想在我的 Web 应用程序上进行一些检测来调整性能,特别是与缓存相关的性能。 我已经编写了很多自定义动态生成的 UserControls,我想尝试以不同的方式进行缓存 - 可能通过 ASPX 页面声明或以编程方式。

我还有很多相互依赖的 Oracle 查询,我想研究这些查询的缓存结果,看看什么可以提供最佳的性能提升。

解决这个问题的最佳方法是什么? 不知何故,我不认为使用秒表来观察 IE 加载页面所需的时间是最好的主意。 除了感知到的延迟之外,我不知道我的缓存是否被命中或错过。 VS2008有内置的工具来辅助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

西瓜 2024-08-05 17:48:32

我最近从事的一个项目需要检查 SQL 查询的时间,并在调试模式下将它们输出到调试侦听器。 通过这种方式,我们可以评估 SQL 查询的时间和执行时间,以及调试我们的网站代码。

我通过将 SQL 查询集中到我们使用的 3 种 SQL 方法的包装器方法中来实现此目的:

  • ExecuteQuery
  • ExecuteNonQuery
  • ExecuteScalar

他们确实使用了 Stopwatch 类,但也有代码将查询转换为 SQL 语句,与所见类似在 SQL Server 事件探查器中。

每种方法都类似于以下内容: 然后,

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");
         }
    } 
}

这将在 DebugView.exe 中输出我们的 SQL 语句,如下所示:

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

这样做的好处是,尽管这些语句存在瓶颈,但我们可以将查询直接粘贴到 SQL 分析器中并获取查询的输出。 这也意味着,如果您有查看日志文件的工具,则可以使用正则表达式来监视所用时间在某个范围内的位置。

因此,如果您想查找耗时超过 0.5 秒的查询,您可以搜索术语:

“in 0.[5-9]\d+” <-- 大于 0.5 秒的所有内容
或者
"in [1-9].\d+" <-- 一切大于 1 秒的内容

这帮助我们极大地集中精力。 它还可以帮助我们确定问题是否与数据库相关,或者如上所述,是 ASP.NET 问题。

最后,有一个名为 Fiddler 的工具,它还可以帮助您在页面进入您的计算机时对其进行诊断。 这为您提供了文件大小、图像/CSS 引用、下载时间等信息。 这对于诊断 ViewState 大小问题也非常有用。

我希望这有帮助

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

风吹雨成花 2024-08-05 17:48:32

我通常解决此问题的方法是启用 ASP.NET 跟踪。 这里有一个非常好的启用指南。 它非常详细,而且最重要的是它是免费的。

需要关注的一件事是页面的大小(尤其是其视图状态),这是代码执行后页面下载所需时间的重要组成部分。 另一个是代码某些部分的渲染速度,这可以通过调用 Trace.Write 在代码进行性能测试之前和之后。

使用 ASP.NET 跟踪的性能测试部分实际上只是一个指南,因为它不容易重复,但仍然是一个很好的指南。 如果您想更进一步,Visual Studio 2008 Team System 中的工具可能会更好(尽管我没有使用过这些工具)。 还有 JetBrains dotTraceANTS 分析器

The way I usually approach this is by enabling ASP.NET tracing. There is a pretty good guide to enabling this here. It's quite detailed and best of all it's free.

One thing to focus on is the size of the page (especially its view state) which is an important part of how long the page takes to download once the code has executed. The other is how fast certain parts of the code render which can be achieved by making calls to Trace.Write before and after the code being perf tested.

The perf testing part of using ASP.NET tracing is really just a guide as it's not easily repeatable, but a good one none-the-less. If you want to step it up, there are tools in Visual Studio 2008 Team System that might be better (although I haven't used those). There's also JetBrains dotTrace and ANTS Profiler.

烈酒灼喉 2024-08-05 17:48:32

AppDynamics 是一个非常好的应用程序性能监视器,无需配置即可检测您的 .NET 应用程序。 我认为该工具可以解决您的问题。 http://www.appdynamics.com/

AppDynamics is a very good application performance monitor, that can instrumentation your .NET applications with no configuration. I think will could sove your problem with that tool. http://www.appdynamics.com/

む无字情书 2024-08-05 17:48:32

我在 New Relic 方面获得了非常好的体验。 安装非常简单,其网站上的仪表板极其清晰,如果您碰巧在 Rackspace 或亚马逊云上托管,他们通常会提供专业帐户的免费升级。 检查一下!

I've had very good experiences with New Relic. Very simple install, extremely clear dashboard on their site, and if you happen to host on the Rackspace or Amazon Clouds they usually offer free upgrades to Pro accounts. Check 'em out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文