Sample

This tutorial will show how to instrument a unit test DLL for performance profiling. Visual Studio will allow you to do performance profiling on individual tests (i.e. functions) in a test suite. This can be done in the user interface (i.e. UI or IDE). However if you have a unit test DLL that contains lots of unit tests or test methods, it’s apparently not possible to do performance profiling in the IDE.

Here was my original question:

Question on MSDN forums

I have a MS Unit Test DLL written in C# that targets a C++/CLI managed assembly. I have roughly 60 unit tests in that unit test dll. What I would like to do is run all the unit tests under the performance profiler, in the IDE. That is using the performance explorer to run a performance session for all the unit tests in my test DLL. I know how to do this from the command line, but I feel that is a last resort. I’d like to do this in the IDE if possible. It is possible to create a performance session for one unit test in the unit test DLL. The steps are listed here: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vsetlt01/html/4cc514d2-47bd-4f1c-a039-5ffae7c68bf1.htm i.e. Right click over a test result, and select "Create Performance Session". Whereupon the Performance Explorer will show a new session based off of one unit test. That’s great, but I’ve got 60 unit tests in my DLL. I don’t want to have to create 60 performance sessions.

Preparation

Build the code that you will be testing. In my case it was the following DLL’s (Assuming a common root folder)

DLL name

Description

..\ManagedAPI\Win32\Debug\UnderstandWrapper.ManagedAPI.dl The DLL with the code I wanted to test
..\UnitTest\bin\x86\Debug\UnitTest.dll This DLL contains my unit tests

Then just to play it safe, run all the unit tests (This can be done in the IDE) in UnitTest.dll. It pays to be paranoid like this since if you want to detect bugs early and swiftly.

Now the strategy for profiling an entire DLL is to:

  • Instrument the DLL. This means to inject performance related commands into the code. Don’t worry it’s perfectly safe.

    • Turn on performance profiling via a global command.

      • Run your unit tests. Here is where the real work gets done.
    • Turn off performance profiling via a global command. (A results file gets saved somewhere).
  • Un-Instrument the DLL. This means restoring it to it’s original state. (i.e. Don’t ship a DLL with performance profiling enabled).

Instrument the Binaries

I instrument my DLL’s using the following batch script called: Instrument_ON.bat

  1. @echo off
  2. @echo Instrumenting Binary
  3. set VS90TEAMTOOLS="%VS90COMNTOOLS%..\..\Team Tools\Performance Tools\"
  4. %VS90TEAMTOOLS%\VSInstr.exe ..\ManagedAPI\Win32\Debug\UnderstandWrapper.ManagedAPI.dll
  5. %VS90TEAMTOOLS%\VSInstr.exe ..\UnitTest\bin\x86\Debug\UnitTest.dll
  6. pause
  7. @echo on

The results of instrumenting it looks like this:

Instrumenting Binary      Microsoft (R) VSInstr Post-Link Instrumentation 9.0.30729 x86       Copyright (C) Microsoft Corp. All rights reserved.

File to Process:         F:\CodePlex\UnderstandAPI\Managed\ManagedAPI\Win32\Debug\UnderstandWrapper.ManagedAPI.dll –> F:\CodePlex\UnderstandAPI\Managed\ManagedAPI\Win32\De       bug\UnderstandWrapper.ManagedAPI.dll       Original file backed up to F:\CodePlex\UnderstandAPI\Managed\ManagedAPI\Win32\Debug\UnderstandWrapper.ManagedAPI.dll.orig

Successfully instrumented file F:\CodePlex\UnderstandAPI\Managed\ManagedAPI\Win32\Debug\UnderstandWrapper.ManagedAPI.dll.      Warning VSP2013 : Instrumenting this image requires it to run as a 32-bit process.  The CLR header flags have been updated to reflect this.       Microsoft (R) VSInstr Post-Link Instrumentation 9.0.30729 x86       Copyright (C) Microsoft Corp. All rights reserved.

File to Process:         F:\CodePlex\UnderstandAPI\Managed\UnitTest\bin\x86\Debug\UnitTest.dll –> F:\CodePlex\UnderstandAPI\Managed\UnitTest\bin\x86\Debug\UnitTest.dll       Original file backed up to F:\CodePlex\UnderstandAPI\Managed\UnitTest\bin\x86\Debug\UnitTest.dll.orig

Successfully instrumented file F:\CodePlex\UnderstandAPI\Managed\UnitTest\bin\x86\Debug\UnitTest.dll.      Press any key to continue . . .

Turn on Monitor

I then turn on performance profiling using the following batch script called: Performance_ON.bat       

  1. @echo off
  2. @echo Turning ON performance coverage session recorder
  3. set VS90TEAMTOOLS="%VS90COMNTOOLS%..\..\Team Tools\Performance Tools"
  4. %VS90TEAMTOOLS%\VsPerfCmd /start:trace /output:ManagedAPI.vsp
  5. pause
  6. @echo on

The results of this batch scripts looks like this:

Turning ON performance coverage session recorder      Microsoft (R) VSPerf Command Version 9.0.30729 x86       Copyright (C) Microsoft Corp. All rights reserved.

Press any key to continue . . .

The results of this script is it starts another process that will monitor any application that happens to be instrumented with performance profiling, or code coverage profiling. In our case it started a process called VsPerfMon.exe:

Run Unit Tests

I then run all the unit tests in my DLL using another batch script. Now I have two build configurations (debug and release) so I have two batch scripts to run those: Run_Tests_Debug.bat and Run_Tests_Release.bat. Here is one of them:

  1. @echo off
  2. color 16
  3. @echo Running Unit Tests
  4. call "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Team Tools\Performance Tools\VsPerfCLREnv" /traceon
  5. call "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat";
  6. mstest /testcontainer:%CD%\UnitTest\bin\x86\Debug\UnitTest.dll > results.txt
  7. pause
  8. @echo on

Notice there the actual call to mstest.exe is what runs the unit tests. Notice that I piped the results of running the actual unit tests into a text file (i.e. results.txt). I did this because it gives me a more permanent record of how my unit tests did.

The mstest.exe file resides on my computer at:

"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\MSTest.exe"

The output, to the console window, of this script on my looks like the following:

Running Unit Tests      Enabling VSPerf Trace Profiling of managed applications (excluding allocation profiling).

Current Profiling Environment variables are:      COR_ENABLE_PROFILING=1       COR_PROFILER={6468ec6c-94bd-40d3-bd93-4414565dafbf}       COR_LINE_PROFILING=0       COR_GC_PROFILING=0       Setting environment for using Microsoft Visual Studio 2008 x86 tools.       Press any key to continue . . .

Noticed I redirected the standard output (standard out) to a text file. The results.txt file looks like this:

Microsoft (R) Test Execution Command Line Tool Version 9.0.30729.1      Copyright (c) Microsoft Corporation. All rights reserved.       Loading F:\CodePlex\UnderstandAPI\Managed\UnitTest\bin\x86\Debug\UnitTest.dll…       Starting execution…

Results               Top Level Tests      ——-               —————       Passed                UnitTest.DatabaseTest.DatabaseTest_GetAllEntities       Passed                UnitTest.DatabaseTest.DatabaseTest_GetFileEntities       Passed                UnitTest.DatabaseTest.DatabaseTest_LookupEntity       Passed                UnitTest.DatabaseTest.DatabaseTest_LookupEntitybyReference       Passed                UnitTest.DatabaseTest.DatabaseTest_LookupEntityUnique       Passed                UnitTest.DatabaseTest.DatabaseTest_LookupFile       Passed                UnitTest.DatabaseTest.DatabaseTest_Misc       Passed                UnitTest.DatabaseTest.DatabaseTest_OpenClose       Passed                UnitTest.Derived.ClassTypeTest.TestClassNests       Passed                UnitTest.Derived.ClassTypeTest.TestClassType       Passed                UnitTest.Derived.ClassTypeTest.TestClassTypeDerivedFrom       Passed                UnitTest.Derived.ClassTypeTest.TestClassTypeEnums       Passed                UnitTest.Derived.ClassTypeTest.TestClassTypeFields       Passed                UnitTest.Derived.ClassTypeTest.TestClassTypeInheritance       Passed                UnitTest.Derived.ClassTypeTest.TestClassTypeTemplates       Passed                UnitTest.Derived.ClassTypeTest.TestClassTypeUnions       Passed                UnitTest.Derived.ClassTypeTest.TestGetDefinedMethods       Passed                UnitTest.Derived.ClassTypeTest.TestGetMethods       Passed                UnitTest.Derived.EnumTypeTest.EnumTest_Enumerators       Passed                UnitTest.Derived.EnumTypeTest.EnumTest_Type       Passed                UnitTest.Derived.FileEntityTest.TestFileEntity       Passed                UnitTest.Derived.FileEntityTest.TestFileEntity_base_h       Passed                UnitTest.Derived.FileEntityTest.TestFileEntity_classes_h       Passed                UnitTest.Derived.FileEntityTest.TestFileEntityClassTypes       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Constructor       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Inline       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Inline_B       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_LexerStuff       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Member_A       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Member_B       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Member_C       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Member_D       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Member_E       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Member_F       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_MemberBlank       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Overrides       Passed                UnitTest.Derived.MethodTypeTest.MethodTest_Pure       Passed                UnitTest.EntityTest.EntityTest_Basic       Passed                UnitTest.EntityTest.EntityTest_Comments       Passed                UnitTest.EntityTest.EntityTest_Comments_Enums       Passed                UnitTest.EntityTest.EntityTest_Comments_FunctionDeclarations       Passed                UnitTest.EntityTest.EntityTest_Comments_FunctionDefinitions       Passed                UnitTest.EntityTest.EntityTest_Comments_Typedefs       Passed                UnitTest.EntityTest.EntityTest_FileReference       Passed                UnitTest.EntityTest.EntityTest_LineNumbers       Passed                UnitTest.EntityTest.EntityTest_LineNumbers_FunctionDeclarations       Passed                UnitTest.EntityTest.EntityTest_LineNumbers_FunctionDefinitions       Passed                UnitTest.EntityTest.EntityTest_LineNumbers_UnresolvedMethods       Passed                UnitTest.EntityTest.EntityTest_Refs       Passed                UnitTest.EntityTest.EntityTest_Types       Passed                UnitTest.KindTest.KindTest_Basic       Passed                UnitTest.LexerTest.Lexer_Test       Passed                UnitTest.LexerTest.Lexer_Test_GetLexeme       Passed                UnitTest.LexerTest.Lexer_Test_Next       Passed                UnitTest.LexerTest.Lexer_Test_Previous       Passed                UnitTest.LexerTest.Lexer_Test_Ref       Passed                UnitTest.ReferenceTest.ReferencesTest_Basic       Passed                UnitTest.ReferenceTest.ReferencesTest_Copying       58/58 test(s) Passed

Summary      ——-       Test Run Completed.         Passed  58         ———-         Total   58       Results file:      F:\CodePlex\UnderstandAPI\Managed\TestResults\Chris Johnson_CHRISJOHNSON-PC 2010-02-17 23_38_54.trx       Run Configuration: Default Run Configuration

Feels good to see all the unit tests pass. But now I have to save out the results of the performance profiling. And to do this, I now have to turn off the VsPerfMon.exe process that is running, and was running when I ran my unit tests.

Turn off Monitor

I have a batch script that turns off monitoring and saves out a results file containing all the profiling goodness. Performance_OFF.bat

  1. @echo off
  2. @echo Turning off performance coverage session recorder
  3. set VS90TEAMTOOLS="%VS90COMNTOOLS%..\..\Team Tools\Performance Tools"
  4. %VS90TEAMTOOLS%\VSPerfCmd /shutdown
  5. pause
  6. @echo on

The results of this batch script looks like this:

Turning off performance coverage session recorder      Microsoft (R) VSPerf Command Version 9.0.30729 x86       Copyright (C) Microsoft Corp. All rights reserved.

Shutting down the Profile Monitor      ————————————————————       Press any key to continue . . .

Now a new file is saved out to my computer:

F:\CodePlex\UnderstandAPI\Managed\Performance\ManagedAPI.vsp

Notice the name: ManagedAPI.vsp. This is the same name I passed in when I turned on the Performance Monitor. Remember this?

from: Performance_ON.bat

VsPerfCmd /start:trace /output:ManagedAPI.vsp

Now at this point, you can do two things:

  1. Examine your performance results. This is done by double clicking the ManagedAPI.vsp file. This will just launch visual studio, and display the results of your tests:
  2. Or Uninstrument your DLL’s. It’s never a good idea to ship DLL’s with instrumentation in them.

Un-Instrument the Binaries

I have a script that does this for me: Instrument_OFF.bat

  1. @echo off
  2. @echo Removing Instrumented Files
  3. pushd ..\ManagedAPI\Win32\Debug
  4. if exist UnderstandWrapper.ManagedAPI.instr.pdb (
  5. del UnderstandWrapper.ManagedAPI.instr.pdb
  6. del UnderstandWrapper.ManagedAPI.dll
  7. rename UnderstandWrapper.ManagedAPI.dll.orig UnderstandWrapper.ManagedAPI.dll
  8. )
  9. popd
  10. pushd ..\UnitTest\bin\x86\Debug
  11. if exist UnitTest.instr.pdb (
  12. del UnitTest.instr.pdb
  13. del UnitTest.dll
  14. rename UnitTest.dll.orig UnitTest.dll
  15. )
  16. popd
  17. pause
  18. @echo on

When I run this, I get the following results in the command window:

Removing Instrumented Files      Press any key to continue . . .

However, sometimes I do this only after I reviewed my performance results. Since the performance results display in visual studio may be dependent on the instrumented binaries to properly display its data.

View Results

Now that you have your .vsp file it is time to open it. The file ManagedAPI.vsp shows the following in visual studio:

From there you can analyze the results and find your performance bottlenecks.

怎么监视跟踪一个进程(Process)中的MS Unit Test DLL的详细性能(performance)【asp.net C#】的更多相关文章

  1. 一个进程(Process)最多可以生成多少个线程(Thread)

    1.进程中创建线程的限制 默认情况下,一个线程的栈要预留1M的内存空间,而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程,但是内存当然不可能完全拿来作线程的栈,所以实际 ...

  2. 如何在 Linux 中统计一个进程的线程数

    编译自:http://ask.xmodulo.com/number-of-threads-process-linux.html作者: Dan Nanni原创:LCTT https://linux.cn ...

  3. java代码中获取进程process id(转)

    另一方面,线程ID=进程ID+内部线程对象ID并不成立,    参考: blog.csdn.net/heyetina/article/details/6633901     如何在java代码中获取进 ...

  4. linux暂停一个在运行中的进程【转】

    转自:https://blog.csdn.net/Tim_phper/article/details/53536621 转载于: http://www.cszhi.com/20120328/linux ...

  5. 如何在Linux中统计一个进程的线程数(转)

    方法一: /proc proc 伪文件系统,它驻留在 /proc 目录,这是最简单的方法来查看任何活动进程的线程数. /proc 目录以可读文本文件形式输出,提供现有进程和系统硬件相关的信息如 CPU ...

  6. 计算机必知必会:进程process与线程thread 进程定义为一个正在运行的程序的实例

    http://www.nowamagic.net/librarys/veda/detail/1741进程和线程这对概念的理解也是很难的,至今网络上可查的资料对其的理解出入都挺大,在不同的操作系统中,如 ...

  7. (转)如何在Linux中统计一个进程的线程数

    如何在Linux中统计一个进程的线程数 原文:http://os.51cto.com/art/201509/491728.htm 我正在运行一个程序,它在运行时会派生出多个线程.我想知道程序在运行时会 ...

  8. C# 最基本的涉及模式(单例模式) C#种死锁:事务(进程 ID 112)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务,解决方案: C#关闭应用程序时如何关闭子线程 C#中 ThreadStart和ParameterizedThreadStart区别

    C# 最基本的涉及模式(单例模式) //密封,保证不能继承 public sealed class Xiaohouye    { //私有的构造函数,保证外部不能实例化        private  ...

  9. 在GNU Linux中怎样得到一个进程当前的流量

    /*********************************************************************  * Author  : Samson  * Date   ...

随机推荐

  1. [POJ2002]Squares(计算几何,二分)

    题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...

  2. 上下左右布局(DIV+CSS)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 齐次坐标概念&&透视投影变换推导

    http://daehgib.blog.163.com/blog/static/1861071422011579551134/ 透视投影是3D固定流水线的重要组成部分,是将相机空间中的点从视锥体(fr ...

  4. Java里面instanceof怎么实现的

    开始完全一头雾水呀,后面看了Java指令集的介绍,逐渐理解了. https://www.zhihu.com/question/21574535/answer/18998914 下面这个答案比较直白 你 ...

  5. 函数lock_rec_get_n_bits

    /*********************************************************************//** Gets the number of bits i ...

  6. [ionic开源项目教程] - 第12讲 医疗模块的实现以及Service层loadMore和doRefresh的提取封装

    关注微信订阅号:TongeBlog,可查看[ionic开源项目]全套教程. 这一讲主要实现tab2[医疗]模块,[医疗]模块跟tab1[健康]模块类似. [ionic开源项目教程] - 第12讲 医疗 ...

  7. SharePoint的实体生成

    生成Linq实体 使用SPMetal工具生成Linq to SharePoint实体 工具安装目录: C:\Program Files\Common Files\Microsoft Shared\We ...

  8. UVa 548 (二叉树的递归遍历) Tree

    题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...

  9. C#基本数据类型

    在第一章我们了解了C#的输入.输出语句后,我这一节主要是介绍C#的基础知识,本节的内容也是后续章节的基础,好的开端等于成功的一半.在你阅读完本章后,你就有足够的C#知识编写简单的程序了.但还不能使用继 ...

  10. checkbox美化;给div加上checked属性

    DIV的背景图修改 $("#isOpenmibao").css("backgroundImage", " url('../images/checkbo ...