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:
- Create a Class Library Project (C# 4.0).
- 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.
- Create a new class. Assign it a name, like CustomLogDataCollector.
- Add the following using declarations above the class name.
- Make the class inherit from DataCollector (Microsoft.VisualStudio.PerformanceTools.DataCollection namespace).
- 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.
- 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.
- 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!
- Build the project. Copy the project’s DLL into the %DevEnvDir%\PrivateAssemblies\DataCollectors 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:
- In Solution Explorer, double click on the “.testsettings” file (the project’s test run configuration) to open the configuration editor.
- 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.
- Start a Load Test run.
- Wait until the Load Test run is completed. On the Test Results window, click the Test run completed link.
- 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.
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 PrivateAssemblies\DataCollectors 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.