[
https://issues.apache.org/jira/browse/VELOCITY-934?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17232388#comment-17232388
]
Andreas Hager commented on VELOCITY-934:
----------------------------------------
Yes the template is used concurrently. It's a performance benchmark and I think
the author wanted Velocity render as fast as possible.
This is the code the benchmark uses:
{code:java}
@Setup
public void setup() {
Properties configuration = new Properties();
configuration.setProperty("resource.loader", "class");
configuration.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
VelocityEngine engine = new VelocityEngine(configuration);
context = new VelocityContext(getContext());
template = engine.getTemplate("templates/stocks.velocity.html",
"UTF-8");
}
@Benchmark
public String benchmark() {
Writer writer = new StringWriter();
template.merge(context, writer);
return writer.toString();
}
{code}
So Template and Context are not thread safe? Seems like a strange design
decision, since the engine provides those to the caller. Anyways, I could
probably change the code to something like this:
{code:java}
@Benchmark
public String benchmark() {
context = new VelocityContext(getContext());
template = engine.getTemplate("templates/stocks.velocity.html",
"UTF-8");
Writer writer = new StringWriter();
template.merge(context, writer);
return writer.toString();
}
{code}
But I think this would let velocity perform poorly in the benchmark. Is there
another thread safe way to do this, while still rendering quickly?
> NullPointerException under high concurrency
> -------------------------------------------
>
> Key: VELOCITY-934
> URL: https://issues.apache.org/jira/browse/VELOCITY-934
> Project: Velocity
> Issue Type: Bug
> Components: Engine
> Affects Versions: 1.7.x, 2.2
> Environment: Windows 10 Professional
> Reporter: Andreas Hager
> Priority: Major
>
> I adjusted the mboseke/template-benchmark to profile throughput on AMD Ryzen
> 5950x:
> [https://github.com/casid/template-benchmark/tree/ryzen-5950x]
> With 32 concurrent threads, I can reliably reproduce a runtime exception in
> the Apache Velocity benchmark:
> {code:java}
> java.lang.NullPointerException
> at
> org.apache.velocity.runtime.directive.Directive.postRender(Directive.java:240)
> at
> org.apache.velocity.runtime.directive.Foreach.clean(Foreach.java:325)
> at
> org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:292)
> at
> org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:301)
> at
> org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:423)
> at org.apache.velocity.Template.merge(Template.java:358)
> at org.apache.velocity.Template.merge(Template.java:262)
> at com.mitchellbosecke.benchmark.Velocity.benchmark(Velocity.java:34)
> at
> com.mitchellbosecke.benchmark.generated.Velocity_benchmark_jmhTest.benchmark_thrpt_jmhStub(Velocity_benchmark_jmhTest.java:122)
> at
> com.mitchellbosecke.benchmark.generated.Velocity_benchmark_jmhTest.benchmark_Throughput(Velocity_benchmark_jmhTest.java:69)
> at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown
> Source)
> at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.base/java.lang.reflect.Method.invoke(Method.java:566)
> at
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:430)
> at
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:412)
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
> at
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
> at
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
> at java.base/java.lang.Thread.run(Thread.java:834)
> {code}
> This initially happened with version 1.7, but after updating to 2.2 the
> problem is still there. It looks like there is some threading issue under
> high concurrency.
> This is the velocity benchmark class:
> [https://github.com/casid/template-benchmark/blob/ryzen-5950x/src/main/java/com/mitchellbosecke/benchmark/Velocity.java]
> And this is the velocity template:
> [https://github.com/casid/template-benchmark/blob/ryzen-5950x/src/main/resources/templates/stocks.velocity.html]
>
>
>
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]