Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
efficacy committed May 5, 2011
0 parents commit d2d7445
Show file tree
Hide file tree
Showing 17 changed files with 396 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .classpath
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>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/*
17 changes: 17 additions & 0 deletions .project
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>
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
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
26 changes: 26 additions & 0 deletions docs/results.txt
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 added lib/commons-collections-3.2.1.jar
Binary file not shown.
Binary file added lib/commons-lang-2.6.jar
Binary file not shown.
Binary file added lib/freemarker.jar
Binary file not shown.
Binary file added lib/velocity-1.7.jar
Binary file not shown.
69 changes: 69 additions & 0 deletions src/test/PerformanceTestCase.java
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();
}
}
57 changes: 57 additions & 0 deletions src/test/SingleTemplateTest.java
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");
}
}
31 changes: 31 additions & 0 deletions src/wrapper/AbstractTemplateSystem.java
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;
}
}
37 changes: 37 additions & 0 deletions src/wrapper/EmoTemplateSystem.java
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);
}

}
51 changes: 51 additions & 0 deletions src/wrapper/FreeMarkerTemplateSystem.java
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);
}
}
34 changes: 34 additions & 0 deletions src/wrapper/StringtreeTemplateSystem.java
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);
}

}
8 changes: 8 additions & 0 deletions src/wrapper/TemplateSystem.java
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);
}
Loading

0 comments on commit d2d7445

Please sign in to comment.