views:

1454

answers:

5

I have read and practiced the MSDN's Profiler Tutorial. But I couldn't find a way to profile a library solution (the button "Launch with profiling" is disabled for libraries).

  1. The only solution I could think of so far is to create an executable project just for profiling purposes.
  2. I have already written unit tests for my library, using Visual Studio Team System's Test Framework, so I imagine I could use them for profile, as Rick Minerich does with NUnit.

Anyone can point me the right direction?

A: 

Profiling is a runtime technique so if I understand well you have to run your software so you can runtime profile it.

You can not normal run a library either, the simplest option is to run your tests or create a simple exe that uses the library. You can instrument just the dll so you profile only where you want.

davidnr
A: 

This doesn't make a lot of sense. The Visual Studio profiler doesn't perform a static analysis of your code to report how fast it is. It lets you run it, and instruments the code or takes samples of which code is running when to estimate how long each section of code takes.

Given that, how would you propose to profile your library? How does Visual Studio know about your tests, and which ones you want to run for profiling purposes?

Answer: you make an executable and call those tests, and that's how it knows.

mquander
+3  A: 

It is possible to profile using your tests. You simply run the profiler on whatever is launching the tests and specify your to-be-profiled assemblies as the profiler targets. However, without knowing exactly what infrastructure you are using for both testing and profiling, it's pretty much impossible to point you towards a solution.

Because I use NUnit, I'll use it as an example. You could create a test category and call it profile tests. Then you could run nunit.exe from the profiler with the /include command line option to specify those tests:

nunit-console myassembly.dll /include:Profile

You can use the Analyize->Performance Wizard to do this. The dll path would be your assembly and the Executable path would be the nunit-console command line as mentioned above.

I also know that ReSharper adds extensions to Visual Studio to do this for you. With it you can just right click on a test and select "Profile TESTNAME". While it does cost money, I think that there is an evaluation version you could check out.

Also, just as a note, be sure to profile with Release libraries. Release and Debug often have MUCH different performance profiles.

Rick Minerich
I use is Visual Studio Team Studio Test Framework or MSTest as some people call it. That's the reason of my question: if NUnit has a way to do that, why MSTest don't, or if it has, where to find it?
Jader Dias
If you understand how MSTest works, you'll know why your answer don't satisfies me fully. MSTest generates multiple folders, one for each time I run a test, so I can't automatize the profiling process.
Jader Dias
So, using MSTest you can do almost exactly what I specified. Just replace the call to nunit-console with MSTest.exe /test:[TestName]See the MSDN docs on MSTest.exe for more detailed usage information:http://msdn.microsoft.com/en-us/library/ms182489(VS.80).aspx
Rick Minerich
A: 

By default all projects must have program test. This is easier mode to exercise code. Now you should be thinking about cover all the possibilities of entries. He also will use the tools of coverage.

lsalamon
A: 

Actually you can add to the list of Targets (right click on Targets) any executable.

Text of Program.cs is pretty simple:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            YourDllLibrary.Tests.TestClass t = new YourDllLibrary.Tests.TestClass();
            t.Init();
            MethodInfo[] m = t.GetType().GetMethods();
            for (int i = 0; i < m.Length; i++)
            {
                MethodInfo mi = m[i];

                if (mi.DeclaringType.Name != t.GetType().Name )
                    continue;

                if(Attribute.GetCustomAttribute(mi, 
                    typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute)) == null)
                    continue;

                try
                {
                    Console.Write(mi.Name + " - ");
                    mi.Invoke(t, null);
                    Console.WriteLine("passed");
                }
                catch
                {
                    Console.WriteLine("failed");
                }
            }
        }

    }
}
itur