|
| 1 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 2 | +import com.datastax.oss.driver.api.core.config.DefaultDriverOption; |
| 3 | +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; |
| 4 | +import com.datastax.oss.driver.api.core.cql.PreparedStatement; |
| 5 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 6 | +import com.datastax.oss.driver.api.core.cql.Row; |
| 7 | + |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.*; |
| 12 | +import java.util.concurrent.atomic.AtomicLong; |
| 13 | + |
| 14 | +public class MainClass { |
| 15 | + private static Config config; |
| 16 | + |
| 17 | + private static ExecutorService executor; |
| 18 | + private static final String INSERT_STRING = "INSERT INTO benchks.benchtab (pk, v1, v2) VALUES(?, ?, ?)"; |
| 19 | + private static final String SELECT_STRING = "SELECT v1, v2 FROM benchks.benchtab WHERE pk = ?"; |
| 20 | + |
| 21 | + private static long benchmarkStart; |
| 22 | + |
| 23 | + private static long benchmarkEnd; |
| 24 | + |
| 25 | + public static void main(String[] args) throws InterruptedException, ExecutionException { |
| 26 | + |
| 27 | + config = new Config(args); |
| 28 | + System.out.println("Parsed config: "); |
| 29 | + System.out.println(config.toString()); |
| 30 | + |
| 31 | + DriverConfigLoader loader = |
| 32 | + DriverConfigLoader.programmaticBuilder() |
| 33 | + .withString(DefaultDriverOption.PROTOCOL_VERSION, "V4") |
| 34 | + .withLong(DefaultDriverOption.REQUEST_THROTTLER_MAX_QUEUE_SIZE, Math.max(2048, 2L * config.concurrency)) |
| 35 | + .build(); |
| 36 | + |
| 37 | + CqlSession session = CqlSession |
| 38 | + .builder() |
| 39 | + .withConfigLoader(loader) |
| 40 | + .addContactPoints(parseAddresses(config.node_addresses)) |
| 41 | + .withLocalDatacenter("datacenter1") |
| 42 | + .build(); |
| 43 | + |
| 44 | + prepareKeyspaceAndTable(session); |
| 45 | + |
| 46 | + if (!config.dont_prepare) { |
| 47 | + prepareKeyspaceAndTable(session); |
| 48 | + |
| 49 | + if (config.workload.equals(Config.Workload.Selects)) { |
| 50 | + prepareSelectsBenchmark(session); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + AtomicLong nextBatchStart = new AtomicLong(0); |
| 55 | + |
| 56 | + executor = Executors.newFixedThreadPool((int) config.concurrency); |
| 57 | + |
| 58 | + System.out.println("Starting the benchmark"); |
| 59 | + |
| 60 | + benchmarkStart = System.nanoTime(); |
| 61 | + ArrayList<Future<?>> arr = new ArrayList<>(); |
| 62 | + for (int i = 0; i < config.concurrency; i++) { |
| 63 | + arr.add( |
| 64 | + executor.submit(() -> { |
| 65 | + PreparedStatement insertQ = session.prepare(INSERT_STRING); |
| 66 | + PreparedStatement selectQ = session.prepare(SELECT_STRING); |
| 67 | + while (true) { |
| 68 | + |
| 69 | + long curBatchStart = nextBatchStart.addAndGet(config.batch_size); |
| 70 | + if (curBatchStart >= config.tasks) { |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + long curBatchEnd = Math.min(curBatchStart + config.batch_size, config.tasks); |
| 75 | + |
| 76 | + for (long pk = curBatchStart; pk < curBatchEnd; pk++) { |
| 77 | + if (config.workload.equals(Config.Workload.Inserts) || config.workload.equals(Config.Workload.Mixed)) { |
| 78 | + session.execute(insertQ.bind(pk, 2L * pk, 3L * pk)); |
| 79 | + } |
| 80 | + |
| 81 | + if (config.workload.equals(Config.Workload.Selects) || config.workload.equals(Config.Workload.Mixed)) { |
| 82 | + ResultSet rs = session.execute(selectQ.bind(pk)); |
| 83 | + Row r = rs.one(); |
| 84 | + assert r != null; |
| 85 | + if ((r.getLong("v1") != 2 * pk) || (r.getLong("v2") != 3 * pk)) { |
| 86 | + throw new RuntimeException(String.format("Received incorrect data. " + "Expected: (%s, %s, %s). " + "Received: (%s, %s ,%s).", pk, 2 * pk, 3 * pk, r.getInt("pk"), r.getInt("v1"), r.getInt("v2"))); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + })); |
| 92 | + } |
| 93 | + |
| 94 | + executor.shutdown(); |
| 95 | + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
| 96 | + benchmarkEnd = System.nanoTime(); |
| 97 | + for (Future<?> f : arr) { |
| 98 | + f.get(); // make sure nothing has thrown |
| 99 | + } |
| 100 | + System.out.println(String.format("Finished\nBenchmark time: %d ms\n", (benchmarkEnd - benchmarkStart) / 1_000_000)); |
| 101 | + |
| 102 | + session.close(); |
| 103 | + } |
| 104 | + |
| 105 | + static void prepareKeyspaceAndTable(CqlSession session) { |
| 106 | + session.execute("DROP KEYSPACE IF EXISTS benchks"); |
| 107 | + session.execute("CREATE KEYSPACE IF NOT EXISTS benchks WITH REPLICATION = {'class' " + ": 'SimpleStrategy', 'replication_factor' : 1}"); |
| 108 | + session.execute("CREATE TABLE IF NOT EXISTS benchks.benchtab (pk " + "bigint PRIMARY KEY, v1 bigint, v2 bigint)"); |
| 109 | + if (!session.checkSchemaAgreement()) { |
| 110 | + throw new RuntimeException("Schema not in agreement after preparing keyspace and table."); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static void prepareSelectsBenchmark(CqlSession session) throws InterruptedException, ExecutionException { |
| 115 | + System.out.println("Preparing a selects benchmark (inserting values)..."); |
| 116 | + |
| 117 | + AtomicLong nextBatchStart = new AtomicLong(0); |
| 118 | + executor = Executors.newFixedThreadPool((int) config.concurrency); |
| 119 | + |
| 120 | + ArrayList<Future<?>> arr = new ArrayList<>(); |
| 121 | + try { |
| 122 | + for (int i = 0; i < config.concurrency; i++) { |
| 123 | + arr.add(executor.submit(() -> { |
| 124 | + PreparedStatement insertQ = session.prepare(INSERT_STRING); |
| 125 | + while (true) { |
| 126 | + long curBatchStart = nextBatchStart.addAndGet(config.batch_size); |
| 127 | + if (curBatchStart >= config.tasks) { |
| 128 | + break; |
| 129 | + } |
| 130 | + long curBatchEnd = Math.min(curBatchStart + config.batch_size, config.tasks); |
| 131 | + for (long pk = curBatchStart; pk < curBatchEnd; pk++) { |
| 132 | + session.execute(insertQ.bind(pk, 2L * pk, 3L * pk)); |
| 133 | + } |
| 134 | + } |
| 135 | + })); |
| 136 | + } |
| 137 | + } finally { |
| 138 | + executor.shutdown(); |
| 139 | + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
| 140 | + for (Future<?> f : arr) { |
| 141 | + f.get(); // make sure nothing has thrown |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static List<InetSocketAddress> parseAddresses(String[] arr){ |
| 147 | + ArrayList<InetSocketAddress> result = new ArrayList<>(); |
| 148 | + for(String s : arr){ |
| 149 | + InetSocketAddress addr; |
| 150 | + if(s.contains(":")){ |
| 151 | + String[] tmp = s.split(":"); |
| 152 | + addr = new InetSocketAddress(tmp[0], Integer.parseInt(tmp[1])); |
| 153 | + } |
| 154 | + else { |
| 155 | + addr = new InetSocketAddress(s, 9042); |
| 156 | + } |
| 157 | + result.add(addr); |
| 158 | + } |
| 159 | + return result; |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | + |
0 commit comments