java模版引擎freemarker和velocity的對比

**

一、是否維護

**
1.Freemarker
在freemarker的官網中看到,fm在2015-7-1後就託管給apache基金會了,後面的bug修改都來自於git。最近的一次更新在2016年6月,順便值得一提的是,這個版本是最後一個版本了。
Around 2015-07-01, FreeMarker was voted in into the Apache Incubator, and the project (all code from which the releases and the Web site are created, along with the right for using the “FreeMarker” product name) was granted to the Apache Software Foundation by the earlier owners. The license remains Apache License, Version 2.0. In 2015-09-02, the main code base was imported from GitHub into the Apache Software Foundation infrastructure, where development continues.

2.Velocity
Velocity早在幾年前的時候就已經託管給apache了,在git上我們可以看到,最新的更新是2010年的11月,說明velocity很多最新的bug已經沒有人處理了,

當然我們一定要用的話,還是有很多大神對velocity的bug進行了修改並提交了git,所以實質上我們要用的話,可以對git上的版本進行打包到我們的私服上。當然,如果大神改出了新bug也是有可能的。

**

二、性能對比。

**
1.針對10W條數據的模版生成對比
生成的時間單位都是ms,fm用是2.3.25-incubating,vm用的是1.7
Fm vm
1 195 229
2 121 223
3 122 221
4 120 733
5 120 232
6 150 396
7 150 326
8 273 198
平均
可以從數據中很輕易的看出fm的性能和穩定性都比vm好。
下面是測試的代碼
vm:

public void test(){
        VelocityEngine engine = new VelocityEngine();
        Properties p = new Properties();
        p.setProperty(VelocityEngine.RESOURCE_LOADER,"class");
        p.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        engine.init(p);

        Template template = engine.getTemplate("vm/test.vm","UTF-8");

        VelocityContext context = new VelocityContext();

        context.put("name","陳善奔");
        context.put("age","18");
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < 100000; i++) {
            list.add(UUID.randomUUID().toString());
        }
        context.put("list",list);

        long one = System.currentTimeMillis();
        StringWriter writer = new StringWriter();
        System.out.println(one);
        template.merge(context,writer);
        System.out.println(System.currentTimeMillis() - one);
        //System.out.println(writer.toString());
    }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>測試</title>

</head>

<body>
<div class="main">
    <h2>測試</h2>
    <div>
        ${name},年齡${age}
        #foreach($uuid in ${list})
            <div>${uuid}</div>
        #end
    </div>
</div>
</body>

</html>  

freemarker的代碼:

public void test() throws TemplateException {
        Configuration cfg = new Configuration();
        ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(),"/ftl");
//        cfg.setDirectoryForTemplateLoading("ftl/");
        TemplateLoader[] loaders = new TemplateLoader[]{ctl};
        MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
        cfg.setTemplateLoader(mtl);
        cfg.setEncoding(Locale.CHINA,"utf-8");
        cfg.setDefaultEncoding("utf-8");

        Map<String,Object> context = new HashMap<>();
        context.put("name","陳善奔");
        context.put("age","18");
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < 100000; i++) {
            list.add(UUID.randomUUID().toString());
        }
        context.put("list",list);
        try {
            StringWriter sw = new StringWriter();
            Template template = cfg.getTemplate("test.ftl", "utf-8");
            long one = System.currentTimeMillis();
            System.out.println(one);
            template.process(context,sw);
            System.out.println(System.currentTimeMillis()-one);
            //System.out.println(sw);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>測試</title>
</head>

<body>
<div class="main">
    <h2>測試</h2>
    <div>
    ${name},年齡${age}
        <#list list as uuid>
        <div>${uuid}</div>
        </#list>
    </div>
</div>
</body>

</html>

總結

1.針對維護性來說,fm比vm來得更可靠一些。
2.針對性能來說,fm比vm更穩定,更快,但實質上,兩者還是在同一個數量級上。如果是對併發量特別高的還是採用fm吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章