Skip to content

Instantly share code, notes, and snippets.

@yusuke2255
Created May 9, 2014 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusuke2255/d741d13819d3891f20c0 to your computer and use it in GitHub Desktop.
Save yusuke2255/d741d13819d3891f20c0 to your computer and use it in GitHub Desktop.
FreeMarkerTest
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
<a href="${redirectUrl}">
<img src="${creativeImageUrl}" width="${creativeWidth}" height="${creativeHeight}"/>
<img src="${impressionUrl}" style="display: none;"/>
</a>
</body>
</html>
package jp.test.runner;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import jp.test.common.TemplateEngineTestUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkerRunner implements Runnable {
private static Configuration cfg = null;
private static final Random rdm = new Random();
private String templateName = null;
public FreeMarkerRunner(String templatePath) {
try {
if (cfg == null) {
URL url = ClassLoader.getSystemResource(templatePath);
File file = new File(url.toURI());
File dir = file.getParentFile();
this.templateName = file.getName();
System.out.println("Create Configration : " + dir.getAbsolutePath());
cfg = new Configuration();
try {
cfg.setDirectoryForTemplateLoading(dir);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void run() {
Map<String, Object> data = new HashMap<>();
data.put("redirectUrl", TemplateEngineTestUtil.getValue(1));
data.put("impressionUrl", TemplateEngineTestUtil.getValue(2));
data.put("creativeImageUrl", TemplateEngineTestUtil.getValue(3));
data.put("creativeWidth", rdm.nextInt());
data.put("creativeHeight", rdm.nextInt());
try {
Writer out = new StringWriter();
Template template = cfg.getTemplate(templateName);
template.process(data, out);
String html = out.toString();
// System.out.println(html);
} catch (TemplateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package jp.test.runner;
import java.util.Random;
import jp.test.common.TemplateEngineTestUtil;
public class StringFormatRunner implements Runnable {
private static final String TEMPLATE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><head></head><body><a href=\"%s\" target=\"_top\"><img src=\"%s\" width=\"%d\" height=\"%d\" /><img src=\"%s\" style=\"display: none; \" /></a></body></html>";
private static final Random rdm = new Random();
@Override
public void run() {
String html = String.format(TEMPLATE, TemplateEngineTestUtil.getValue(1), TemplateEngineTestUtil.getValue(3), rdm.nextInt(), rdm.nextInt(), TemplateEngineTestUtil.getValue(2));
//System.out.println(html);
}
}
package jp.test;
import jp.test.runner.FreeMarkerRunner;
import jp.test.runner.StringFormatRunner;
public class TemplateEngineTest {
public static void main(String[] args) {
//Runnable runner = new FreeMarkerRunner("jp/test/runner/templates/default.ftl");
Runnable runner = new StringFormatRunner();
long start = System.nanoTime();
for (int i = 0 ; i < 1000000 ; i ++) {
runner.run();
}
long end = System.nanoTime();
System.out.println(end - start);
}
}
package jp.test.common;
import java.util.UUID;
public class TemplateEngineTestUtil {
public static String getValue(int type) {
String uuid = UUID.randomUUID().toString();
String result = null;
switch (type) {
case 1:
result = "http://redirect.jp/" + uuid;
break;
case 2:
result = "http://impression.jp/" + uuid;
break;
case 3:
result = "http://creativeImageUrl.jp/" + uuid;
break;
default:
break;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment