postgresql-jdbc.git / summary / log / commit / refs
commit 613a641982ff1640d8fb9eae23200f06dea41ff9
Author: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
Date: Wed Oct 07 11:30:49 2015 +0000
chore: add more benchmarks, add missing FlightRecorderProfiler
Add "bind boolean" and "padding zeroes" benchmarks
Closes #385
.../profilers/FlightRecorderProfiler.java | 77 ++++++++++++++++++++++
.../benchmark/statement/BindBoolean.java | 75 +++++++++++++++++++++
.../postgresql/benchmark/time/AddPaddingZeros.java | 74 +++++++++++++++++++++
3 files changed, 226 insertions(+)
diff --git a/ubenchmark/src/main/java/org/postgresql/benchmark/profilers/FlightRecorderProfiler.java b/ubenchmark/src/main/java/org/postgresql/benchmark/profilers/FlightRecorderProfiler.java
new file mode 100644
index 00000000..3cb9ee44
--- /dev/null
+++ b/ubenchmark/src/main/java/org/postgresql/benchmark/profilers/FlightRecorderProfiler.java
@@ -0,0 +1,77 @@
+/*-------------------------------------------------------------------------
+*
+* Copyright (c) 2003-2015, PostgreSQL Global Development Group
+*
+*
+*-------------------------------------------------------------------------
+*/
+package org.postgresql.benchmark.profilers;
+
+import org.openjdk.jmh.infra.BenchmarkParams;
+import org.openjdk.jmh.infra.IterationParams;
+import org.openjdk.jmh.profile.ExternalProfiler;
+import org.openjdk.jmh.results.BenchmarkResult;
+import org.openjdk.jmh.results.Result;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Captures Flight Recorder log.
+ * Note: Flight Recorder is available in OracleJDK only.
+ * Usage of Flight Recorder in production requires a LICENSE FEE, however Flight Recorder is free for use in test systems.
+ * It is assumed you would not use pgjdbc benchmarks for running a production system, thus it is believed to be safe.
+ */
+public class FlightRecorderProfiler implements ExternalProfiler {
+ @Override
+ public Collection<String> addJVMInvokeOptions(BenchmarkParams params) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public Collection<String> addJVMOptions(BenchmarkParams params) {
+ StringBuilder sb = new StringBuilder();
+ for (String param : params.getParamsKeys()) {
+ if (sb.length() != 0) sb.append('-');
+ sb.append(param).append('-').append(params.getParam(param));
+ }
+
+ long duration = getDurationSeconds(params.getWarmup()) + getDurationSeconds(params.getMeasurement());
+ return Arrays.asList(
+ "-XX:+UnlockCommercialFeatures", "-XX:+FlightRecorder",
+ "-XX:StartFlightRecording=settings=profile,duration=" + duration + "s,filename="
+ + params.getBenchmark() + "_" + sb + ".jfr");
+ }
+
+ private long getDurationSeconds(IterationParams warmup) {
+ return warmup.getTime().convertTo(TimeUnit.SECONDS) * warmup.getCount();
+ }
+
+ @Override
+ public void beforeTrial(BenchmarkParams benchmarkParams) {
+
+ }
+
+ @Override
+ public Collection<? extends Result> afterTrial(BenchmarkResult br, long pid, File stdOut, File stdErr) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public boolean allowPrintOut() {
+ return true;
+ }
+
+ @Override
+ public boolean allowPrintErr() {
+ return true;
+ }
+
+ @Override
+ public String getDescription() {
+ return "Collects Java Flight Recorder profile";
+ }
+}
diff --git a/ubenchmark/src/main/java/org/postgresql/benchmark/statement/BindBoolean.java b/ubenchmark/src/main/java/org/postgresql/benchmark/statement/BindBoolean.java
new file mode 100644
index 00000000..2d41a179
--- /dev/null
+++ b/ubenchmark/src/main/java/org/postgresql/benchmark/statement/BindBoolean.java
@@ -0,0 +1,75 @@
+/*-------------------------------------------------------------------------
+*
+* Copyright (c) 2003-2015, PostgreSQL Global Development Group
+*
+*
+*-------------------------------------------------------------------------
+*/
+package org.postgresql.benchmark.statement;
+
+import org.openjdk.jmh.annotations.*;
+import org.openjdk.jmh.profile.GCProfiler;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+import org.postgresql.benchmark.profilers.FlightRecorderProfiler;
+import org.postgresql.util.ConnectionUtil;
+
+import java.sql.*;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+@Fork(value = 0, jvmArgsPrepend = "-Xmx128m")
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@State(Scope.Thread)
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+public class BindBoolean {
+ private Connection connection;
+ private PreparedStatement ps;
+
+ @Setup(Level.Trial)
+ public void setUp() throws SQLException {
+ Properties props = ConnectionUtil.getProperties();
+
+ connection = DriverManager.getConnection(ConnectionUtil.getURL(), props);
+ ps = connection.prepareStatement("select ?");
+ }
+
+ @TearDown(Level.Trial)
+ public void tearDown() throws SQLException {
+ ps.close();
+ connection.close();
+ }
+
+ @Benchmark
+ public Statement boolAsInt() throws SQLException {
+ ps.setObject(1, 1, Types.BOOLEAN);
+ return ps;
+ }
+
+ @Benchmark
+ public Statement boolAsBoolean() throws SQLException {
+ ps.setObject(1, true, Types.BOOLEAN);
+ return ps;
+ }
+
+ @Benchmark
+ public Statement bindBoolean() throws SQLException {
+ ps.setBoolean(1, true);
+ return ps;
+ }
+
+ public static void main(String[] args) throws RunnerException {
+ Options opt = new OptionsBuilder()
+ .include(BindBoolean.class.getSimpleName())
+ .addProfiler(GCProfiler.class)
+ .addProfiler(FlightRecorderProfiler.class)
+ .detectJvmArgs()
+ .build();
+
+ new Runner(opt).run();
+ }
+}
diff --git a/ubenchmark/src/main/java/org/postgresql/benchmark/time/AddPaddingZeros.java b/ubenchmark/src/main/java/org/postgresql/benchmark/time/AddPaddingZeros.java
new file mode 100644
index 00000000..48acfa5c
--- /dev/null
+++ b/ubenchmark/src/main/java/org/postgresql/benchmark/time/AddPaddingZeros.java
@@ -0,0 +1,74 @@
+/*-------------------------------------------------------------------------
+*
+* Copyright (c) 2003-2015, PostgreSQL Global Development Group
+*
+*
+*-------------------------------------------------------------------------
+*/
+package org.postgresql.benchmark.time;
+
+import org.openjdk.jmh.annotations.*;
+import org.openjdk.jmh.profile.GCProfiler;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+import java.sql.Timestamp;
+import java.util.concurrent.TimeUnit;
+
+@Fork(value = 0, jvmArgsPrepend = "-Xmx128m")
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@State(Scope.Thread)
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+public class AddPaddingZeros {
+
+ @Param({"1000", "1000000", "100000000"})
+ int nanos;
+
+ static final char[] ZEROS = "0000000000".toCharArray();
+
+ Timestamp ts = new Timestamp(System.currentTimeMillis());
+
+ StringBuffer sb = new StringBuffer();
+
+ @Setup
+ public void init() {
+ ts.setNanos(nanos);
+ }
+
+ @Benchmark
+ public void charArray() {
+ sb.setLength(0);
+ int nanos = ts.getNanos();
+ char[] decimalStr = {'0', '0', '0', '0', '0', '0', '0', '0', '0'};
+ char[] nanoStr = Integer.toString(nanos).toCharArray();
+ System.arraycopy(nanoStr, 0, decimalStr, decimalStr.length - nanoStr.length, nanoStr.length);
+ sb.append(decimalStr, 0, 6);
+ }
+
+ @Benchmark
+ public void insert() {
+ sb.setLength(0);
+ int len = sb.length();
+ int nanos = ts.getNanos();
+ sb.append(nanos / 1000);
+ int needZeros = 6 - (sb.length() - len);
+ if (needZeros > 0) {
+ sb.insert(len, ZEROS, 0, needZeros);
+ }
+ }
+
+ public static void main(String[] args) throws RunnerException {
+ Options opt = new OptionsBuilder()
+ .include(AddPaddingZeros.class.getSimpleName())
+ .addProfiler(GCProfiler.class)
+// .addProfiler(FlightRecorderProfiler.class)
+ .detectJvmArgs()
+ .build();
+
+ new Runner(opt).run();
+ }
+}
[parent: 0d1237cc10f8]