Skip to content

Commit aa8e756

Browse files
authored
feat: add insert benchmark (#3528)
1 parent 9401a5e commit aa8e756

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

benchmark/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
2727
<dependency>
2828
<groupId>com.4paradigm.openmldb</groupId>
2929
<artifactId>openmldb-jdbc</artifactId>
30-
<version>0.7.0</version>
30+
<version>0.8.3</version>
3131
</dependency>
3232
<dependency>
3333
<groupId>com.4paradigm.openmldb</groupId>
3434
<artifactId>openmldb-native</artifactId>
35-
<version>0.7.0-allinone</version>
35+
<version>0.8.3-allinone</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>org.slf4j</groupId>

benchmark/src/main/java/com/_4paradigm/openmldb/benchmark/BenchmarkConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class BenchmarkConfig {
3434
public static long TS_BASE = System.currentTimeMillis();
3535
public static String DEPLOY_NAME;
3636
public static String CSV_PATH;
37+
public static int PUT_BACH_SIZE = 1;
3738

3839
private static SqlExecutor executor = null;
3940
private static SdkOption option = null;
@@ -58,6 +59,7 @@ public class BenchmarkConfig {
5859
// if(!CSV_PATH.startsWith("/")){
5960
// CSV_PATH=Util.getRootPath()+CSV_PATH;
6061
// }
62+
PUT_BACH_SIZE = Integer.valueOf(prop.getProperty("PUT_BACH_SIZE", "1"));
6163
} catch (Exception e) {
6264
e.printStackTrace();
6365
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com._4paradigm.openmldb.benchmark;
2+
3+
import com._4paradigm.openmldb.sdk.SqlExecutor;
4+
import org.openjdk.jmh.annotations.*;
5+
import org.openjdk.jmh.runner.Runner;
6+
import org.openjdk.jmh.runner.options.Options;
7+
import org.openjdk.jmh.runner.options.OptionsBuilder;
8+
9+
import java.sql.Timestamp;
10+
import java.util.Random;
11+
import java.util.concurrent.TimeUnit;
12+
13+
@BenchmarkMode(Mode.SampleTime)
14+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
15+
@State(Scope.Benchmark)
16+
@Threads(10)
17+
@Fork(value = 1, jvmArgs = {"-Xms8G", "-Xmx8G"})
18+
@Warmup(iterations = 2)
19+
@Measurement(iterations = 5, time = 60)
20+
21+
public class OpenMLDBInsertBenchmark {
22+
private SqlExecutor executor;
23+
private String database = "test_put_db";
24+
private String tableName = "test_put_t1";
25+
private int indexNum;
26+
private String placeholderSQL;
27+
private Random random;
28+
int stringNum = 15;
29+
int doubleNum= 5;
30+
int timestampNum = 5;
31+
int bigintNum = 5;
32+
33+
public OpenMLDBInsertBenchmark() {
34+
executor = BenchmarkConfig.GetSqlExecutor(false);
35+
indexNum = BenchmarkConfig.WINDOW_NUM;
36+
random = new Random();
37+
StringBuilder builder = new StringBuilder();
38+
builder.append("insert into ");
39+
builder.append(tableName);
40+
builder.append(" values (");
41+
for (int i = 0; i < stringNum + doubleNum + timestampNum + bigintNum; i++) {
42+
if (i > 0) {
43+
builder.append(", ");
44+
}
45+
builder.append("?");
46+
}
47+
builder.append(");");
48+
placeholderSQL = builder.toString();
49+
}
50+
51+
@Setup
52+
public void initEnv() {
53+
Util.executeSQL("CREATE DATABASE IF NOT EXISTS " + database + ";", executor);
54+
Util.executeSQL("USE " + database + ";", executor);
55+
String ddl = Util.genDDL(tableName, indexNum);
56+
Util.executeSQL(ddl, executor);
57+
}
58+
59+
@Benchmark
60+
public void executePut() {
61+
java.sql.PreparedStatement pstmt = null;
62+
try {
63+
pstmt = executor.getInsertPreparedStmt(database, placeholderSQL);
64+
for (int num = 0; num < BenchmarkConfig.PUT_BACH_SIZE; num++) {
65+
int idx = 1;
66+
for (int i = 0; i < stringNum; i++) {
67+
if (i < indexNum) {
68+
pstmt.setString(idx, String.valueOf(BenchmarkConfig.PK_BASE + random.nextInt(BenchmarkConfig.PK_NUM)));
69+
} else {
70+
pstmt.setString(idx, "v" + String.valueOf(100000 + random.nextInt(100000)));
71+
}
72+
idx++;
73+
}
74+
for (int i = 0; i < doubleNum; i++) {
75+
pstmt.setDouble(idx, random.nextDouble());
76+
idx++;
77+
}
78+
for (int i = 0; i < timestampNum; i++) {
79+
pstmt.setTimestamp(idx, new Timestamp(System.currentTimeMillis()));
80+
idx++;
81+
}
82+
for (int i = 0; i < bigintNum; i++) {
83+
pstmt.setLong(idx, random.nextLong());
84+
idx++;
85+
}
86+
if (BenchmarkConfig.PUT_BACH_SIZE > 1) {
87+
pstmt.addBatch();
88+
}
89+
}
90+
if (BenchmarkConfig.PUT_BACH_SIZE > 1) {
91+
pstmt.executeBatch();
92+
} else {
93+
pstmt.execute();
94+
}
95+
} catch (Exception e) {
96+
e.printStackTrace();
97+
} finally {
98+
if (pstmt != null) {
99+
try {
100+
pstmt.close();
101+
} catch (Exception e) {
102+
e.printStackTrace();
103+
}
104+
}
105+
}
106+
}
107+
108+
@TearDown
109+
public void cleanEnv() {
110+
Util.executeSQL("USE " + database + ";", executor);
111+
Util.executeSQL("DROP TABLE " + tableName + ";", executor);
112+
Util.executeSQL("DROP DATABASE " + database + ";", executor);
113+
}
114+
115+
public static void main(String[] args) {
116+
/* OpenMLDBPutBenchmark benchmark = new OpenMLDBPutBenchmark();
117+
benchmark.initEnv();
118+
benchmark.executePut();
119+
benchmark.cleanEnv();*/
120+
121+
try {
122+
Options opt = new OptionsBuilder()
123+
.include(OpenMLDBInsertBenchmark.class.getSimpleName())
124+
.forks(1)
125+
.build();
126+
new Runner(opt).run();
127+
} catch (Exception e) {
128+
e.printStackTrace();
129+
}
130+
}
131+
}

benchmark/src/main/resources/conf.properties

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
ZK_CLUSTER=172.24.4.55:30008
2-
ZK_PATH=/openmldb
1+
ZK_CLUSTER=172.24.4.55:32200
2+
ZK_PATH=/openmldb_test
33

44
WINDOW_NUM=2
55
WINDOW_SIZE=1000
@@ -12,3 +12,5 @@ PK_BASE=1000000
1212
DATABASE=bank_perf
1313
DEPLOY_NAME=deploy_bank
1414
CSV_PATH=data/bank_flattenRequest.csv
15+
16+
PUT_BACH_SIZE=100

0 commit comments

Comments
 (0)