Home | History | Annotate | Download | only in benchmarks
      1 /*
      2  * Copyright (C) 2015 Google Inc.
      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 benchmarks;
     18 
     19 import com.google.caliper.BeforeExperiment;
     20 import com.google.caliper.Param;
     21 import java.io.CharArrayWriter;
     22 import java.lang.reflect.Constructor;
     23 import java.util.Random;
     24 import org.xmlpull.v1.XmlSerializer;
     25 
     26 
     27 public class XmlSerializeBenchmark {
     28 
     29     @Param( {"0.99 0.7 0.7 0.7 0.7 0.7",
     30             "0.999 0.3 0.3 0.95 0.9 0.9"})
     31     String datasetAsString;
     32 
     33     @Param( { "854328", "312547"} )
     34     int seed;
     35 
     36     double[] dataset;
     37     private Constructor<? extends XmlSerializer> kxmlConstructor;
     38     private Constructor<? extends XmlSerializer> fastConstructor;
     39 
     40     private void serializeRandomXml(Constructor<? extends XmlSerializer> ctor, long seed)
     41             throws Exception {
     42         double contChance = dataset[0];
     43         double levelUpChance = dataset[1];
     44         double levelDownChance = dataset[2];
     45         double attributeChance = dataset[3];
     46         double writeChance1 = dataset[4];
     47         double writeChance2 = dataset[5];
     48 
     49         XmlSerializer serializer = (XmlSerializer) ctor.newInstance();
     50 
     51         CharArrayWriter w = new CharArrayWriter();
     52         serializer.setOutput(w);
     53         int level = 0;
     54         Random r = new Random(seed);
     55         char[] toWrite = {'a','b','c','d','s','z'};
     56         serializer.startDocument("UTF-8", true);
     57         while(r.nextDouble() < contChance) {
     58             while(level > 0 && r.nextDouble() < levelUpChance) {
     59                 serializer.endTag("aaaaaa", "bbbbbb");
     60                 level--;
     61             }
     62             while(r.nextDouble() < levelDownChance) {
     63                 serializer.startTag("aaaaaa", "bbbbbb");
     64                 level++;
     65             }
     66             serializer.startTag("aaaaaa", "bbbbbb");
     67             level++;
     68             while(r.nextDouble() < attributeChance) {
     69                 serializer.attribute("aaaaaa", "cccccc", "dddddd");
     70             }
     71             serializer.endTag("aaaaaa", "bbbbbb");
     72             level--;
     73             while(r.nextDouble() < writeChance1)
     74                 serializer.text(toWrite, 0, 5);
     75             while(r.nextDouble() < writeChance2)
     76                 serializer.text("Textxtsxtxtxt ");
     77         }
     78         serializer.endDocument();
     79     }
     80 
     81     @SuppressWarnings("unchecked")
     82     @BeforeExperiment
     83     protected void setUp() throws Exception {
     84         kxmlConstructor = (Constructor) Class.forName("org.kxml2.io.KXmlSerializer")
     85                 .getConstructor();
     86         fastConstructor = (Constructor) Class.forName("com.android.internal.util.FastXmlSerializer")
     87                 .getConstructor();
     88         String[] splitted = datasetAsString.split(" ");
     89         dataset = new double[splitted.length];
     90         for (int i = 0; i < splitted.length; i++) {
     91             dataset[i] = Double.parseDouble(splitted[i]);
     92         }
     93     }
     94 
     95     private void internalTimeSerializer(Constructor<? extends XmlSerializer> ctor, int reps)
     96             throws Exception {
     97         for (int i = 0; i < reps; i++) {
     98             serializeRandomXml(ctor, seed);
     99         }
    100     }
    101 
    102     public void timeKxml(int reps) throws Exception {
    103         internalTimeSerializer(kxmlConstructor, reps);
    104     }
    105 
    106     public void timeFast(int reps) throws Exception {
    107         internalTimeSerializer(fastConstructor, reps);
    108     }
    109 }
    110