Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.internal.util;
     18 
     19 import com.google.android.collect.Lists;
     20 import com.google.caliper.SimpleBenchmark;
     21 
     22 import java.io.File;
     23 import java.io.FileOutputStream;
     24 import java.io.IOException;
     25 import java.io.PrintWriter;
     26 import java.util.ArrayList;
     27 
     28 public class IndentingPrintWriterBenchmark extends SimpleBenchmark {
     29 
     30     private PrintWriter mDirect;
     31     private IndentingPrintWriter mIndenting;
     32 
     33     private Node mSimple;
     34     private Node mComplex;
     35 
     36     @Override
     37     protected void setUp() throws IOException {
     38         final FileOutputStream os = new FileOutputStream(new File("/dev/null"));
     39         mDirect = new PrintWriter(os);
     40         mIndenting = new IndentingPrintWriter(mDirect, "  ");
     41 
     42         final Node manyChildren = Node.build("ManyChildren", Node.build("1"), Node.build("2"),
     43                 Node.build("3"), Node.build("4"), Node.build("5"), Node.build("6"), Node.build("7"),
     44                 Node.build("8"), Node.build("9"), Node.build("10"));
     45 
     46         mSimple = Node.build("RED");
     47         mComplex = Node.build("PARENT", Node.build("RED"), Node.build("GREEN",
     48                 Node.build("BLUE", manyChildren, manyChildren), manyChildren, manyChildren),
     49                 manyChildren);
     50     }
     51 
     52     @Override
     53     protected void tearDown() {
     54         mIndenting.close();
     55         mIndenting = null;
     56         mDirect = null;
     57     }
     58 
     59     public void timeSimpleDirect(int reps) {
     60         for (int i = 0; i < reps; i++) {
     61             mSimple.dumpDirect(mDirect, 0);
     62         }
     63     }
     64 
     65     public void timeSimpleIndenting(int reps) {
     66         for (int i = 0; i < reps; i++) {
     67             mSimple.dumpIndenting(mIndenting);
     68         }
     69     }
     70 
     71     public void timeComplexDirect(int reps) {
     72         for (int i = 0; i < reps; i++) {
     73             mComplex.dumpDirect(mDirect, 0);
     74         }
     75     }
     76 
     77     public void timeComplexIndenting(int reps) {
     78         for (int i = 0; i < reps; i++) {
     79             mComplex.dumpIndenting(mIndenting);
     80         }
     81     }
     82 
     83     public void timePairRaw(int reps) {
     84         final int value = 1024;
     85         for (int i = 0; i < reps; i++) {
     86             mDirect.print("key=");
     87             mDirect.print(value);
     88             mDirect.print(" ");
     89         }
     90     }
     91 
     92     public void timePairIndenting(int reps) {
     93         final int value = 1024;
     94         for (int i = 0; i < reps; i++) {
     95             mIndenting.printPair("key", value);
     96         }
     97     }
     98 
     99     private static class Node {
    100         public String name;
    101         public ArrayList<Node> children;
    102 
    103         private static String[] sIndents = new String[] { "", "  ", "    ", "      ", "        " };
    104 
    105         public static Node build(String name, Node... children) {
    106             Node node = new Node();
    107             node.name = name;
    108             if (children != null && children.length > 0) {
    109                 node.children = Lists.newArrayList(children);
    110             }
    111             return node;
    112         }
    113 
    114         private void dumpSelf(PrintWriter pw) {
    115             pw.print("Node ");
    116             pw.print(name);
    117             pw.print(" first ");
    118             pw.print(512);
    119             pw.print(" second ");
    120             pw.print(1024);
    121             pw.print(" third ");
    122             pw.println(2048);
    123         }
    124 
    125         public void dumpDirect(PrintWriter pw, int depth) {
    126             pw.print(sIndents[depth]);
    127             dumpSelf(pw);
    128 
    129             if (children != null) {
    130                 for (Node child : children) {
    131                     child.dumpDirect(pw, depth + 1);
    132                 }
    133             }
    134 
    135             pw.println();
    136         }
    137 
    138         public void dumpIndenting(IndentingPrintWriter pw) {
    139             dumpSelf(pw);
    140 
    141             if (children != null) {
    142                 pw.increaseIndent();
    143                 for (Node child : children) {
    144                     child.dumpIndenting(pw);
    145                 }
    146                 pw.decreaseIndent();
    147             }
    148 
    149             pw.println();
    150         }
    151     }
    152 }
    153