Visual Studio 2010: How to create a Performance Data Collector

Let’s assume you often run Performance Tests and your application outputs a log file that you manually backup on every test run. You can automate the process using a Collector, a new feature that ships with Visual Studio 2010.

Note: For a great introduction on Data Collectors in Visual Studio 2010 Beta1, please check Amit’s blog.

This post is meant to answer the question: How can I create my own Data Collector in Visual Studio 2010?

Steps to create a Performance Collector

Let’s create a Performance Collector that generates a log file and copies it to the Test Results folder:

  1. Create a Class Library Project (C# 4.0).
  2. Add a reference to Microsoft.VisualStudio.PerformanceTools.DataCollection and Microsoft.VisualStudio.QualityTools.ExecutionCommon. Both assemblies can be found in the %DevEnvDir%PrivateAssemblies folder, and should only be used for extending Visual Studio.
  3. Create a new class. Assign it a name, like CustomLogDataCollector.
  4. Add the following using declarations above the class name.
  5. Make the class inherit from DataCollector (Microsoft.VisualStudio.PerformanceTools.DataCollection namespace).
  6. Decorate the class name with the DataCollectorFriendlyName, DataCollectorTypeUri and DataCollectorDescription attributes. The Uri needs to be unique for Visual Studio to identify the Collector, it is usually created in a Company/CollectorName/Version hierarchy, as shown below.
  7. Add a property of type IDataCollectorSink and implement the DoInitialize method to capture the IDataCollectorSink value. An IDataCollectorSink allows the Collector to interact with the Test Results folder.
  8. Override the SessionEnd method and invoke the IDataCollectionSync’s SendFileAsync method to copy the log file contents into the Test Results folder. In the example below I’m simulating a log file by creating a HelloWorld.txt file with a log line. It’s very easy to adapt this to your own environment!
  9. Build the project. Copy the project’s DLL into the %DevEnvDir%PrivateAssembliesDataCollectors folder.

That’s it! Now it’s time to enjoy your first Custom Collector.

Using the custom Data Collector in your Test Project

To use the Data Collector for your Test Project you just need to:

  1. In Solution Explorer, double click on the “.testsettings” file (the project’s test run configuration) to open the configuration editor.
  2. Select the Execution Criteria configuration from the list on the left. In the Collectors section on the right, scroll down until you see “Your custom collector1″. Make sure the Enabled checkbox is selected and click Apply and then Close. image
  3. Start a Load Test run.
  4. Wait until the Load Test run is completed. On the Test Results window, click the Test run completed link.
  5. Verify that there is a file named HelloWorld.txt in the Collected Files section, as shown below. Clicking the link will open the file in your configured text editor.

Next steps

This was a quick introduction on how to create custom Collectors for your Test Projects. Now that you have it working, you can augment it by creating:

  • An installer that copies the DLL file to PrivateAssembliesDataCollectors folder
  • A class to hold the Collector’s configuration
  • A visual editor for the Data Collector’s options

I hope you can find a good use for this, and your feedback is greatly appreciated.

Leave a Reply