简体   繁体   English

Freemarker 表达语言在 java class 中使用

[英]Freemarker expression language using in java class

I'm a junior and got a task.我是小学生,有任务。 I need to write simple app to test performance of Freemarker expression.我需要编写简单的应用程序来测试 Freemarker 表达式的性能。

Map<String, Object> data = new HashMap<String, Object>();
data.put("firstString", "13");
data.put("secondString", "17");

Condition: ${(firstString + secondString)?number};条件:${(firstString + secondString)?number};

The goal is repeat the process 100.000 times (parse template, set variables, evaluate template) and evaluate performance in milliseconds of the process.目标是重复该过程 100.000 次(解析模板、设置变量、评估模板)并以毫秒为单位评估性能。

public static void main(String[] args) throws IOException {

Configuration cfg = new Configuration();
   try {
        long startTime = System.nanoTime();
        // Connect to Freemarker template
        Template template = cfg.getTemplate("src/concatenation.ftl");

        // Create data to use in expresion String to number = ${(firstString + secondString)?number}
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("firstString", "13");
        data.put("secondString", "17");


        for (int i = 0; i < 100_000 ; i++) {
            // Console output
            Writer out = new OutputStreamWriter(System.out);
            template.process(data, out);
            out.flush();
        }

        // Measure performance time
        long endTime = System.nanoTime();
        long duration = (endTime - startTime);
        System.out.println();
        System.out.println("Parse template for 100.000 objects completed in " + duration / 1000000 + " milliseconds");

    } catch (IOException e) {
        e.printStackTrace();
    } catch (TemplateException e) {
        e.printStackTrace();
    }
}

Need help to figure out how to use Freemarker expression in main class, to prevent reading from external file to be more precisely in performance test.需要帮助弄清楚如何在主 class 中使用 Freemarker 表达式,以防止在性能测试中更精确地从外部文件读取。

Would be glad to hear all your recommendations.很高兴听到您的所有建议。

ps We are gonna to build a big app, that would be work with a big data. ps 我们要构建一个大应用程序,它可以处理大数据。 And we planning to use Freemarker to handle this data.我们计划使用 Freemarker 来处理这些数据。

It's critical to decide which phases you really want to measure, and with what weight.决定您真正想要测量的阶段以及重量是至关重要的。 Above you are measuring loading the template once, and executing the already parsed template for 100000 times.上面您正在测量加载模板一次,并执行已解析的模板 100000 次。 If you don't want to measure template loading and parsing, then just put the long startTime =... line after the getTemplate line.如果您不想测量模板加载和解析,那么只需将long startTime =...行放在getTemplate行之后。

Some further notes:一些进一步的说明:

  • What the templates write into can matter, sometimes a lot.模板写入的内容可能很重要,有时很重要。 You re writing to the console, but the real application will not.您正在写入控制台,但真正的应用程序不会。 If you just want to rule out that factor, then use freemarker.template.utility.NullWriter.INSTANCE for the output.如果你只是想排除这个因素,那么对 output 使用freemarker.template.utility.NullWriter.INSTANCE

  • You should do some "warm up", so that the JIT can do its work.您应该做一些“热身”,以便 JIT 可以完成它的工作。 So maybe run the whole test for some hundreds of times before starting the actual measurement.因此,在开始实际测量之前,可能会运行整个测试数百次。

  • The data model ( data ) will be wrapped 100000 times as well.数据 model ( data ) 也将被包装 100000 次。 If lot of small templates will read the same data, then it's maybe a good idea to pre-wrap it via TemplateHashModel wrappedData = (TemplateHashModel) config.getObjectWrapper().wrap(data) , and then pass wrappedData to the process calls of different templates later.如果很多小模板会读取相同的数据,那么最好通过TemplateHashModel wrappedData = (TemplateHashModel) config.getObjectWrapper().wrap(data)预先包装它,然后将wrappedData传递给不同的process调用以后的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM