com.clickhouse.benchmark.jdbc.DriverBenchmark Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clickhouse-benchmark Show documentation
Show all versions of clickhouse-benchmark Show documentation
Benchmarks for ClickHouse clients
The newest version!
package com.clickhouse.benchmark.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
/**
* Base class for JDBC driver benchmarking.
*/
@State(Scope.Benchmark)
@Warmup(iterations = 10, timeUnit = TimeUnit.SECONDS, time = 1)
@Measurement(iterations = 10, timeUnit = TimeUnit.SECONDS, time = 1)
@Fork(value = 2)
@Threads(value = -1)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public abstract class DriverBenchmark {
// batch size for mutation
private final int BATCH_SIZE = Integer.parseInt(System.getProperty("batchSize", "5000"));
// fetch size for query
private final int FETCH_SIZE = Integer.parseInt(System.getProperty("fetchSize", "1000"));
// insert mode: 1) values; 2) table; 3) input
private final String INSERT_MODE = System.getProperty("insertMode", "values").toLowerCase();
protected PreparedStatement setParameters(PreparedStatement s, Object... values)
throws SQLException {
if (values != null && values.length > 0) {
int index = 1;
for (Object v : values) {
s.setObject(index++, v);
}
}
return s;
}
protected String replaceParameters(String sql, Object... values) {
if (values != null && values.length > 0) {
for (Object v : values) {
int index = sql.indexOf('?');
if (index == -1) {
break;
}
String expr = null;
if (v instanceof Number) {
expr = String.valueOf(v);
} else {
expr = "'" + v + "'"; // without escaping...
}
sql = sql.substring(0, index) + expr + sql.substring(index + 1);
}
}
return sql;
}
private int processBatch(Statement s, String sql, SupplyValueFunction func, Enumeration