PerformanceTests.cs :  » Logging-Tools » Common.Logging » Common » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Logging Tools » Common.Logging 
Common.Logging » Common » PerformanceTests.cs
#region License

/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

#if NET_3_0

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using Common.Logging;
using Common.Logging.Simple;
using NUnit.Framework;

namespace Common{
    public class MyClass
    {
        private readonly TraceSource trace = new TraceSource(typeof(MyClass).Name);

        private void Foo(int id)
        {
            try
            {
                Bar(id);
            }
            catch (Exception ex)
            {
                trace.TraceEvent(TraceEventType.Information, -1, "error id={0}:{1}", id, ex);
            }
        }

        private void Bar(int id) { }
    }

    internal class MyTestObjectUnderTrace
    {
        public override string ToString()
        {
            // a typical ToString() implementation of user-defined types
            return string.Format("{0}#{1} {{2}}", this.GetType().Name, this.GetHashCode(), "some info");
        }
    }

    public class MyTestTraceListener : TraceListener
    {
        public readonly ArrayList Messages = new ArrayList();

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            if (Filter == null || Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null))
            {
                // emulates the underlying call to TraceEvent/WriteLine
                string message = string.Format(format, args);
                Messages.Add(message);
            }
            //            base.TraceEvent(eventCache, source, eventType, id, format, args);
        }

        public override void Write(string message)
        {
            // ensure we don't get called accidentially
            throw new NotImplementedException();
        }

        public override void WriteLine(string message)
        {
            // ensure we don't get called accidentially
            throw new NotImplementedException();
        }
    }

    public class MyTestLoggerFactoryAdapter : AbstractSimpleLoggerFactoryAdapter
    {
        private static readonly ArrayList s_messages = new ArrayList();

        public ArrayList Messages = s_messages;

        private class MySimpleLogger : AbstractSimpleLogger
        {
            private readonly IList messages;

            public MySimpleLogger(ArrayList messages, string logName, LogLevel logLevel, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat)
                : base(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
            {
                this.messages = messages;
            }

            protected override void WriteInternal(LogLevel level, object message, Exception exception)
            {
                messages.Add(message);
            }
        }

        public MyTestLoggerFactoryAdapter(NameValueCollection properties)
            : base(properties)
        {
        }

        protected override ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat)
        {
            return new MySimpleLogger(Messages, name, level, showLevel, showDateTime, showLogName, dateTimeFormat);
        }
    }

    [TestFixture, Explicit]
    public class PerformanceTests
    {
        [Test]
        public void JustForTestingLoggingConfiguration()
        {
            TraceSource traceSource = new TraceSource("DiagnosticsTracePerformanceTest");
            traceSource.TraceEvent(TraceEventType.Verbose, -1, "test trace info {0}", new MyTestObjectUnderTrace());
            //            Trace.TraceInformation("traced info");
            Trace.WriteLine("some message", "myCategory");
            Console.WriteLine("from console");
        }

        [Test]
        public void DiagnosticsTracePerformanceTest()
        {
            MyTestObjectUnderTrace myObj = new MyTestObjectUnderTrace();
            int runs = 100000000;
            StopWatch sw;
            MyTestTraceListener listener;

            // use NoOp logging
            LogManager.Adapter = new NoOpLoggerFactoryAdapter();
            ILog log = LogManager.GetLogger(this.GetType());

            sw = new StopWatch();
            using (sw.Start("Time:{0} - log.InfoFormat + NoOpLogger"))
            {
                for (int i = 0; i < runs; i++)
                {
                    log.Info( m=>m("some logger info {0}", (object)myObj) );
                }
            }

            // Use unconfigured TraceSource
            TraceSource traceSource = new TraceSource("bla");
            sw = new StopWatch();
            using (sw.Start("Time:{0} - traceSource.TraceEvent + unconfigured TraceSource"))
            {
                for (int i = 0; i < runs; i++)
                {
                    if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
                    {
                        traceSource.TraceEvent(TraceEventType.Information, -1, "some tracesource info {0}", (object)myObj);
                    }
                }
            }

            // use Common.Logging
            MyTestLoggerFactoryAdapter adapter = new MyTestLoggerFactoryAdapter(null);
            adapter.ShowLogName = true;
            adapter.ShowDateTime = true;
            adapter.Level = LogLevel.Warn;
            LogManager.Adapter = adapter;
            log = LogManager.GetLogger(this.GetType());

            sw = new StopWatch();
            using (sw.Start("Time:{0} - log.InfoFormat"))
            {
                for (int i = 0; i < runs; i++)
                {
                    log.Info(m => m("some logger info {0}", (object)myObj));
                }
            }
            Assert.AreEqual(0, adapter.Messages.Count);

            // Use configured TraceSource
            traceSource = new TraceSource("DiagnosticsTracePerformanceTest");
            listener = (MyTestTraceListener) traceSource.Listeners[0];
            sw = new StopWatch();
            using (sw.Start("Time:{0} - traceSource.TraceEvent"))
            {
                for (int i = 0; i < runs; i++)
                {
                    if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
                    {
                        traceSource.TraceEvent(TraceEventType.Information, -1, "some tracesource info {0}", (object)myObj);
                    }
                }
            }
            Assert.AreEqual(0, listener.Messages.Count);
        }
    }
}

#endif
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.