Skip to content

Instantly share code, notes, and snippets.

@bfritz
Last active February 2, 2017 20:19
Show Gist options
  • Save bfritz/f4294097e0ee77bef466a149a30851df to your computer and use it in GitHub Desktop.
Save bfritz/f4294097e0ee77bef466a149a30851df to your computer and use it in GitHub Desktop.
http4s string vs szb benchmark flamegraph with 16 headers
package org.http4s
package bench
import org.http4s.util.StringWriter
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
class HeaderStringsBench {
@Benchmark
def string(in: HeadersInput2) = {
val w = new StringWriter(512)
w append "HTTP/1.1" append "200 OK" append "\r\n"
in.headers.foreach { h =>
w append h append "\r\n"
}
w append "Connection:close\r\n"
ByteBuffer.wrap(w.result.getBytes(StandardCharsets.ISO_8859_1))
}
@Benchmark
def szb(in: HeadersInput2) = {
val w = new StringBuilder(512)
w append "HTTP/1.1" append "200 OK" append "\r\n"
in.headers.foreach { h =>
w append h append "\r\n"
}
w append "Connection:close\r\n"
ByteBuffer.wrap(w.toString.getBytes(StandardCharsets.ISO_8859_1))
}
}
@State(Scope.Thread)
class HeadersInput2 {
// @Param(Array("2", "4", "8", "16", "32", "64"))
@Param(Array("16"))
var size: Int = _
var headerSeq: Seq[Header] = _
var headers: Headers = _
var replacement: Header = _
@Setup
def setup(): Unit = {
headerSeq = (0 until size) map { i =>
Header(s"X-Headers-Benchmark-$i", i.toString)
}
headers = Headers(headerSeq:_*)
replacement = Header(s"X-Headers-Benchmark-${headers.size / 2}", "replacement")
}
}

Captured 30 seconds of second 40 second iteration for each benchmark with:

JMH_PID=$(ps aux | grep -i java | grep org.openjdk.jmh.runner.ForkedMain | awk '{print $2}')
echo $JMH_PID
sudo perf record -F 99 -p $JMH_PID -g -- sleep 30 ; \
  java -cp /home/brad/projects/perf-map-agent/code/out/attach-main.jar:/usr/lib/jvm/java-8-openjdk/lib/tools.jar net.virtualvoid.perf.AttachOnce $JMH_PID

In SBT:

> ;bench/jmh:run -i 3 -wi 3 -f1 -t1 -r 40 org.http4s.bench.HeaderStringsBench -jvmArgs -XX:+PreserveFramePointer
[info] Running org.openjdk.jmh.Main -i 3 -wi 3 -f1 -t1 -r 40 org.http4s.bench.HeaderStringsBench -jvmArgs -XX:+PreserveFramePointer
[info] # JMH 1.17.4 (released 15 days ago)
[info] # VM version: JDK 1.8.0_121, VM 25.121-b13
[info] # VM invoker: /usr/lib/jvm/java-8-openjdk/jre/bin/java
[info] # VM options: -XX:+PreserveFramePointer
[info] # Warmup: 3 iterations, 1 s each
[info] # Measurement: 3 iterations, 40 s each
[info] # Timeout: 10 min per iteration
[info] # Threads: 1 thread, will synchronize iterations
[info] # Benchmark mode: Average time, time/op
[info] # Benchmark: org.http4s.bench.HeaderStringsBench.string
[info] # Parameters: (size = 16)
[info] 
[info] # Run progress: 0.00% complete, ETA 00:04:06
[info] # Fork: 1 of 1
[info] # Warmup Iteration   1: 0.903 us/op
[info] # Warmup Iteration   2: 0.865 us/op
[info] # Warmup Iteration   3: 0.815 us/op
[info] Iteration   1: 0.818 us/op
[info] Iteration   2: 0.810 us/op
[info] Iteration   3: 0.820 us/op
[info] 
[info] 
[info] Result "org.http4s.bench.HeaderStringsBench.string":
[info]   0.816 ±(99.9%) 0.103 us/op [Average]
[info]   (min, avg, max) = (0.810, 0.816, 0.820), stdev = 0.006
[info]   CI (99.9%): [0.713, 0.919] (assumes normal distribution)
[info] 
[info] 
[info] # JMH 1.17.4 (released 15 days ago)
[info] # VM version: JDK 1.8.0_121, VM 25.121-b13
[info] # VM invoker: /usr/lib/jvm/java-8-openjdk/jre/bin/java
[info] # VM options: -XX:+PreserveFramePointer
[info] # Warmup: 3 iterations, 1 s each
[info] # Measurement: 3 iterations, 40 s each
[info] # Timeout: 10 min per iteration
[info] # Threads: 1 thread, will synchronize iterations
[info] # Benchmark mode: Average time, time/op
[info] # Benchmark: org.http4s.bench.HeaderStringsBench.szb
[info] # Parameters: (size = 16)
[info] 
[info] # Run progress: 50.00% complete, ETA 00:02:03
[info] # Fork: 1 of 1
[info] # Warmup Iteration   1: 2.935 us/op
[info] # Warmup Iteration   2: 2.719 us/op
[info] # Warmup Iteration   3: 2.652 us/op
[info] Iteration   1: 2.526 us/op
[info] Iteration   2: 2.476 us/op
[info] Iteration   3: 2.501 us/op
[info] 
[info] 
[info] Result "org.http4s.bench.HeaderStringsBench.szb":
[info]   2.501 ±(99.9%) 0.450 us/op [Average]
[info]   (min, avg, max) = (2.476, 2.501, 2.526), stdev = 0.025
[info]   CI (99.9%): [2.051, 2.951] (assumes normal distribution)
[info] 
[info] 
[info] # Run complete. Total time: 00:04:07
[info] 
[info] Benchmark                  (size)  Mode  Cnt  Score   Error  Units
[info] HeaderStringsBench.string      16  avgt    3  0.816 ± 0.103  us/op
[info] HeaderStringsBench.szb         16  avgt    3  2.501 ± 0.450  us/op
[success] Total time: 248 s, completed Feb 2, 2017 3:06:06 PM
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment