KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > testcase > PerformanceTest


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.testcase;
54
55 import java.io.BufferedOutputStream JavaDoc;
56 import java.io.File JavaDoc;
57 import java.io.FileOutputStream JavaDoc;
58 import java.io.OutputStream JavaDoc;
59 import java.io.OutputStreamWriter JavaDoc;
60 import java.io.Writer JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Locale JavaDoc;
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 /**
77  * This class executes a FreeMarker template repeatedly in an endless loop.
78  * It is meant to run inside a profiler to identify potential bottlenecks.
79  * It can process either into a local file, or into a special /dev/null
80  * style output stream.
81  * @version $Id: PerformanceTest.java,v 1.18 2004/11/28 12:58:34 ddekany Exp $
82  */

83 public class PerformanceTest
84 {
85     public static void main(String JavaDoc[] args)
86     throws
87         Exception JavaDoc
88     {
89         Configuration config = new Configuration();
90         // config.setDebugMode(false);
91
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 JavaDoc f = File.createTempFile("fmPerfTest", ".txt");
96         f.deleteOnExit();
97         OutputStream JavaDoc 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 JavaDoc stream = toFile ? new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f)) : nullStream;
106             Writer JavaDoc writer = new OutputStreamWriter JavaDoc(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 JavaDoc key)
142         {
143             return this;
144         }
145         
146         public String JavaDoc 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 JavaDoc exec(List JavaDoc 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 JavaDoc key)
175         {
176             return (i & 1) == 1 ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
177         }
178
179         public String JavaDoc getAsString(Locale JavaDoc locale)
180         {
181             return Integer.toString(i);
182         }
183
184         public Number JavaDoc getAsNumber()
185         {
186             return new Integer JavaDoc(i);
187         }
188         
189         public boolean isEmpty()
190         {
191             return false;
192         }
193     }
194     
195     private static class NullStream extends OutputStream JavaDoc
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