1/*
2 * Copyright (C) 2015 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
17package android.text;
18
19import com.google.caliper.AfterExperiment;
20import com.google.caliper.BeforeExperiment;
21import com.google.caliper.Benchmark;
22import com.google.caliper.Param;
23
24public class SpannableStringBuilderBenchmark {
25
26    @Param({"android.text.style.ImageSpan",
27            "android.text.style.ParagraphStyle",
28            "android.text.style.CharacterStyle",
29            "java.lang.Object"})
30    private String paramType;
31
32    @Param({"1", "4", "16"})
33    private String paramStringMult;
34
35    private Class clazz;
36    private SpannableStringBuilder builder;
37
38    @BeforeExperiment
39    protected void setUp() throws Exception {
40        clazz = Class.forName(paramType);
41        int strSize = Integer.parseInt(paramStringMult);
42        StringBuilder strBuilder = new StringBuilder();
43        for (int i = 0; i < strSize; i++) {
44            strBuilder.append(TEST_STRING);
45        }
46        builder = new SpannableStringBuilder(Html.fromHtml(strBuilder.toString()));
47    }
48
49    @AfterExperiment
50    protected void tearDown() {
51        builder.clear();
52        builder = null;
53    }
54
55    @Benchmark
56    public void timeGetSpans(int reps) throws Exception {
57        for (int i = 0; i < reps; i++) {
58            builder.getSpans(0, builder.length(), clazz);
59        }
60    }
61
62    //contains 0 ImageSpans, 2 ParagraphSpans, 53 CharacterStyleSpans
63    public static String TEST_STRING =
64            "<p><span><a href=\"http://android.com\">some link</a></span></p>\n" +
65            "<h1 style=\"margin: 0.0px 0.0px 10.0px 0.0px; line-height: 64.0px; font: 62.0px " +
66                    "'Helvetica Neue Light'; color: #000000; \"><span>some title</span></h1>\n" +
67            "<p><span>by <a href=\"http://android.com\"><span>some name</span></a>\n" +
68            "  <a href=\"https://android.com\"><span>some text</span></a></span></p>\n" +
69            "<p><span>some date</span></p>\n" +
70            "<table cellspacing=\"0\" cellpadding=\"0\">\n" +
71            "  <tbody><tr><td valign=\"bottom\">\n" +
72            "        <p><span><blockquote>a paragraph</blockquote></span><br></p>\n" +
73            "  </tbody></tr></td>\n" +
74            "</table>\n" +
75            "<h2 style=\"margin: 0.0px 0.0px 0.0px 0.0px; line-height: 38.0px; font: 26.0px " +
76                    "'Helvetica Neue Light'; color: #262626; -webkit-text-stroke: #262626\">" +
77                    "<span>some header two</span></h2>\n" +
78            "<p><span>Lorem ipsum dolor concludaturque. </span></p>\n" +
79            "<p><span></span><br></p>\n" +
80            "<p><span>Vix te doctus</span></p>\n" +
81            "<p><span><b>Error mel</b></span><span>, est ei. <a href=\"http://andorid.com\">" +
82                    "<span>asda</span></a> ullamcorper eam.</span></p>\n" +
83            "<p><span>adversarium <a href=\"http://android.com\"><span>efficiantur</span></a>, " +
84                    "mea te.</span></p>\n" +
85            "<p><span></span><br></p>\n" +
86            "<h1>Testing display of HTML elements</h1>\n" +
87            "<h2>2nd level heading</h2>\n" +
88            "<p>test paragraph.</p>\n" +
89            "<h3>3rd level heading</h3>\n" +
90            "<p>test paragraph.</p>\n" +
91            "<h4>4th level heading</h4>\n" +
92            "<p>test paragraph.</p>\n" +
93            "<h5>5th level heading</h5>\n" +
94            "<p>test paragraph.</p>\n" +
95            "<h6>6th level heading</h6>\n" +
96            "<p>test paragraph.</p>\n" +
97            "<h2>level elements</h2>\n" +
98            "<p>a normap paragraph(<code>p</code> element).\n" +
99            "  with some <strong>strong</strong>.</p>\n" +
100            "<div>This is a <code>div</code> element. </div>\n" +
101            "<blockquote><p>This is a block quotation with some <em>style</em></p></blockquote>\n" +
102            "<address>an address element</address>\n" +
103            "<h2>Text-level markup</h2>\n" +
104            "<ul>\n" +
105            "  <li> <abbr title=\"Cascading Style Sheets\">CSS</abbr> (an abbreviation;\n" +
106            "    <code>abbr</code> markup used)\n" +
107            "  <li> <acronym title=\"radio detecting and ranging\">radar</acronym>\n" +
108            "  <li> <b>bolded</b>\n" +
109            "  <li> <big>big thing</big>\n" +
110            "  <li> <font size=6>large size</font>\n" +
111            "  <li> <font face=Courier>Courier font</font>\n" +
112            "  <li> <font color=red>red text</font>\n" +
113            "  <li> <cite>Origin of Species</cite>\n" +
114            "  <li> <code>a[i] = b[i] + c[i);</code>\n" +
115            "  <li> some <del>deleted</del> text\n" +
116            "  <li> an <dfn>octet</dfn> is an\n" +
117            "  <li> this is <em>very</em> simple\n" +
118            "  <li> <i lang=\"la\">Homo sapiens</i>\n" +
119            "  <li> some <ins>inserted</ins> text\n" +
120            "  <li> type <kbd>yes</kbd> when\n" +
121            "  <li> <q>Hello!</q>\n" +
122            "  <li> <q>She said <q>Hello!</q></q>\n" +
123            "  <li> <samp>ccc</samp>\n" +
124            "  <li> <small>important</small>\n" +
125            "  <li> <strike>overstruck</strike>\n" +
126            "  <li> <strong>this is highlighted text</strong>\n" +
127            "  <li> <code>sub</code> and\n" +
128            "    <code>sup</code> x<sub>1</sub> and H<sub>2</sub>O\n" +
129            "    M<sup>lle</sup>, 1<sup>st</sup>, e<sup>x</sup>, sin<sup>2</sup> <i>x</i>,\n" +
130            "    e<sup>x<sup>2</sup></sup> and f(x)<sup>g(x)<sup>a+b+c</sup></sup>\n" +
131            "    (where 2 and a+b+c should appear as exponents of exponents).\n" +
132            "  <li> <tt>text in monospace font</tt>\n" +
133            "  <li> <u>underlined</u> text\n" +
134            "  <li> <code>cat</code> <var>filename</var> displays the\n" +
135            "    the <var>filename</var>.\n" +
136            "</ul>\n";
137
138}
139