001/** 002 * 003 * Copyright 2023 Florian Schmaus 004 * 005 * This file is part of smack-examples. 006 * 007 * smack-examples is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021package org.igniterealtime.smack.examples; 022 023import java.util.function.Supplier; 024 025import org.jivesoftware.smack.util.XmlStringBuilder; 026 027public class XmlStringBuilderTest { 028 static int COUNT_OUTER = 500; 029 static int COUNT_INNER = 50; 030 031 public static void main(String[] args) throws Exception { 032 test1(); 033 test2(); 034 test3(); 035 } 036 037 public static void test1() throws Exception { 038 // CHECKSTYLE:OFF 039 System.out.println("Test 1"); 040 // CHECKSTYLE:ON 041 XmlStringBuilder parent = new XmlStringBuilder(); 042 XmlStringBuilder child = new XmlStringBuilder(); 043 XmlStringBuilder child2 = new XmlStringBuilder(); 044 045 for (int i = 1; i < COUNT_OUTER; i++) { 046 XmlStringBuilder cs = new XmlStringBuilder(); 047 for (int j = 0; j < COUNT_INNER; j++) { 048 cs.append("abc"); 049 } 050 child2.append((CharSequence) cs); 051 } 052 053 child.append((CharSequence) child2); 054 parent.append((CharSequence) child); 055 056 time("test1: parent", () -> "len=" + parent.toString().length()); 057 time("test1: child", () -> "len=" + child.toString().length()); 058 time("test1: child2", () -> "len=" + child2.toString().length()); 059 } 060 061 public static void test2() throws Exception { 062 // CHECKSTYLE:OFF 063 System.out.println("Test 2: evaluate children first"); 064 // CHECKSTYLE:ON 065 XmlStringBuilder parent = new XmlStringBuilder(); 066 XmlStringBuilder child = new XmlStringBuilder(); 067 XmlStringBuilder child2 = new XmlStringBuilder(); 068 069 for (int i = 1; i < COUNT_OUTER; i++) { 070 XmlStringBuilder cs = new XmlStringBuilder(); 071 for (int j = 0; j < COUNT_INNER; j++) { 072 cs.append("abc"); 073 } 074 child2.append((CharSequence) cs); 075 } 076 077 child.append((CharSequence) child2); 078 parent.append((CharSequence) child); 079 080 time("test2: child2", () -> "len=" + child2.toString().length()); 081 time("test2: child", () -> "len=" + child.toString().length()); 082 time("test2: parent", () -> "len=" + parent.toString().length()); 083 } 084 085 public static void test3() throws Exception { 086 // CHECKSTYLE:OFF 087 System.out.println("Test 3: use append(XmlStringBuilder)"); 088 // CHECKSTYLE:ON 089 XmlStringBuilder parent = new XmlStringBuilder(); 090 XmlStringBuilder child = new XmlStringBuilder(); 091 XmlStringBuilder child2 = new XmlStringBuilder(); 092 093 for (int i = 1; i < COUNT_OUTER; i++) { 094 XmlStringBuilder cs = new XmlStringBuilder(); 095 for (int j = 0; j < COUNT_INNER; j++) { 096 cs.append("abc"); 097 } 098 child2.append(cs); 099 } 100 101 child.append(child2); 102 parent.append(child); 103 104 time("test3: parent", () -> "len=" + parent.toString().length()); 105 time("test3: child", () -> "len=" + child.toString().length()); 106 time("test3: child2", () -> "len=" + child2.toString().length()); 107 } 108 109 static void time(String name, Supplier<String> block) { 110 long start = System.currentTimeMillis(); 111 String result = block.get(); 112 long end = System.currentTimeMillis(); 113 114 // CHECKSTYLE:OFF 115 System.out.println(name + " took " + (end - start) + "ms: " + result); 116 // CHECKSTYLE:ONy 117 } 118}