OperationPerformanceCounters.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Diagnostics / OperationPerformanceCounters.cs / 1 / OperationPerformanceCounters.cs

                            //------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel.Diagnostics 
{
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Diagnostics;
    using System.IO; 
    using System.ServiceModel.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Globalization;
 
    internal class OperationPerformanceCounters : PerformanceCountersBase
    { 
        string instanceName; 
        string operationName;
 
        PerformanceCounter[] counters;

        enum PerfCounters : int
        { 
            Calls = 0,
            CallsPerSecond, 
            CallsOutstanding, 
            CallsFailed,
            CallsFailedPerSecond, 
            CallsFaulted,
            CallsFaultedPerSecond,
            CallDuration,
            CallDurationBase, 
            SecurityValidationAuthenticationFailures,
            SecurityValidationAuthenticationFailuresPerSecond, 
            CallsNotAuthorized, 
            CallsNotAuthorizedPerSecond,
            TxFlowed, 
            TxFlowedPerSecond,
            TotalCounters = TxFlowedPerSecond + 1
        }
 
        string[] perfCounterNames =
        { 
            PerformanceCounterStrings.SERVICEMODELOPERATION.Calls, 
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallsPerSecond,
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallsOutstanding, 
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFailed,
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFailedPerSecond,
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFaulted,
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallsFaultedPerSecond, 
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallDuration,
            PerformanceCounterStrings.SERVICEMODELOPERATION.CallDurationBase, 
            PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityValidationAuthenticationFailures, 
            PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityValidationAuthenticationFailuresPerSecond,
            PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityCallsNotAuthorized, 
            PerformanceCounterStrings.SERVICEMODELOPERATION.SecurityCallsNotAuthorizedPerSecond,
            PerformanceCounterStrings.SERVICEMODELOPERATION.TxFlowed,
            PerformanceCounterStrings.SERVICEMODELOPERATION.TxFlowedPerSecond,
        }; 

        const int maxCounterLength = 64; 
        const int hashLength = 2; 
        [Flags]
        enum truncOptions : uint 
        {
            NoBits = 0,
            service7 = 0x01,
            contract7 = 0x02, 
            operation15 = 0x04,
            uri32    = 0x08 
        } 

        internal OperationPerformanceCounters(string service, string contract, string operationName, string uri) 
        {
            this.operationName = operationName;
            this.instanceName = OperationPerformanceCounters.CreateFriendlyInstanceName(service, contract, operationName, uri);
 
            this.counters = new PerformanceCounter[(int)PerfCounters.TotalCounters];
            for (int i = 0; i < (int)PerfCounters.TotalCounters; i++) 
            { 
                PerformanceCounter counter = PerformanceCounters.GetOperationPerformanceCounter(this.perfCounterNames[i], this.instanceName);
                if (counter != null) 
                {
                    try
                    {
                        counter.RawValue = 0; 
                        this.counters[i] = counter;
                    } 
#pragma warning suppress 56500 // covered by FxCOP 
                    catch (Exception e)
                    { 
                        if (ExceptionUtility.IsFatal(e))
                            throw;
                        if (DiagnosticUtility.ShouldTraceError)
                            TraceUtility.TraceEvent(TraceEventType.Error, 
                                TraceCode.PerformanceCounterFailedToLoad,
                                null, e); 
                        break; 
                    }
                } 
                else
                {
                    break;
                } 
            }
        } 
 

        internal static string CreateFriendlyInstanceName(string service, string contract, string operation, string uri) 
        {
            // instance name is: serviceName.interfaceName.operationName@uri
            int length = service.Length + contract.Length + operation.Length + uri.Length + 3;
 
            if (length > maxCounterLength)
            { 
                int count = 0; 

                truncOptions tasks = OperationPerformanceCounters.GetCompressionTasks( 
                    length, service.Length, contract.Length, operation.Length, uri.Length);

                //if necessary, compress service name to 5 chars with a 2 char hash code
                if ((tasks & truncOptions.service7) > 0) 
                {
                    count = 7; 
                    service = GetHashedString(service, count - hashLength, service.Length - count + hashLength, true); 
                }
 
                //if necessary, compress contract name to 5 chars with a 2 char hash code
                if ((tasks & truncOptions.contract7) > 0)
                {
                    count = 7; 
                    contract = GetHashedString(contract, count - hashLength, contract.Length - count + hashLength, true);
                } 
 
                //if necessary, compress operation name to 13 chars with a 2 char hash code
                if ((tasks & truncOptions.operation15) > 0) 
                {
                    count = 15;
                    operation = GetHashedString(operation, count - hashLength, operation.Length - count + hashLength, true);
                } 

                //if necessary,  compress uri to 30 chars with a 2 char hash code 
                if ((tasks & truncOptions.uri32) > 0) 
                {
                    count = 32; 
                    uri = GetHashedString(uri, 0, uri.Length - count + hashLength, false);
                }
            }
 
            // replace '/' with '|' because perfmon fails when '/' is in perfcounter instance name
            return service + "." + contract + "." + operation + "@" + uri.Replace('/', '|'); 
        } 

        private static truncOptions GetCompressionTasks(int totalLen, int serviceLen, int contractLen, int operationLen, int uriLen) 
        {
            truncOptions bitmask = 0;

            if (totalLen > maxCounterLength) 
            {
                int workingLen = totalLen; 
 
                //note: order of if statements important (see spec)!
                if (workingLen > maxCounterLength && serviceLen > 8) 
                {
                    bitmask |= truncOptions.service7; //compress service name to 8 chars
                    workingLen -= serviceLen - 7;
                } 
                if (workingLen > maxCounterLength && contractLen > 7)
                { 
                    bitmask |= truncOptions.contract7; //compress contract name to 8 chars 
                    workingLen -= contractLen - 7;
                } 
                if (workingLen > maxCounterLength && operationLen > 15)
                {
                    bitmask |= truncOptions.operation15; //compress operation name to 16 chars
                    workingLen -= operationLen - 15; 
                }
                if (workingLen > maxCounterLength && uriLen > 32) 
                { 
                    bitmask |= truncOptions.uri32; //compress uri to 32 chars
                } 
            }

            return bitmask;
        } 

 
        internal override PerformanceCounter[] Counters 
        {
            get 
            {
                return this.counters;
            }
            set 
            {
                this.counters = value; 
            } 
        }
 
        internal override string InstanceName
        {
            get
            { 
                return this.instanceName;
            } 
        } 

        internal override string[] CounterNames 
        {
            get
            {
                return this.perfCounterNames; 
            }
        } 
 
        internal override int PerfCounterStart
        { 
            get { return (int)PerfCounters.Calls; }
        }

        internal override int PerfCounterEnd 
        {
            get { return (int)PerfCounters.TotalCounters; } 
        } 

        internal void MethodCalled() 
        {
            Increment((int)PerfCounters.Calls);
            Increment((int)PerfCounters.CallsPerSecond);
            Increment((int)PerfCounters.CallsOutstanding); 
        }
 
        internal void MethodReturnedSuccess() 
        {
            Decrement((int)PerfCounters.CallsOutstanding); 
        }

        internal void MethodReturnedError()
        { 
            Increment((int)PerfCounters.CallsFailed);
            Increment((int)PerfCounters.CallsFailedPerSecond); 
            Decrement((int)PerfCounters.CallsOutstanding); 
        }
 
        internal void MethodReturnedFault()
        {
            Increment((int)PerfCounters.CallsFaulted);
            Increment((int)PerfCounters.CallsFaultedPerSecond); 
            Decrement((int)PerfCounters.CallsOutstanding);
        } 
 

        internal void SaveCallDuration(long time) 
        {
            IncrementBy((int)PerfCounters.CallDuration, time);
            Increment((int)PerfCounters.CallDurationBase);
        } 

        internal void AuthenticationFailed() 
        { 
            Increment((int)PerfCounters.SecurityValidationAuthenticationFailures);
            Increment((int)PerfCounters.SecurityValidationAuthenticationFailuresPerSecond); 
        }

        internal void AuthorizationFailed()
        { 
            Increment((int)PerfCounters.CallsNotAuthorized);
            Increment((int)PerfCounters.CallsNotAuthorizedPerSecond); 
        } 

        internal void TxFlowed() 
        {
            Increment((int)PerfCounters.TxFlowed);
            Increment((int)PerfCounters.TxFlowedPerSecond);
        } 

        internal string OperationName 
        { 
            get { return this.operationName; }
        } 

        internal bool Initialized
        {
            get { return this.counters != null; } 
        }
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK