Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Measuring and Monitoring WCF Web Service Performance

, 4 Oct 2012 GPL3
Using ServiceMon to obtain performance statistics for web services
ServiceMonExtensionExample-noexe.zip
ServiceMonExtensionExample
ServiceMonExtensions
Properties
Service References
TempConvertWebService
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
tempconvert.wsdl
Tests
Properties
ServiceMonExtensionExample.zip
bin
Kaleida.ServiceMonitor.Framework.dll
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
tempconvert.wsdl
ServiceMon_v1.0.0_Source-noexe.zip
ServiceMon v1.0.0 Source
BuildTools
antlr-3.2.jar
nUnit
Failure.jpg
Ignored.jpg
NUnitTests.nunit
Success.jpg
tempfile.tmp
Everything.build
Kaleida.ServiceMonitor.CodePlexUploader
Kaleida.ServiceMonitor.Framework
Properties
Responses
Kaleida.ServiceMonitor.Model
Email
Layout
Parsing
ScriptGrammar.g
Properties
Runtime
ScriptDirectives
Statistics
StructuredSource
Kaleida.ServiceMonitor.Operations
PreparedRequests
Properties
ResponseHandlers
Kaleida.ServiceMonitor.UI
Application.ico
Icons
Application.ico
Error.ico
Running.ico
Stopped.ico
Kaleida.ProductBuilder.ServiceMonitor.csproj.DotSettings
Kaleida.ProductBuilder.ServiceMonitor.csproj.DotSettings.user
Kaleida.ProductBuilder.ServiceMonitor.csproj.ReSharper
NotificationPanels
Properties
Settings.settings
Scripts
Introductory Examples.mscr
More Examples.mscr
Kaleida.ServiceMonitor.UnitTests
Framework
Model
Email
Layout
Parsing
Runtime
Statistics
Operations
Scripts
Setup
DialogBackround.bmp
DialogBackround.pdn
DialogTopBanner.bmp
GPLv3License.rtf
Product.wxs
Setup.wixproj
ServiceMon_v1.0.0_Source.zip
bin
Antlr3.Runtime.dll
CodePlex.WebServices.Client.dll
JetBrains.Annotations.dll
log4net.dll
antlr-3.2.jar
Failure.jpg
fit.dll
Ignored.jpg
loadtest-assembly.dll
mock-assembly.dll
nonamespace-assembly.dll
notestfixtures-assembly.dll
nunit-console-runner.dll
nunit-console-x86.exe
nunit-console.exe
nunit-console.tests.dll
nunit-gui-runner.dll
nunit-gui.exe
nunit-gui.tests.dll
nunit-test-server.dll
nunit-x86.exe
nunit.core.dll
nunit.core.extensions.dll
nunit.core.interfaces.dll
nunit.core.tests.dll
nunit.exe
nunit.extensions.tests.dll
nunit.fixtures.dll
nunit.fixtures.tests.dll
nunit.framework.dll
nunit.framework.extensions.dll
nunit.framework.tests.dll
nunit.mocks.dll
nunit.mocks.tests.dll
nunit.testutilities.dll
nunit.uikit.dll
nunit.uikit.tests.dll
nunit.util.dll
nunit.util.tests.dll
NUnitTests.nunit
runFile.exe
Success.jpg
tempfile.tmp
test-assembly.dll
test-utilities.dll
timing-tests.dll
WixFileVersionExtension.dll
COPYING
Everything.build
Properties
ScriptGrammar.g
Application.ico
Application.ico
Error.ico
Running.ico
Stopped.ico
Kaleida.ProductBuilder.ServiceMonitor.csproj.DotSettings
Kaleida.ProductBuilder.ServiceMonitor.csproj.DotSettings.user
Kaleida.ProductBuilder.ServiceMonitor.csproj.ReSharper
Settings.settings
Introductory Examples.mscr
More Examples.mscr
DialogBackround.bmp
DialogBackround.pdn
DialogTopBanner.bmp
GPLv3License.rtf
Product.wxs
Setup.wixproj
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Antlr.Runtime.Tree;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Model.Runtime;
using Kaleida.ServiceMonitor.Model.StructuredSource;

namespace Kaleida.ServiceMonitor.Model.Parsing
{
    public static class ScriptParser
    {
        [CanBeNull]
        private static ScriptCompiler compiler;
        
        [CanBeNull]
        private static ReflectionOperationFactory commandFactory;

        public static void Initialise(string searchDirectoryPath)
        {
            if(!Directory.Exists(searchDirectoryPath))
                throw new InvalidOperationException(string.Format("The required directory '{0}' does not exist", searchDirectoryPath));

            var dllPaths = Directory.GetFiles(searchDirectoryPath, "*.dll");
            var assemblies = dllPaths.Select(LoadFile);
            commandFactory = new ReflectionOperationFactory(assemblies);
			Initialise(commandFactory, commandFactory);
        }

    	internal static void Initialise(IPreparedRequestFactory preparedRequestFactory, IResponseHandlerFactory responseHandlerFactory)
    	{
    		compiler = new ScriptCompiler(preparedRequestFactory, responseHandlerFactory);
    	}

    	public static RuntimeEnvironment GetRuntimeEnvironment()
        {
            if(commandFactory == null) throw new InvalidOperationException("Must first call ScriptParser.Initialise");
            return commandFactory.GetRuntimeEnvironment();
        }

        private static Assembly LoadFile(string path)
        {
            try
            {
                return Assembly.LoadFile(path);
            }
            catch (NotSupportedException)
            {
                throw new InvalidOperationException(string.Format("Could not load '{0}'. Please ensure that all DLL files in this directory are unblocked. In Windows Explorer right-mouse on the file, click 'Properties', then the 'Unblock' button", path));
            }
        }

        public static ScriptDefinition Parse(this string scriptSource)
        {
            try
            {
                var tree = scriptSource.GetSyntaxTree();
                return BuildScript(tree);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(string.Format("An error occurred whilst parsing '{0}': {1}", scriptSource, e.Message));
            }
        }

        public static CompiledScript ParseAndBuild(this string text)
        {
            if (compiler == null) throw new InvalidOperationException("Must first call ScriptParser.Initialise");

            var scriptDefinition = text.Parse();
            var executableScript = compiler.Build(scriptDefinition);
            return executableScript;
        }

        private static ScriptDefinition BuildScript(ITree tree)
        {
            var nodes = tree.GetChildren();
            var directives = nodes.Where(i => i.Type == ScriptGrammarLexer.DIRECTIVE).Select(BuildDirective).ToList();
            var lines = nodes.Where(i => i.Type == ScriptGrammarLexer.COMMAND).Select(BuildCommandWithHandler).ToList();
            var comments = nodes.Where(i => i.Type == ScriptGrammarLexer.COMMENT).Select(BuildComment).ToList();

            return new ScriptDefinition(directives, lines, comments);
        }

        private static DirectiveDefinition BuildDirective(ITree node)
        {
            ValidateNodeType(node, ScriptGrammarLexer.DIRECTIVE, "Directive");
            
            if (node.ChildCount != 1)
                throw new InvalidOperationException(string.Format("Expected directive node on line {0} to have one child but found {1}", node.Line, node.ChildCount));

            var directiveNode = node.GetChild(0);
            ValidateNodeType(directiveNode, ScriptGrammarLexer.PrefixedDirective, "Prefixed Directive");

            if (!directiveNode.Text.StartsWith("#"))
                throw new InvalidOperationException(string.Format("Expected directive node '{0}' on line {1} to begin with #", directiveNode.Text, directiveNode.Line));

            var text = directiveNode.Text.Substring(1);
            var arguments = directiveNode.GetChildren().Select(BuildArgument).ToList();

            return new DirectiveDefinition(text, arguments);
        }

        private static OperationDefinition BuildCommandWithHandler(ITree node)
        {
            var actions = node.GetChildren().Select(BuildActionDefinition).ToList();

            if(actions.Count < 1 || actions.Count > 2)
                throw new InvalidOperationException(string.Format("Expected command line {0} to have 1 or 2 actions but found {1}", node.Line, actions.Count));

            var requestAction = actions.First();
            var responseAction = actions.Count == 2 ? actions.Last() : ActionDefinition.DoNothing;
            
            return new OperationDefinition(requestAction, responseAction);
        }

        private static string BuildComment(ITree node)
        {
            ValidateNodeType(node, ScriptGrammarLexer.COMMENT, "Comment");

            if (node.ChildCount != 1)
                throw new InvalidOperationException(string.Format("Expected comment node on line {0} to have one child but found {1}", node.Line, node.ChildCount));

            if (!node.GetChild(0).Text.StartsWith("//"))
                throw new InvalidOperationException(string.Format("Expected comment on line {0} to start with //", node.Line));

            return node.GetChild(0).Text.Substring("//".Length);
        }

        private static ActionDefinition BuildActionDefinition(ITree node)
        {
            ValidateNodeType(node, ScriptGrammarLexer.Identifier, "Identifier");

            return new ActionDefinition(node.Text, node.GetChildren().Select(BuildArgument).ToList());
        }

        private static string BuildArgument(ITree node)
        {
            ValidateNodeType(node, ScriptGrammarLexer.QuotedString, "String");

            return node.Text.Substring(1, node.Text.Length - 2).Replace("\"\"", "\"");
        }

        private static void ValidateNodeType(ITree node, int expectedType, string expectedTypeName)
        {
            if (node.Type != expectedType)
                throw new InvalidOperationException(string.Format("Expected '{0}' (at line {1}, column {2}) to be type {3} but it was {4}", node.Text, node.Line, node.CharPositionInLine, expectedTypeName, node.Type));
        }
    }
}

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)

Share

About the Author

Stuart Wheelwright
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.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 4 Oct 2012
Article Copyright 2012 by Stuart Wheelwright
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid