Click here to Skip to main content
15,891,864 members
Articles / DevOps / Load Testing

Measuring and Monitoring WCF Web Service Performance

Rate me:
Please Sign up or sign in to vote.
5.00/5 (17 votes)
4 Oct 2012GPL310 min read 55.4K   2.2K   47  
Using ServiceMon to obtain performance statistics for web services
using System;
using Kaleida.ServiceMonitor.Framework;
using Kaleida.ServiceMonitor.Framework.Responses;

namespace Kaleida.ServiceMonitor.Operations.ResponseHandlers
{
    public class MustEqual : ResponseHandler
    {
        private readonly string expected;

        public MustEqual(string expected)
        {
            this.expected = expected;
        }

        public override string Description
        {
            get { return string.Format("expect equal to '{0}'", expected); }
        }

        public override string ProcessResponse(object response)
        {
            if (response is int && expected.IsInteger())
            {
                var actualInt = (int)response;
                TestAsInteger(actualInt);
                return actualInt.ToFormattedString();
            }
            if (response is decimal && expected.IsDecimal())
            {
                var actualDecimal = (decimal)response;
                TestAsDecimal(actualDecimal);
                return actualDecimal.ToFormattedString();
            }
            if (response is TimeSpan && expected.IsTimeSpan())
            {
                var actualTimespan = (TimeSpan)response;
                TestAsTimespan(actualTimespan);
                return actualTimespan.ToFormattedString();
            }
            if (response is ITimedEvent && expected.IsTimeSpan())
            {
                var actualTimespan = ((ITimedEvent)response).TimeTaken;
                TestAsTimespan(actualTimespan);
                return "Time Taken " + actualTimespan.ToFormattedString();
            }
            if (response is IStringContainer)
            {
                var actualContent = ((IStringContainer)response).Content;
                TestAsString(actualContent);
                return actualContent.ToFormattedString();
            }
            
            var actualString = response.ToString();
            TestAsString(actualString);
            return actualString.ToFormattedString();
        }

        private void TestAsString(string actualString)
        {
            if (actualString != expected)
                throw NewMismatchException(expected.ToFormattedString(), actualString.ToFormattedString());
        }

        private void TestAsInteger(int actualInt)
        {
            var expectedInt = Convert.ToInt32(expected);

            if (actualInt != expectedInt)
                throw NewMismatchException(expectedInt.ToFormattedString(), actualInt.ToFormattedString());
        }

        private void TestAsDecimal(decimal actualDecimal)
        {
            var expectedDecimal = Convert.ToDecimal(expected);

            if (actualDecimal != expectedDecimal)
                throw NewMismatchException(expectedDecimal.ToFormattedString(), actualDecimal.ToFormattedString());
        }

        private void TestAsTimespan(TimeSpan actualTimespan)
        {
            var expectedTimespan = expected.ToTimeSpan();

            if (actualTimespan != expectedTimespan)
                throw NewMismatchException(expectedTimespan.ToFormattedString(), actualTimespan.ToFormattedString());
        }

        private static InvalidOperationException NewMismatchException(object expected, object response)
        {
            return new InvalidOperationException(string.Format("Expected response to equal {0} but was {1}", expected, response));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect BlackJet Software Ltd
United Kingdom United Kingdom
Stuart Wheelwright is the Principal Architect and Software Developer at BlackJet Software Ltd.

He has over 16 years commercial experience producing robust, maintainable, web-based solutions and bespoke systems for Microsoft platforms.

His latest project is Shopping UK, an elegantly simple shopping list for iPhone.

Comments and Discussions