1 52 53 package freemarker.testcase; 54 55 import java.io.BufferedOutputStream ; 56 import java.io.File ; 57 import java.io.FileOutputStream ; 58 import java.io.OutputStream ; 59 import java.io.OutputStreamWriter ; 60 import java.io.Writer ; 61 import java.util.List ; 62 import java.util.Locale ; 63 64 import freemarker.template.Configuration; 65 import freemarker.template.SimpleHash; 66 import freemarker.template.Template; 67 import freemarker.template.TemplateBooleanModel; 68 import freemarker.template.TemplateCollectionModel; 69 import freemarker.template.TemplateHashModel; 70 import freemarker.template.TemplateMethodModelEx; 71 import freemarker.template.TemplateModel; 72 import freemarker.template.TemplateModelIterator; 73 import freemarker.template.TemplateNumberModel; 74 import freemarker.template.TemplateScalarModel; 75 76 83 public class PerformanceTest 84 { 85 public static void main(String [] args) 86 throws 87 Exception 88 { 89 Configuration config = new Configuration(); 90 config.setClassicCompatible(false); 92 config.setClassForTemplateLoading(PerformanceTest.class, "/freemarker/testcase"); 93 Template template = config.getTemplate("PerformanceTest.fm"); 94 boolean toFile = args.length > 0 && args[0].equals("file"); 95 File f = File.createTempFile("fmPerfTest", ".txt"); 96 f.deleteOnExit(); 97 OutputStream nullStream = new NullStream(); 98 SimpleHash h = new SimpleHash(); 99 h.put("ii", new TestSequence()); 100 h.put("j", new TestHash()); 101 h.put("k", new TestMethod()); 102 103 for(;;) 104 { 105 OutputStream stream = toFile ? new BufferedOutputStream (new FileOutputStream (f)) : nullStream; 106 Writer writer = new OutputStreamWriter (stream, "UTF-8"); 107 try 108 { 109 template.process(h, writer); 110 } 111 finally 112 { 113 writer.close(); 114 } 115 } 116 } 117 118 private static class TestSequence implements TemplateCollectionModel 119 { 120 public TemplateModelIterator iterator() 121 { 122 return new TemplateModelIterator() 123 { 124 private int i = 0; 125 126 public TemplateModel next() 127 { 128 return new TestI(i++); 129 } 130 131 public boolean hasNext() 132 { 133 return i < 1000; 134 } 135 }; 136 }; 137 } 138 139 private static class TestHash implements TemplateHashModel, TemplateScalarModel 140 { 141 public TemplateModel get(String key) 142 { 143 return this; 144 } 145 146 public String getAsString() 147 { 148 return "j"; 149 } 150 151 public boolean isEmpty() 152 { 153 return false; 154 } 155 } 156 157 private static class TestMethod implements TemplateMethodModelEx 158 { 159 public Object exec(List arguments) 160 { 161 return arguments.get(0); 162 } 163 } 164 165 private static class TestI implements TemplateHashModel, TemplateNumberModel 166 { 167 private final int i; 168 169 TestI(int i) 170 { 171 this.i = i; 172 } 173 174 public TemplateModel get(String key) 175 { 176 return (i & 1) == 1 ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; 177 } 178 179 public String getAsString(Locale locale) 180 { 181 return Integer.toString(i); 182 } 183 184 public Number getAsNumber() 185 { 186 return new Integer (i); 187 } 188 189 public boolean isEmpty() 190 { 191 return false; 192 } 193 } 194 195 private static class NullStream extends OutputStream 196 { 197 public void close() 198 { 199 } 200 201 public void flush() 202 { 203 } 204 205 public void write(byte[] arg0, int arg1, int arg2) 206 { 207 } 208 209 public void write(byte[] arg0) 210 { 211 } 212 213 public void write(int arg0) 214 { 215 } 216 } 217 } 218 | Popular Tags |