-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d2d7445
Showing
17 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry combineaccessrules="false" kind="src" path="/emo"/> | ||
<classpathentry combineaccessrules="false" kind="src" path="/stringtree"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/> | ||
<classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/> | ||
<classpathentry kind="lib" path="lib/commons-lang-2.6.jar"/> | ||
<classpathentry kind="lib" path="lib/velocity-1.7.jar"/> | ||
<classpathentry kind="lib" path="lib/freemarker.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bin/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>templatetests</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#Tue May 03 20:23:26 BST 2011 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pervasive template caching | ||
|
||
plain text (10000): st=500 emo=15 | ||
single subst (10000): st=422 emo=16 | ||
conditional (10000): st=453 emo=16 | ||
bean call (10000): st=484 emo=47 | ||
iteration (10000): st=547 emo=78 | ||
|
||
token tree refactoring | ||
|
||
plain text (10000): st=468 emo=16 | ||
single subst (10000): st=390 emo=16 | ||
conditional (10000): st=437 emo=16 | ||
bean call (10000): st=469 emo=47 | ||
iteration (10000): st=531 emo=78 | ||
|
||
added velocity and freemarker for comparison | ||
|
||
plain text(10000): st=469 emo=16 vel=703 fm=203 | ||
single subst(10000): st=437 emo=16 vel=765 fm=79 | ||
conditional(10000): st=453 emo=16 vel=844 fm=62 | ||
bean call(10000): st=484 emo=47 vel=1031 fm=94 | ||
iteration(10000): st=547 emo=78 [*]vel=1140 fm=157 | ||
|
||
[*] velocity template insists on adding a trailing comma to the list. Sigh. | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package test; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import wrapper.EmoTemplateSystem; | ||
import wrapper.FreeMarkerTemplateSystem; | ||
import wrapper.StringtreeTemplateSystem; | ||
import wrapper.TemplateSystem; | ||
import wrapper.VelocityTemplateSystem; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public abstract class PerformanceTestCase extends TestCase { | ||
protected StringtreeTemplateSystem st; | ||
protected EmoTemplateSystem emo; | ||
protected VelocityTemplateSystem vel; | ||
protected FreeMarkerTemplateSystem fm; | ||
|
||
Collection<TemplateSystem> systems; | ||
|
||
public void setUp() { | ||
st = new StringtreeTemplateSystem(); | ||
emo = new EmoTemplateSystem(); | ||
vel = new VelocityTemplateSystem(); | ||
fm = new FreeMarkerTemplateSystem(); | ||
|
||
systems = new ArrayList<TemplateSystem>(); | ||
systems.addAll(Arrays.asList(st, emo, vel, fm)); | ||
} | ||
|
||
protected void putTemplate(String name, String template) { | ||
for (TemplateSystem system : systems) { | ||
system.putTemplate(name, template); | ||
} | ||
} | ||
|
||
protected void putContext(String key, Object value) { | ||
for (TemplateSystem system : systems) { | ||
system.putContext(key, value); | ||
} | ||
} | ||
|
||
protected void check(String label, String expected, String tplName) { | ||
check(label, 1, expected, tplName); | ||
} | ||
|
||
protected void check(String label, int n, String expected, String tplName) { | ||
Map<String, Long> results = new LinkedHashMap<String, Long>(); | ||
System.out.print(label); | ||
System.out.print("("); | ||
System.out.print(n); | ||
System.out.print("): "); | ||
for (TemplateSystem system : systems) { | ||
String name = system.getName(); | ||
long result = system.check(label, n, expected, tplName); | ||
System.out.print(name); | ||
System.out.print("="); | ||
System.out.print(result); | ||
System.out.print(" "); | ||
results.put(name, result); | ||
} | ||
System.out.println(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package test; | ||
|
||
import java.util.Arrays; | ||
|
||
public class SingleTemplateTest extends PerformanceTestCase { | ||
|
||
public void testAbsorbSetupTime() { | ||
assertEquals("hello", "hello"); | ||
} | ||
|
||
public void testPlainText() { | ||
putTemplate("ugh", "hello there!"); | ||
check("plain text", 10000, "hello there!", "ugh"); | ||
} | ||
|
||
public void testSingleSubstitution() { | ||
putContext("name", "Frank"); | ||
putTemplate("ugh", "hello there, ${name}!"); | ||
check("single subst", 10000, "hello there, Frank!", "ugh"); | ||
} | ||
|
||
public void testConditionalSubstitution() { | ||
putContext("yes", Boolean.TRUE); | ||
|
||
st.putTemplate("ugh", "hello there, ${yes?'Frank':'Margaret'}!"); | ||
|
||
emo.putTemplate("ugh", "hello there, ${yes?'Frank':'Margaret'}!"); | ||
|
||
vel.putTemplate("ugh", "hello there, #if($yes)Frank#else#**#Margaret#end!"); | ||
|
||
fm.putTemplate("ugh", "hello there, <#if yes>Frank<#else>Margaret</#if>!"); | ||
|
||
check("conditional", 10000, "hello there, Frank!", "ugh"); | ||
} | ||
|
||
public void testBeanCall() { | ||
putContext("obj", this); | ||
putTemplate("ugh", "name=${obj.name}"); | ||
check("bean call", 10000, "name=testBeanCall", "ugh"); | ||
} | ||
|
||
public void testIteration() { | ||
putContext("family", Arrays.asList("Frank", "Margaret", "Elizabeth", "Katherine")); | ||
|
||
st.putTemplate("person", "[${this}]"); | ||
st.putTemplate("ugh", "hello there, ${family*person/','}!"); | ||
|
||
emo.putTemplate("person", "[${this}]"); | ||
emo.putTemplate("ugh", "hello there, ${family*person/','}!"); | ||
|
||
vel.putTemplate("ugh", "hello there, #foreach($person in $family)[$person],#end!"); | ||
|
||
fm.putTemplate("ugh", "hello there, <#list family as person>[${person}]<#if person_has_next>,</#if></#list>!"); | ||
|
||
check("iteration", 10000, "hello there, [Frank],[Margaret],[Elizabeth],[Katherine]!", "ugh"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package wrapper; | ||
|
||
import org.stringtree.timing.StopWatch; | ||
|
||
public abstract class AbstractTemplateSystem implements TemplateSystem { | ||
|
||
protected String name; | ||
protected StopWatch clock; | ||
protected String result; | ||
|
||
public AbstractTemplateSystem(String name) { | ||
this.name = name; | ||
this.clock = new StopWatch(); | ||
} | ||
|
||
@Override public long check(String label, int n, String expected, String tplName) { | ||
clock.reset(); | ||
for (int i = 0; i < n; ++i) { | ||
result = expand(tplName); | ||
} | ||
clock.stop(); | ||
if (!expected.equals(result)) System.out.print("[*]"); | ||
return clock.total(); | ||
} | ||
|
||
protected abstract String expand(String tplName); | ||
|
||
@Override public String getName() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package wrapper; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.stringtree.context.Context; | ||
import org.stringtree.context.MapContext; | ||
import org.stringtree.solomon.EasySolomon; | ||
import org.stringtree.solomon.Session; | ||
|
||
public class EmoTemplateSystem extends AbstractTemplateSystem { | ||
protected Map<String, String> templates; | ||
protected Context<Object> context; | ||
protected Session session; | ||
protected EasySolomon templater; | ||
|
||
public EmoTemplateSystem() { | ||
super("emo"); | ||
templates = new HashMap<String, String>(); | ||
context = new MapContext<Object>(); | ||
templater = new EasySolomon(new MapContext<String>(templates), context); | ||
session = new Session(); | ||
} | ||
|
||
protected String expand(String tplName) { | ||
return templater.toString(tplName, session); | ||
} | ||
|
||
@Override public void putContext(String key, Object value) { | ||
context.put(key, value); | ||
} | ||
|
||
@Override public void putTemplate(String name, String template) { | ||
templates.put(name, template); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package wrapper; | ||
|
||
import freemarker.cache.StringTemplateLoader; | ||
import freemarker.template.Configuration; | ||
import freemarker.template.DefaultObjectWrapper; | ||
import freemarker.template.Template; | ||
import freemarker.template.TemplateException; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class FreeMarkerTemplateSystem extends AbstractTemplateSystem { | ||
|
||
StringTemplateLoader stringLoader; | ||
Configuration cfg; | ||
Map<String, Object> root; | ||
|
||
public FreeMarkerTemplateSystem() { | ||
super("fm"); | ||
cfg = new Configuration(); | ||
stringLoader = new StringTemplateLoader(); | ||
|
||
cfg.setTemplateLoader(stringLoader); | ||
cfg.setObjectWrapper(new DefaultObjectWrapper()); | ||
root = new HashMap<String, Object>(); | ||
} | ||
|
||
@Override protected String expand(String tplName) { | ||
StringWriter writer = new StringWriter(); | ||
try { | ||
Template temp = cfg.getTemplate(tplName); | ||
temp.process(root, writer); | ||
writer.flush(); | ||
} catch (TemplateException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return writer.toString(); | ||
} | ||
|
||
@Override public void putContext(String key, Object value) { | ||
root.put(key, value); | ||
} | ||
|
||
@Override public void putTemplate(String name, String template) { | ||
stringLoader.putTemplate(name, template); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package wrapper; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.stringtree.fetcher.MapFetcher; | ||
import org.stringtree.finder.MapStringKeeper; | ||
import org.stringtree.template.EasyTemplater; | ||
|
||
public class StringtreeTemplateSystem extends AbstractTemplateSystem { | ||
protected Map<String, Object> templates; | ||
protected MapStringKeeper context; | ||
protected EasyTemplater templater; | ||
|
||
public StringtreeTemplateSystem() { | ||
super("st"); | ||
templates = new HashMap<String, Object>(); | ||
context = new MapStringKeeper(); | ||
templater = new EasyTemplater(new MapFetcher(templates), context); | ||
} | ||
|
||
@Override protected String expand(String tplName) { | ||
return templater.toString(tplName); | ||
} | ||
|
||
@Override public void putContext(String key, Object value) { | ||
context.put(key, value); | ||
} | ||
|
||
@Override public void putTemplate(String name, String template) { | ||
templates.put(name, template); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package wrapper; | ||
|
||
public interface TemplateSystem { | ||
String getName(); | ||
void putTemplate(String name, String template); | ||
void putContext(String key, Object value); | ||
long check(String label, int n, String expected, String tplName); | ||
} |
Oops, something went wrong.