Search code examples
benchmarkingload-testingperfmonperformancecounter

VS Load Test and 'Total Bytes Sent' Performance Counter?


I have a load test for a WCF service, where we are trying out different compression libraries and configurations and we need to measure the total number of mb sent during a test. Is there a performance counter that measure the relative trafic pr. test. If so, how do I add it to my load test - it seems that only a fraction of the performance counters are visible - E.g. under the category "Web Service", i don't see the performance counter "Total Bytes Received" in VS Load test, but I can find it in PerfMon.

Thanks


Solution

  • In the load test expand Counter Sets > Expand applicable > Right Click on Counter Sets > Add Counters

    You can implement your own custom performance counter like so:

    using System;
    using System.Diagnostics;
    using System.Net.NetworkInformation;
    
    namespace PerfCounter
    {
    class PerfCounter
    {
        private const String categoryName = "Custom category";
        private const String counterName = "Total bytes received";
        private const String categoryHelp = "A category for custom performance counters";
        private const String counterHelp = "Total bytes received on network interface";
        private const String lanName = "Local Area Connection"; // change this to match your network connection
        private  const int sampleRateInMillis = 1000;
        private const int numberofSamples = 200000;
    
        private static NetworkInterface lan = null;
        private static PerformanceCounter perfCounter;
        private static long initialReceivedBytes;
    
        static void Main(string[] args)
        {
            setupLAN();
            setupCategory();
            createCounters();
            updatePerfCounters();
        }
    
        private static void setupCategory()
        {
            if (!PerformanceCounterCategory.Exists(categoryName))
            {
                CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection();
                CounterCreationData totalBytesReceived = new CounterCreationData();
                totalBytesReceived.CounterType = PerformanceCounterType.NumberOfItems64;
                totalBytesReceived.CounterName = counterName;
                counterCreationDataCollection.Add(totalBytesReceived);
                PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection);
            }
            else
                Console.WriteLine("Category {0} exists", categoryName);
        }
    
        private static void createCounters()
        {
            perfCounter = new PerformanceCounter(categoryName, counterName, false);
            perfCounter.RawValue = getTotalBytesReceived();
        }
    
        private static long getTotalBytesReceived()
        {
            return lan.GetIPv4Statistics().BytesReceived;
        }
    
        private static void setupLAN()
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface networkInterface in interfaces)
            {
                if (networkInterface.Name.Equals(lanName))
                    lan = networkInterface;
            }
            initialReceivedBytes = lan.GetIPv4Statistics().BytesReceived;
        }
    
        private static void updatePerfCounters()
        {
            for (int i = 0; i < numberofSamples; i++)
            {
                perfCounter.RawValue = getTotalBytesReceived();
                Console.WriteLine("received: {0} bytes", perfCounter.RawValue - initialReceivedBytes);
                System.Threading.Thread.Sleep(sampleRateInMillis);
            }
        }
    }
    }