|
| 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 | +} |
0 commit comments