Java で HTML Escape とかのベンチマーク

なんとなくとってみた。結局 Caliper でとった。guava が意外と速いんだなあ。

package me.geso.javaMicroBenchmarks;

import java.io.IOException;

import org.apache.commons.lang3.StringEscapeUtils;

import com.google.caliper.runner.CaliperMain;
import com.google.common.html.HtmlEscapers;

import org.springframework.web.util.HtmlUtils;

public class HTMLEscapeBenchmark {
	private static final String src = "<><><><>&&&&;;;;jl2kjlnnfljflksdjfuowu-9urjnl321knl;fu3poifuokbkvnl;uigufjslfjadsipuru1o2krn;lkmfzkjhvojopijkJ:LJKU)!*)($J!KLJOIFHS)JPJ";

	public void timeGuava(int reps) {
		for (int i = 0; i < reps; i++) {
			HtmlEscapers.htmlEscaper().escape(src);
		}
	}

	public void timeApacheCommons(int reps) {
		for (int i = 0; i < reps; i++) {
			StringEscapeUtils.escapeHtml4(src);
		}
	}

	public void timeReplace(int reps) {
		for (int i = 0; i < reps; i++) {
		    src.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
		            .replace("'", "&apos;").replaceAll("\"", "&quot;");
		}
	}

	public void timeSpring(int reps) {
		for (int i = 0; i < reps; i++) {
			HtmlUtils.htmlEscape(src);
		}
	}

	public static void main(String[] args) throws IOException {
		CaliperMain.main(HTMLEscapeBenchmark.class, args);
	}

}
Experiment selection: 
  Instruments:   [allocation, runtime]
  User parameters:   {}
  Virtual machines:  [default]
  Selection type:    Full cartesian product

This selection yields 8 experiments.
Starting trial 1 of 8: {instrument=allocation, benchmarkMethod=timeApacheCommons, vm=default, parameters={}}… Complete!
  bytes(B): min=24.00, 1st qu.=24.00, median=24.00, mean=24.00, 3rd qu.=24.00, max=24.00
  objects: min=1.00, 1st qu.=1.00, median=1.00, mean=1.00, 3rd qu.=1.00, max=1.00
Starting trial 2 of 8: {instrument=allocation, benchmarkMethod=timeGuava, vm=default, parameters={}}… Complete!
  bytes(B): min=24.00, 1st qu.=24.00, median=24.00, mean=24.00, 3rd qu.=24.00, max=24.00
  objects: min=1.00, 1st qu.=1.00, median=1.00, mean=1.00, 3rd qu.=1.00, max=1.00
Starting trial 3 of 8: {instrument=allocation, benchmarkMethod=timeReplace, vm=default, parameters={}}… Complete!
  bytes(B): min=0.00, 1st qu.=0.00, median=0.00, mean=0.00, 3rd qu.=0.00, max=0.00
  objects: min=0.00, 1st qu.=0.00, median=0.00, mean=0.00, 3rd qu.=0.00, max=0.00
Starting trial 4 of 8: {instrument=allocation, benchmarkMethod=timeSpring, vm=default, parameters={}}… Complete!
  bytes(B): min=24.00, 1st qu.=24.00, median=24.00, mean=24.00, 3rd qu.=24.00, max=24.00
  objects: min=1.00, 1st qu.=1.00, median=1.00, mean=1.00, 3rd qu.=1.00, max=1.00
Starting trial 5 of 8: {instrument=runtime, benchmarkMethod=timeApacheCommons, vm=default, parameters={}}… INFO: This experiment does not require a microbenchmark. The granularity of the timer (70ns) is less than 0.1% of the measured runtime. If all experiments for this benchmark have runtimes greater than 70μs, consider the macrobenchmark instrument.
Complete!
  runtime(ns): min=15944.51, 1st qu.=16172.86, median=16588.74, mean=17012.46, 3rd qu.=17048.50, max=21121.09
Starting trial 6 of 8: {instrument=runtime, benchmarkMethod=timeGuava, vm=default, parameters={}}… Complete!
  runtime(ns): min=524.60, 1st qu.=528.43, median=545.21, mean=544.92, 3rd qu.=562.21, max=564.73
Starting trial 7 of 8: {instrument=runtime, benchmarkMethod=timeReplace, vm=default, parameters={}}… INFO: This experiment does not require a microbenchmark. The granularity of the timer (70ns) is less than 0.1% of the measured runtime. If all experiments for this benchmark have runtimes greater than 70μs, consider the macrobenchmark instrument.
Complete!
  runtime(ns): min=8872.24, 1st qu.=8957.33, median=9274.61, mean=9592.84, 3rd qu.=10445.22, max=10987.49
Starting trial 8 of 8: {instrument=runtime, benchmarkMethod=timeSpring, vm=default, parameters={}}… Complete!
  runtime(ns): min=1042.80, 1st qu.=1056.34, median=1080.96, mean=1082.21, 3rd qu.=1091.70, max=1168.09

Execution complete: 1.710m.
Collected 108 measurements from:
  2 instrument(s)
  2 virtual machine(s)
  4 benchmark(s)
Results have been uploaded. View them at: https://microbenchmarks.appspot.com/runs/28c56794-ec42-45b8-8c58-6f0a617fe4d7

Published: 2014-11-28(Fri) 08:09