diff options
author | Jeff Sharkey <jsharkey@android.com> | 2013-03-26 17:37:46 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2013-03-26 20:16:44 -0700 |
commit | 8fa9bb5210e3c48ad34cbd6db880ea12de4baca9 (patch) | |
tree | 6af68f1da0aa44a6760f7e3a087230f57d0759eb /core/tests | |
parent | 05191053545065c9c71afae173d3ab42a2d947fa (diff) | |
download | frameworks_base-8fa9bb5210e3c48ad34cbd6db880ea12de4baca9.zip frameworks_base-8fa9bb5210e3c48ad34cbd6db880ea12de4baca9.tar.gz frameworks_base-8fa9bb5210e3c48ad34cbd6db880ea12de4baca9.tar.bz2 |
Benchmarks for IndentingPrintWriter.
Current timings on a mako listed below, which show minimal overhead
compared to writing directly. The increased code readability and
automatic indenting is worth the overhead.
Also worth noting is that writing concatenated strings is
substantially faster than printing components separately.
benchmark us linear runtime
ComplexDirect 10712.7 =========================
ComplexIndenting 12623.7 ==============================
PairIndenting 45.8 =
PairRaw 82.1 =
SimpleDirect 282.2 =
SimpleIndenting 294.4 =
Change-Id: I7c38690c14b017fa46248ebb0be69f6beff03371
Diffstat (limited to 'core/tests')
-rw-r--r-- | core/tests/benchmarks/src/com/android/internal/util/IndentingPrintWriterBenchmark.java | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/core/tests/benchmarks/src/com/android/internal/util/IndentingPrintWriterBenchmark.java b/core/tests/benchmarks/src/com/android/internal/util/IndentingPrintWriterBenchmark.java new file mode 100644 index 0000000..34c73e8 --- /dev/null +++ b/core/tests/benchmarks/src/com/android/internal/util/IndentingPrintWriterBenchmark.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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. + */ + +package com.android.internal.util; + +import com.google.android.collect.Lists; +import com.google.caliper.SimpleBenchmark; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; + +public class IndentingPrintWriterBenchmark extends SimpleBenchmark { + + private PrintWriter mDirect; + private IndentingPrintWriter mIndenting; + + private Node mSimple; + private Node mComplex; + + @Override + protected void setUp() throws IOException { + final FileOutputStream os = new FileOutputStream(new File("/dev/null")); + mDirect = new PrintWriter(os); + mIndenting = new IndentingPrintWriter(mDirect, " "); + + final Node manyChildren = Node.build("ManyChildren", Node.build("1"), Node.build("2"), + Node.build("3"), Node.build("4"), Node.build("5"), Node.build("6"), Node.build("7"), + Node.build("8"), Node.build("9"), Node.build("10")); + + mSimple = Node.build("RED"); + mComplex = Node.build("PARENT", Node.build("RED"), Node.build("GREEN", + Node.build("BLUE", manyChildren, manyChildren), manyChildren, manyChildren), + manyChildren); + } + + @Override + protected void tearDown() { + mIndenting.close(); + mIndenting = null; + mDirect = null; + } + + public void timeSimpleDirect(int reps) { + for (int i = 0; i < reps; i++) { + mSimple.dumpDirect(mDirect, 0); + } + } + + public void timeSimpleIndenting(int reps) { + for (int i = 0; i < reps; i++) { + mSimple.dumpIndenting(mIndenting); + } + } + + public void timeComplexDirect(int reps) { + for (int i = 0; i < reps; i++) { + mComplex.dumpDirect(mDirect, 0); + } + } + + public void timeComplexIndenting(int reps) { + for (int i = 0; i < reps; i++) { + mComplex.dumpIndenting(mIndenting); + } + } + + public void timePairRaw(int reps) { + final int value = 1024; + for (int i = 0; i < reps; i++) { + mDirect.print("key="); + mDirect.print(value); + mDirect.print(" "); + } + } + + public void timePairIndenting(int reps) { + final int value = 1024; + for (int i = 0; i < reps; i++) { + mIndenting.printPair("key", value); + } + } + + private static class Node { + public String name; + public ArrayList<Node> children; + + private static String[] sIndents = new String[] { "", " ", " ", " ", " " }; + + public static Node build(String name, Node... children) { + Node node = new Node(); + node.name = name; + if (children != null && children.length > 0) { + node.children = Lists.newArrayList(children); + } + return node; + } + + private void dumpSelf(PrintWriter pw) { + pw.print("Node "); + pw.print(name); + pw.print(" first "); + pw.print(512); + pw.print(" second "); + pw.print(1024); + pw.print(" third "); + pw.println(2048); + } + + public void dumpDirect(PrintWriter pw, int depth) { + pw.print(sIndents[depth]); + dumpSelf(pw); + + if (children != null) { + for (Node child : children) { + child.dumpDirect(pw, depth + 1); + } + } + + pw.println(); + } + + public void dumpIndenting(IndentingPrintWriter pw) { + dumpSelf(pw); + + if (children != null) { + pw.increaseIndent(); + for (Node child : children) { + child.dumpIndenting(pw); + } + pw.decreaseIndent(); + } + + pw.println(); + } + } +} |