Coverage Report - org.perfidix.ouput.TabularSummaryOutput
 
Classes in this File Line Coverage Branch Coverage Complexity
TabularSummaryOutput
98%
64/65
81%
13/16
2.143
 
 1  
 /**
 2  
  * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
 3  
  * All rights reserved.
 4  
  * 
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  * * Redistributions of source code must retain the above copyright
 8  
  * notice, this list of conditions and the following disclaimer.
 9  
  * * Redistributions in binary form must reproduce the above copyright
 10  
  * notice, this list of conditions and the following disclaimer in the
 11  
  * documentation and/or other materials provided with the distribution.
 12  
  * * Neither the name of the University of Konstanz nor the
 13  
  * names of its contributors may be used to endorse or promote products
 14  
  * derived from this software without specific prior written permission.
 15  
  * 
 16  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 17  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 18  
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 19  
  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 20  
  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 21  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 22  
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 23  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 24  
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 25  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26  
  */
 27  
 package org.perfidix.ouput;
 28  
 
 29  
 import java.io.PrintStream;
 30  
 import java.lang.reflect.Method;
 31  
 
 32  
 import org.perfidix.exceptions.AbstractPerfidixMethodException;
 33  
 import org.perfidix.exceptions.PerfidixMethodInvocationException;
 34  
 import org.perfidix.meter.AbstractMeter;
 35  
 import org.perfidix.ouput.asciitable.NiceTable;
 36  
 import org.perfidix.ouput.asciitable.AbstractTabularComponent.Alignment;
 37  
 import org.perfidix.result.AbstractResult;
 38  
 import org.perfidix.result.BenchmarkResult;
 39  
 import org.perfidix.result.ClassResult;
 40  
 import org.perfidix.result.MethodResult;
 41  
 
 42  
 /**
 43  
  * Summary output using the {@link NiceTable} to format. Just giving an overview
 44  
  * of statistical analysis over the results.
 45  
  * 
 46  
  * @author Sebastian Graf, University of Konstanz
 47  
  */
 48  
 public final class TabularSummaryOutput extends AbstractOutput {
 49  
 
 50  
     /** Print stream where the result should end. */
 51  
     private transient final PrintStream out;
 52  
 
 53  
     /**
 54  
      * Constructor for piping the result to elsewhere.
 55  
      * 
 56  
      * @param paramOut
 57  
      *            an {@link PrintStream} to pipe to.
 58  
      */
 59  
     public TabularSummaryOutput(final PrintStream paramOut) {
 60  25
         super();
 61  25
         out = paramOut;
 62  25
     }
 63  
 
 64  
     /**
 65  
      * Constructor, just giving out on the {@link System#out}.
 66  
      */
 67  
     public TabularSummaryOutput() {
 68  25
         this(System.out);
 69  25
     }
 70  
 
 71  
     /** {@inheritDoc} */
 72  
     @Override
 73  
     public void visitBenchmark(final BenchmarkResult benchRes) {
 74  10
         final int numberOfColumns = 9;
 75  10
         NiceTable table = new NiceTable(numberOfColumns);
 76  10
         table = generateHeader(table);
 77  10
         for (final AbstractMeter meter : benchRes.getRegisteredMeters()) {
 78  15
             table.addHeader(meter.getName(), '=', Alignment.Center);
 79  15
             for (final ClassResult classRes : benchRes.getIncludedResults()) {
 80  25
                 table.addHeader(classRes.getElementName(), '.', Alignment.Left);
 81  25
                 for (final MethodResult methRes : classRes.getIncludedResults()) {
 82  75
                     table = generateMeterResult(methRes.getElementName(), meter, methRes, table);
 83  75
                 }
 84  
 
 85  25
                 table.addHeader(new StringBuilder("Summary for ").append(classRes.getElementName())
 86  
                     .toString(), '_', Alignment.Left);
 87  25
                 table = generateMeterResult("", meter, classRes, table);
 88  25
                 table.addLine('-');
 89  
 
 90  25
             }
 91  15
         }
 92  10
         table.addHeader("Summary for the whole benchmark", '=', Alignment.Center);
 93  
 
 94  10
         for (final AbstractMeter meter : benchRes.getRegisteredMeters()) {
 95  15
             table = generateMeterResult("", meter, benchRes, table);
 96  15
         }
 97  
 
 98  10
         table.addHeader("Exceptions", '=', Alignment.Center);
 99  10
         for (final AbstractPerfidixMethodException exec : benchRes.getExceptions()) {
 100  26
             final StringBuilder execBuilder0 = new StringBuilder();
 101  26
             execBuilder0.append("Related exception: ").append(exec.getExec().getClass().getSimpleName());
 102  26
             table.addHeader(execBuilder0.toString(), ' ', Alignment.Left);
 103  
 
 104  26
             final StringBuilder execBuilder1 = new StringBuilder();
 105  26
             if (exec instanceof PerfidixMethodInvocationException) {
 106  26
                 execBuilder1.append("Related place: method invocation");
 107  
             } else {
 108  0
                 execBuilder1.append("Related place: method check");
 109  
             }
 110  26
             table.addHeader(execBuilder1.toString(), ' ', Alignment.Left);
 111  26
             if (exec.getMethod() != null) {
 112  26
                 final StringBuilder execBuilder2 = new StringBuilder();
 113  26
                 execBuilder2.append("Related method: ").append(exec.getMethod().getName());
 114  26
                 table.addHeader(execBuilder2.toString(), ' ', Alignment.Left);
 115  
             }
 116  26
             final StringBuilder execBuilder3 = new StringBuilder();
 117  26
             execBuilder3.append("Related annotation: ").append(exec.getRelatedAnno().getSimpleName());
 118  26
             table.addHeader(execBuilder3.toString(), ' ', Alignment.Left);
 119  26
             table.addLine('-');
 120  
 
 121  26
         }
 122  10
         table.addLine('=');
 123  10
         out.println(table.toString());
 124  10
     }
 125  
 
 126  
     /**
 127  
      * Generating the results for a given table.
 128  
      * 
 129  
      * @param columnDesc
 130  
      *            the description for the row
 131  
      * @param meter
 132  
      *            the corresponding {@link AbstractMeter} instance
 133  
      * @param result
 134  
      *            the corresponding {@link AbstractResult} instance
 135  
      * @param input
 136  
      *            the {@link NiceTable} to be print to
 137  
      * @return the modified {@link NiceTable} instance
 138  
      */
 139  
     private NiceTable generateMeterResult(final String columnDesc, final AbstractMeter meter,
 140  
         final AbstractResult result, final NiceTable input) {
 141  115
         input.addRow(new String[] {
 142  
             columnDesc,
 143  
             meter.getUnit(),
 144  
             AbstractOutput.format(result.sum(meter)),
 145  
             AbstractOutput.format(result.min(meter)),
 146  
             AbstractOutput.format(result.max(meter)),
 147  
             AbstractOutput.format(result.mean(meter)),
 148  
             AbstractOutput.format(result.getStandardDeviation(meter)),
 149  
             new StringBuilder("[").append(AbstractOutput.format(result.getConf05(meter))).append("-").append(
 150  
                 AbstractOutput.format(result.getConf95(meter))).append("]").toString(),
 151  
             AbstractOutput.format(result.getResultSet(meter).size())
 152  
         });
 153  115
         return input;
 154  
     }
 155  
 
 156  
     /**
 157  
      * {@inheritDoc}
 158  
      */
 159  
     @Override
 160  
     public boolean listenToResultSet(final Method meth, final AbstractMeter meter, final double data) {
 161  50
         final StringBuilder builder = new StringBuilder();
 162  50
         builder.append("Class: ").append(meth.getDeclaringClass().getSimpleName()).append("#").append(
 163  
             meth.getName());
 164  50
         builder.append("\nMeter: ").append(meter.getName());
 165  50
         builder.append("\nData: ").append(data).append("\n");
 166  50
         out.println(builder.toString());
 167  50
         return true;
 168  
     }
 169  
 
 170  
     /** {@inheritDoc} */
 171  
     @Override
 172  
     public boolean listenToException(final AbstractPerfidixMethodException exec) {
 173  5
         final StringBuilder builder = new StringBuilder();
 174  5
         if (exec.getMethod() != null) {
 175  5
             builder.append("Class: ").append(exec.getMethod().getDeclaringClass().getSimpleName())
 176  
                 .append("#").append(exec.getMethod().getName()).append("\n");
 177  
         }
 178  5
         builder.append("Annotation: ").append(exec.getRelatedAnno().getSimpleName());
 179  5
         builder.append("\nException: ").append(exec.getClass().getSimpleName()).append("/").append(
 180  
             exec.getExec().toString());
 181  5
         out.println(builder.toString());
 182  5
         exec.getExec().printStackTrace(out);
 183  5
         return true;
 184  
 
 185  
     }
 186  
 
 187  
     /**
 188  
      * Generating header for a given table.
 189  
      * 
 190  
      * @param table
 191  
      *            the table where the header should fit to
 192  
      * @return another {@link NiceTable} instance
 193  
      */
 194  
     private NiceTable generateHeader(final NiceTable table) {
 195  10
         table.addHeader("Benchmark");
 196  10
         table.addRow(new String[] {
 197  
             "-", "unit", "sum", "min", "max", "avg", "stddev", "conf95", "runs"
 198  
         });
 199  10
         return table;
 200  
     }
 201  
 
 202  
 }