blob: a8c51b395ee575f9898c13e241feb1dac01659c4 [file] [log] [blame]
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +01001/*
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
17package benchmarks;
18
Paul Duffinea13f822015-11-30 11:09:55 +000019import com.google.caliper.BeforeExperiment;
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010020import com.google.caliper.Param;
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010021import java.io.CharArrayWriter;
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010022import java.lang.reflect.Constructor;
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010023import java.util.Random;
Paul Duffinea13f822015-11-30 11:09:55 +000024import org.xmlpull.v1.XmlSerializer;
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010025
26
Paul Duffinea13f822015-11-30 11:09:55 +000027public class XmlSerializeBenchmark {
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010028
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")
Paul Duffinea13f822015-11-30 11:09:55 +000082 @BeforeExperiment
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010083 protected void setUp() throws Exception {
Neil Fuller458d8cb2018-07-10 18:36:31 +010084 kxmlConstructor = (Constructor) Class.forName("com.android.org.kxml2.io.KXmlSerializer")
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010085 .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++) {
Tobias Thierera5448c02016-04-21 18:12:12 +010091 dataset[i] = Double.parseDouble(splitted[i]);
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +010092 }
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 }
Wojciech Staszkiewiczc3dc3f72015-04-23 11:42:32 +0100109}