Author: byron
Date: Fri Jan 2 17:31:26 2009
New Revision: 730886
URL: http://svn.apache.org/viewvc?rev=730886&view=rev
Log:
Added a simple benchmark tool for performance testing
Added:
velocity/engine/trunk/experimental/benchmark/ (with props)
velocity/engine/trunk/experimental/benchmark/Benchmark.java
velocity/engine/trunk/experimental/benchmark/main.vm
velocity/engine/trunk/experimental/benchmark/parse1.vm
velocity/engine/trunk/experimental/benchmark/run.sh (with props)
velocity/engine/trunk/experimental/benchmark/vmlib1.vm
velocity/engine/trunk/experimental/benchmark/vmlib2.vm
Propchange: velocity/engine/trunk/experimental/benchmark/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Jan 2 17:31:26 2009
@@ -0,0 +1 @@
+*.class
Added: velocity/engine/trunk/experimental/benchmark/Benchmark.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/experimental/benchmark/Benchmark.java?rev=730886&view=auto
==============================================================================
--- velocity/engine/trunk/experimental/benchmark/Benchmark.java (added)
+++ velocity/engine/trunk/experimental/benchmark/Benchmark.java Fri Jan 2
17:31:26 2009
@@ -0,0 +1,151 @@
+//package org.apache.velocity.benchmark;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+
+
+public class Benchmark
+{
+ int threadCnt = 25;
+ int runCnt = 1000;
+
+ public static final void main(String[] argv) throws Exception
+ {
+ Benchmark benchmark = new Benchmark();
+ benchmark.go();
+ }
+
+ public void log(String str)
+ {
+ System.out.println(str);
+ }
+
+ public void go() throws Exception
+ {
+
+ Properties props = new Properties();
+ props.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");
+ props.setProperty(RuntimeConstants.VM_LIBRARY, "vmlib1.vm,vmlib2.vm");
+ props.setProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE,
"0");
+ //props.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, "true");
+
+ VelocityEngine vengine = new VelocityEngine();
+ vengine.init(props);
+
+ log("Starting " + threadCnt + " threads which will run " + runCnt + "
times");
+ ArrayList list = new ArrayList(threadCnt);
+ for (int i = 0; i < threadCnt; i++)
+ {
+ VelocityThread vt = new VelocityThread(vengine, runCnt);
+ list.add(vt);
+ vt.start();
+ }
+ }
+}
+
+
+/**
+ * Worker thread which calls the template.
+ */
+class VelocityThread extends Thread
+{
+ VelocityEngine vengine = null;
+ int runCnt = 1000;
+ boolean runError = false;
+
+ // Stuff for the context
+ List innerList = null;
+ List outerList = null;
+
+ public void initContextObjs()
+ {
+ outerList = new ArrayList();
+ innerList = new ArrayList();
+ for (int i=0; i < 10; i++)
+ {
+ outerList.add(new Integer(i));
+ innerList.add(new Integer(i));
+ }
+ }
+
+
+ public VelocityThread(VelocityEngine vengine, int runCnt)
+ {
+ this.vengine = vengine;
+ this.runCnt = runCnt;
+ initContextObjs();
+ }
+
+ public void run()
+ {
+ for (int i = 0; i < runCnt; i++)
+ {
+ StringWriter writer = new StringWriter(10000);
+
+ // We do the setup inside the loop so we can be a little realistic
+ // Since this type of setup would be done in a real application,
+ // And filling the context is appropriate for Velocity performance.
+ VelocityContext context = new VelocityContext();
+ context.put("blue", "green");
+ context.put("innerList", innerList);
+ context.put("outerList", outerList);
+
+ for (int j=0; j<10; j++)
+ {
+ Test test = new Test();
+ test.name = "test" + j;
+ context.put(test.name, test);
+ }
+
+ // Render template
+ try
+ {
+ vengine.mergeTemplate("main.vm", "utf-8", context, writer);
+ }
+ catch(Exception e)
+ {
+ System.out.println(e);
+ runError = true;
+ System.out.println("Errors during run");
+ System.exit(1);
+ }
+ }
+ }
+
+ /**
+ * Test Object to be referenced from the template
+ */
+ public static class Test
+ {
+ String name = null;
+ public String getName()
+ {
+ return name;
+ }
+ }
+}
\ No newline at end of file
Added: velocity/engine/trunk/experimental/benchmark/main.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/experimental/benchmark/main.vm?rev=730886&view=auto
==============================================================================
--- velocity/engine/trunk/experimental/benchmark/main.vm (added)
+++ velocity/engine/trunk/experimental/benchmark/main.vm Fri Jan 2 17:31:26
2009
@@ -0,0 +1,59 @@
+
+These are some lines of text 1
+These are some lines of text 1
+These are some lines of text 1
+These are some lines of text 1
+These are some lines of text 1
+These are some lines of text 1
+
+#foreach($i in $outerList) ## iterate 10 times
+
+ #set($tests = [$test1, $test2, $test3, $test4, $test5])
+ #foreach($test in $tests)
+ #parse("parse1.vm")
+ #end
+
+
+ #foreach($j in $innerList) ## iterate 10 times
+
+ #vm_macro1($test4 $test5)
+ #vm_macro2($test8 $test9)
+
+ #if($i < 5)
+ This is some lines of text
+ This is some lines of text
+ This is some lines of text
+ This is some lines of text
+ This is some lines of text
+ #end
+
+ #set($v1 = "some text 1")
+ #set($v2 = "some text 1")
+ #set($v3 = "some text 1")
+ #set($v4 = "some text 1")
+ #set($v5 = "some text 1")
+ #set($v6 = $test1.name)
+ #set($v7 = $test2.name)
+ #set($v8 = $test3.name)
+ #set($v9 = $test4.name)
+ #set($v10 = $test5.name)
+
+
+ #if ($i > 5)
+ This is some lines of text, blaa blaa
+ This is some lines of text, blaa blaa
+ This is some lines of text, blaa blaa
+ This is some lines of text, blaa blaa
+ This is some lines of text, blaa blaa
+ This is some lines of text, blaa blaa
+ #end
+
+ more lines of text $test4.name
+ more lines of text $test5.name
+ more lines of text $test6.name
+ more lines of text $test7.name
+ more lines of text $test8.name
+
+ #end
+
+#end
Added: velocity/engine/trunk/experimental/benchmark/parse1.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/experimental/benchmark/parse1.vm?rev=730886&view=auto
==============================================================================
--- velocity/engine/trunk/experimental/benchmark/parse1.vm (added)
+++ velocity/engine/trunk/experimental/benchmark/parse1.vm Fri Jan 2 17:31:26
2009
@@ -0,0 +1,4 @@
+
+
+
+#vm_macro1($test1 $test2)
Added: velocity/engine/trunk/experimental/benchmark/run.sh
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/experimental/benchmark/run.sh?rev=730886&view=auto
==============================================================================
--- velocity/engine/trunk/experimental/benchmark/run.sh (added)
+++ velocity/engine/trunk/experimental/benchmark/run.sh Fri Jan 2 17:31:26 2009
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+# Front end for running the benchmark. This both compiles and runs the
Benchmark class.
+# Before running make sure you have run ant so that it creates the bin
directory to
+# access the necessary libraries.
+
+# If you place a velocity.jar file in this directory, then it will benchmark
against
+# that jar, otherwise it will use the compiled classes in the bin/classes
directory.
+
+# This script assumes that javac and java are available at the command line
+
+ROOT=../..
+LIBDIR=$ROOT/bin/lib
+
+[[ ! -d $LIBDIR ]] && echo Oops, directory $LIBDIR does not exit, make sure
you run ant && exit 1
+
+COMMON_COLL=$LIBDIR/commons-collections-3.2.1.jar
+[[ ! -f $COMMON_COLL ]] && echo Oops, $COMMON_COLL does not exist && exit 1
+
+COMMON_LANG=$LIBDIR/commons-lang-2.4.jar
+[[ ! -f $COMMON_LANG ]] && echo Oops, $COMMON_LANG does not exit && exit 1
+
+VELOCITY_PATH=velocity.jar
+if [[ ! -f $VELOCITY_PATH ]]; then
+ VELOCITY_PATH=$ROOT/bin/classes
+ echo velocity.jar was not found in this directory, so we are using the
classes in:
+ echo $VELOCITY_PATH
+else
+ echo Found $VELOCITY_PATH in the current directory, so we are going to use
it!
+fi
+
+CP=$COMMON_LANG:$COMMON_COLL:$VELOCITY_PATH:.
+
+javac -cp $CP Benchmark.java
+time java -server -Xmx50M -cp $CP Benchmark
Propchange: velocity/engine/trunk/experimental/benchmark/run.sh
------------------------------------------------------------------------------
svn:executable = *
Added: velocity/engine/trunk/experimental/benchmark/vmlib1.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/experimental/benchmark/vmlib1.vm?rev=730886&view=auto
==============================================================================
--- velocity/engine/trunk/experimental/benchmark/vmlib1.vm (added)
+++ velocity/engine/trunk/experimental/benchmark/vmlib1.vm Fri Jan 2 17:31:26
2009
@@ -0,0 +1,8 @@
+
+#macro(vm_macro1 $x $y)
+ Some macro2 lines of text $x.name $y.name
+ #if ($x.name == "bogus") xyz #end
+ #if ($x.name == "test4")
+ This is a line of text $y.name
+ #end
+#end
Added: velocity/engine/trunk/experimental/benchmark/vmlib2.vm
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/experimental/benchmark/vmlib2.vm?rev=730886&view=auto
==============================================================================
--- velocity/engine/trunk/experimental/benchmark/vmlib2.vm (added)
+++ velocity/engine/trunk/experimental/benchmark/vmlib2.vm Fri Jan 2 17:31:26
2009
@@ -0,0 +1,10 @@
+
+
+#macro(vm_inner2 $a $b)
+ <td>$a</td><td>#if($b)$b#end<td>
+#end
+
+#macro(vm_macro2 $a $b)
+ Some lines of $a.name text $b.name asdfsad asdsdf fsdff
+ #vm_inner2($a.name $b.name)
+#end