1 Beetl
对于这三种模板的介绍可以在oschina上查看,对于前两者比较熟悉,对Thymeleaf是新的认识,Thymeleaf有一个地方本人很喜欢,模板页面静态或者动态打开可以正常显示,方便前端测试和后端分离开发,下面是对三者的性能测试。
通过 @闲.大赋 ,找到了测试工具TEB, http://git.oschina.net/kiang/teb @kiang
__________________________________________________________________________________________________________________________
发现Beetl和FreeMarker测试弄能代码都已经实现,于是对Thymeleaf进行了添加, 这里本人第一次用Thymeleaf所以不清楚对Thymeleaf配置是否对它性能产生了影响,下面直接代码:
package kiang.tei;
import kiang.teb.TebEngine;
import kiang.teb.TebModel;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.FileTemplateResolver;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* @author eyeLee(yinjun622@163.com)
* Time 2014/11/1.
*/
public class Thymeleaf implements TebEngine {
private TemplateEngine engine;
@Override
public TebEngine init(Properties properties) throws Exception {
FileTemplateResolver templateResolver = new FileTemplateResolver();
templateResolver.setPrefix("./src/kiang/tpl/");
templateResolver.setSuffix(".tpl");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setTemplateMode("XHTML");
engine = new TemplateEngine();
engine.setTemplateResolver(templateResolver);
engine.initialize();
return this;
}
@Override
public void test(Map arguments, Writer writer) throws Exception {
Context ctx = new Context();
ctx.setVariables(arguments);
engine.process("thymeleaf", ctx, writer);
}
@Override
public void test(Map arguments, OutputStream output) throws Exception {
}
@Override
public void shut() throws Exception {
}
@Override
public boolean isBinarySupport() {
return false;
}
public static void main(String args[]) throws Exception {
String source="UTF-8", target = "UTF-8";
Writer writer = new OutputStreamWriter(System.out, target);
Map data = new HashMap();
data.put("target", target);
data.put("models", TebModel.dummyModels(20));
Properties properties = new Properties();
properties.setProperty("source", source);
properties.setProperty("target", target);
properties.setProperty("binary", String.valueOf(true));
TebEngine engine = new Thymeleaf().init(properties);
engine.test(data, writer);
writer.flush();
engine.shut();
}
}
页面代码:
<html>
<head>
<title>Thymeleaf!!!!!</title>
<meta http-equiv="Content-Type" content="text/html;" th:charset="${target}"/>
<style type="text/css">
body { font-size: 10pt; color: #333333; }
thead { font-weight: bold; background-color: #C8FBAF; }
td { font-size: 10pt; text-align: center; }
.odd { background-color: #F3DEFB; }
.even { background-color: #EFFFF8; }
</style>
</head>
<body>
<h1>Template Engine Benchmark - Thymeleaf!!!!!</h1>
<table>
<thead>
<tr>
<th>序号</th>
<th>编码</th>
<th>名称</th>
<th>日期</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr th:each="model : ${models}" th:class="${modelStat.odd}? 'odd' : 'even'" >
<td th:text="${modelStat.index}"></td>
<td th:text="${model.code}"></td>
<td th:text="${model.name}"></td>
<td th:text="${model.date}"></td>
<td th:text="${model.value}"></td>
<td th:if="${model.value} > 105.5" style="color: red" th:text="${model.value}+'%'"></td>
<td th:unless="${model.value} > 105.5" style="color: blue" th:text="${model.value}+'%'"></td>
</tr>
</tbody>
</table>
</body>
</html>
在TEB配置文件中添加Thymeleaf项:
#Thymeleaf
thy.name=Thymeleaf 2.1.3
thy.site=http://thymeleaf.com
thy.test=kiang.tei.Thymeleaf
完整代码地址:http://git.oschina.net/yinjun622/teb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
测试结果:


TPS : 引擎的吞吐量, 单位时间内引擎渲染次数, 单位: 次/秒
Time : 引擎全部渲染的执行时间, 单位: 毫秒
OnceIo : 引擎单次渲染的IO次数, 单位: 次
MassIo : 引擎全部渲染的IO次数, 单位: 次
OnceOut : 引擎单次渲染的输出字节(字符)数, 单位: 字节/字符
MassOut : 引擎全部渲染的输出字节(字符)数, 单位: 字节/字符
PermMem : 内存消耗, 内存取新生代之外的内存(新生代内存会被很快回收), 单位: 字节
通过测试结果发现,Thymeleaf与beetl不在一个数量级上,在并发和渲染时间上有巨大的差距