Click here to Skip to main content
15,897,518 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.5K   2.2K   47  
Using ServiceMon to obtain performance statistics for web services
using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using log4net;

namespace Kaleida.ServiceMonitor.Model
{
    public class CodePlexWebVersionCheck
    {
        private static readonly ILog log = LogManager.GetLogger(typeof (CodePlexWebVersionCheck));

        private const string CodePlexReleaseUrl = "http://servicemon.codeplex.com/releases/";

        public VersionCheckResult CheckLatestVersion()
        {
            try
            {
                log.DebugFormat("Checking for latest version of ServiceMon using '{0}'", CodePlexReleaseUrl);

                var request = (HttpWebRequest)WebRequest.Create(CodePlexReleaseUrl);

                var webResponse = request.GetResponse();

                var stream = webResponse.GetResponseStream();
                if (stream == null)
                    throw new InvalidOperationException("Received null response from " + CodePlexReleaseUrl);

                using (var sr = new StreamReader(stream))
                {
                    string html = sr.ReadToEnd();
                    var version = ExtractVersionFromHtmlPage(html);

                    return VersionCheckResult.FoundLatestVersion(version, CodePlexReleaseUrl);
                }
            }
            catch (Exception exception)
            {
                log.ErrorFormat("Error checking for latest version: {0}", exception);
                return VersionCheckResult.Error(exception);
            }
        }

        internal static Version ExtractVersionFromHtmlPage(string html)
        {
            var regex = new Regex("<h1 class=\"page_title\">.*ServiceMon Release ([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+).*</h1>");
            Match match = regex.Match(html);
            if (match.Success)
            {
                var versionString = match.Groups[1].Value;

                Version version;
                if(!Version.TryParse(versionString, out version))
                    throw new InvalidOperationException(string.Format("Cannot parse '{0}' as a Version", versionString));

                return version;
            }
            throw new InvalidOperationException("Couldn't extract latest version from CodePlex page. Unexpected format");
        }
    }
}

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