(转自:http://www.cnblogs.com/DebugLZQ/archive/2012/07/10/2585245.html)

这篇博文给大家分享的是,如何使用VS自带的性能分析工具来分析我们编写的.NET程序,一边找出程序性能的瓶颈,改善代码的质量。在实际开发中,性能真的很重要,往往决定一个产品的生死~良好的用户体验的基础之一也是程序要有好的性能~

下面以一个大家熟悉比较极端的例子,来说明编写代码时考虑性能的重要性。这里DebugLZQ用的是10.0版本的VS。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace VS2010性能测试
{
class Program
{
static void Main(string[] args)
{
int start = Environment.TickCount;
for (int i = ; i < ; i++)
{
string s = "";
for (int j = ; j <; j++)
{
s += "Outer index = ";
s += i;
s += " Inner index = ";
s += j;
s += " ";
}
}
int middle = Environment.TickCount;
Console.WriteLine("Program part1 run for {0} seconds",0.001 * (middle - start));
//
for (int i = ; i < ; i++)
{
StringBuilder s = new StringBuilder();
for (int j = ; j <; j++)
{
s.Append("Outer index = ");
s.Append(i);
s.Append("Inner index = ");
s.Append(j);
s.Append(" ");
}
}
int end = Environment.TickCount;
Console.WriteLine("Program part2 run for {0} seconds", 0.001 * (end - middle)); //
Console.ReadKey();
}
}
}

差距就是这么大!

我们可以使用VS自带的性能分析工具来分析这个程序。可以通过“分析”--“启动性能向导”来启动性能分析

我们可以根据需要选择不同的分析方法

面以“CPU采样”分析为例

切换到函数视图

定位到我们的源码:

问题找到了~

就像剥去.NET语法糖衣的工具(Reflector等)很多一样,我们可以用来分析.NET程序性能的工具有很多,如前面一片博文DebugLZQ给大家介绍的vs自带的性能分析工具,除此之外常用的还有还有clr profiler、Windbg等。

  vs自带的性能分析可以很快的找到瓶颈代码,而且支持多线程。

  Windbg就不多说了,Windows平台下强大的用户态和内核态调试工具!虽然windbg也提供图形界面操作,但它最强大的地方还是有着强大的调试命令,用起来比较费劲。

  这里主要要说的是CLR Profile了,他检测结果最为详细,不过由于检测托管堆分配和垃圾回收会影响应用程序的运行速度,因此无法得之时间上的性能测试。

CLR Profiler简介

CLR Profiler 是用来观察托管堆内存分配和研究垃圾回收行为的一种工具。使用该工具中不同的视图,你能获得关于你运用程序的执行、内存的分配和消耗等有用信息。CLR Profiler分析的结果存放在日志文件中,常用的几种视图如下:

View Description
Histogram Allocated Types Gives you a high-level view of what object types are allocated (by allocation size) during the lifetime of your application. This view also shows those objects that are allocated in the large object heap (objects larger than 85 KB).

This view allows you to click parts of the graph so that you can see which methods allocated which objects.

Histogram Relocated Types Displays the objects that the garbage collector has moved because they have survived a garbage collection.
Objects By Address Provides a picture of what is on the managed heap at a given time.
Histogram By Age Allows you to see the lifetime of the objects on the managed heap.
Allocation Graph Graphically displays the call stack for how objects were allocated. You can use this view to:

-See the cost of each allocation by method.

-Isolate allocations that you were not expecting.

-View possible excessive allocations by a method.

Assembly, Module, Function, and Class Graph These four views are very similar. They allow you to see which methods pulled in which assemblies, functions, modules, or classes.
Heap Graph Shows you all of the objects in the managed heap, along with their connections.
Call Graph Lets you see which methods call which other methods and how frequently.

You can use this graph to get a feel for the cost of library calls and to determine how many calls are made to methods and which methods are called.

Time Line Displays what the garbage collector does over the lifetime of the application. Use this view to:

-Investigate the behavior of the garbage collector.

-Determine how many garbage collections occur at the three generations (Generation 0, 1, and 2) and how frequently they occur.

-Determine which objects survive garbage collection and are promoted to the next generation.

You can select time points or intervals and right-click to show who allocated memory in the interval.

Call Tree View Provides a text-based, chronological, hierarchical view of your application's execution. Use this view to:

-See what types are allocated and their size.

-See which assemblies are loaded as result of method calls.

-Analyze the use of finalizers, including the number of finalizers executed.

-Identify methods where Close or Dispose has not been implemented or called, thereby causing a bottleneck.

-Analyze allocations that you were not expecting.

下面还是以前面给出的代码为例,来介绍各种功能。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace VS2010性能测试
{
class Program
{
static void Main(string[] args)
{
int start = Environment.TickCount;
for (int i = ; i < ; i++)
{
string s = "";
for (int j = ; j <; j++)
{
s += "Outer index = ";
s += i;
s += " Inner index = ";
s += j;
s += " ";
}
}
int middle = Environment.TickCount;
Console.WriteLine("Program part1 run for {0} seconds",0.001 * (middle - start));
//
for (int i = ; i < ; i++)
{
StringBuilder s = new StringBuilder();
for (int j = ; j <; j++)
{
s.Append("Outer index = ");
s.Append(i);
s.Append("Inner index = ");
s.Append(j);
s.Append(" ");
}
}
int end = Environment.TickCount;
Console.WriteLine("Program part2 run for {0} seconds", 0.001 * (end - middle)); //
Console.ReadKey();
}
}
}

CLR Profiler程序的运行界面如下:

通过start application 选择需要运行的程序,可以选择是否跟踪内存分配和方法调用。当关闭应用程序(可以自动或手动),Profiler自动开始整理结果。分析结果存放在日志文件中,显示如下:

报告统计界面如下:

Heap statistics 堆栈统计信息:DebugLZQ的这个测试程序需要分配6.6GB的内存!你有想到过吗?

Allocation Graph:用图表显示堆栈的分配情况

Allocated bytes:应用程序整个启动周期内分配的对象。按照对象大小排列,不同的颜色代码不同的对象,在右侧会列出。以下图为例,红色的是String对象。

Relocated bytes:GC时被对象在托管堆中的位置被移动过的。不同的颜色代表不同的对象。

(简要介绍下GC过程,大概分两步:有具体算法判断哪些对象成为了垃圾(即根据根引用列表遍历列表引用所指向的对象,不能被遍历的对象);移动堆中的不为垃圾的对象)

Final Heap bytes:最终还在堆中的。颜色代表种类。

Garbage Collection Statistics :GC统计信息。总共进行了4501次0代垃圾回收!

Time视图如下;从图中可以清晰的看出各次回收时间和前后内存占用量(总共4501次)。

GC Handle: 统计GC句柄数

具体细节如下:

就介绍到这里吧。

更为详细的信息,请阅读CLR Profiler 108页的详细说明,这个文档就在你释放出来的文件的根目录下,名称是CLRProfiler.doc。

使用VS自带的工具分析.NET程序的性能的更多相关文章

  1. VS2010自带的性能分析工具分析.NET程序的性能

    这篇博文给大家分享的是,如何使用VS自带的性能分析工具来分析我们编写的.NET程序,一边找出程序性能的瓶颈,改善代码的质量.在实际开发中,性能真的很重要,往往决定一个产品的生死~良好的用户体验的基础之 ...

  2. 从系统的角度分析影响程序执行性能的因素——SA20225205 黄兴宇

    实验总结分析报告:从系统的角度分析影响程序执行性能的因素 1.请您根据本课程所学内容总结梳理出一个精简的Linux系统概念模型,最大程度统摄整顿本课程及相关的知识信息,模型应该是逻辑上可以运转的.自洽 ...

  3. 使用JDK自带的VisualVM进行Java程序的性能分析

    VisualVM是什么? VisualVM是JDK自带的一个用于Java程序性能分析的工具,JDK安装完毕后就有啦,在JDK安装目录的bin文件夹下能找到名称为jvisualvm.exe. 要使用Vi ...

  4. 用jstack工具分析java程序

    最近做项目时遇到了一个问题,我的多个采集线程中,有一个线程经常挂起,线程并没有死掉,但是一直采集不到数据,为了解决这个问题,用到了jstack. 首先查找到java进程的pid,ps -ef|grep ...

  5. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-可以用软件自带NC工具驱动但是程序无法让电机转动怎么办

    新建一个项目,当扫描的时候务必勾选YES,使用网上最新的XML文件   如果不使用,则有些设备可能被扫描出来是无效的(图标不正常)   如果完全删除XML描述文件,可能也能扫描出来,而且可以用Twin ...

  6. Python程序的性能分析指南(转)

    原文地址 :http://blog.jobbole.com/47619/ 虽然不是所有的Python程序都需要严格的性能分析,不过知道如何利用Python生态圈里的工具来分析性能,也是不错的. 分析一 ...

  7. 转:LoadRunner自带的协议分析工具

    在做性能测试的时候,协议分析是困扰初学者的难题,不过优秀的第三方协议分析工具还是挺多的,如:MiniSniffer .Wireshark .Ominpeek 等:当然他们除了帮你分析协议之外,还提供其 ...

  8. JDK中自带的JVM分析工具

    目录 一.业务背景 二.Jdk-Bin目录 三.命令行工具 1.jps命令 2.jinfo命令 3.jstat命令 4.jstack命令 5.jmap命令 四.可视化工具 1.jconsole 2.v ...

  9. java自带的jvm分析工具

    http://domark.iteye.com/blog/1924302   这段时间觉得很有必要对java的内存分析工具进行熟悉,这样以后出现机器负载较高,或者反应很慢的时候,我就可以查找原因了.上 ...

随机推荐

  1. Linux(1)- 服务器核心知识、Linux入门、VMware与centeos安装、远程连接linux、linux基本命令使用

    一.服务器核心知识 1.电脑和电脑的硬件组成 现在的人们几乎无时无刻不在使用着电脑!不管是桌上型电脑(桌机).笔记型电脑(笔电).平板电脑,还是智慧型手机等等,这些东西都算是电脑.虽然接触这么多,但是 ...

  2. Sql server用QQ邮箱发送邮件

    一.配置数据库邮件 https://jingyan.baidu.com/article/3ea51489a135f752e71bba5b.html

  3. win7开启特定端口

    win7开启特定端口        在xp系统的时代,修改防火墙很方便,很简单.windows7或许是做得过于复杂了.当然所谓安全性也是相当于其他之前版本的系统更高了.为什么要打开端口,肯定是在win ...

  4. 怎么找出解析失败的sql

    本文由我和公司同事问心共同测试分析完成. 很多时候我们会有这样一个误区,语法错误或者对象不存在应该在语法语义检查这个步骤就结束了,怎么还会存在共享池里面呢?带着这个几个问题我们做几个简单的测试. 我们 ...

  5. python16_day11【MQ、Redis、Memcache】

    一.RabbitMQ 是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(MQ)是一种应 ...

  6. java 创建包含枚举的常量类

    参考 public class Constants { public static enum ServiceStatus{ NORMAL(1,"正常办理"),CHANGEING(2 ...

  7. 什么是JIT?

    JIT是just in time,即时编译技术.使用该技术,能够加速java程序的执行速度.下面,就对该技术做个简单的讲解. 首先,我们大家都知道,通常javac将程序源代码编译,转换成java字节码 ...

  8. mybatis入门学习记录(一)

    过硬的技术本领,可以给我们保驾护航,飞得更高.今天开始呢.我们就一起来探讨使用mybatis的好处. 首先我们一起来先看看原生的JDBC对于数据库的操作,然后总结其中的利弊,为学习mybatis奠定基 ...

  9. netty4----netty5的客户端和服务端

    服务端: package com.server; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; ...

  10. Python单元测试框架——unittest

    测试的常用规则 一个测试单元必须关注一个很小的功能函数,证明它是正确的: 每个测试单元必须是完全独立的,必须能单独运行.这样意味着每一个测试方法必须重新加载数据,执行完毕后做一些清理工作.通常通过se ...