Creating a Custom Printer

If the built in printers doesn't do it for you, you can create a custom printer for benchmark output. The only thing you need to do is to create a class that inherits from QuickBencher's Printer interface:

    /// <summary>
    /// Defines an interface for classes used to output benchmarks.
    /// </summary>
    public interface Printer
    {
        /// <summary>
        /// Prints the measurement result to some output.
        /// </summary>
        /// <param name="o">Measurement data</param>
        void Print(object o);
    }

As you can see the Printer interface only has a single method called Print, which takes an object as the only parameter. If you don't want to do something crazy the only thing you should want from the parameter is to call it's ToString() method, which will return the pre-formatted measurement result, and send it to the output target of your choice.

As an example, here is a printer that uses log4net for output:

    public class Log4netPrinter : Printer
    {
        private static ILog log = LogManager.GetLogger(typeof(Log4netPrinter).Name);       
        
        public void Print(object o)
        {
            log.Debug(o.ToString());
        }
    }

To use this printer you simply provide an instance of it to the static Printers class prior to running your benchmarks, like this:

    Printers.UsePrinter(new Log4netPrinter());

Last edited Jan 22, 2010 at 1:21 PM by tormaroe, version 2

Comments

No comments yet.