|
| 1 | +import com.datastax.driver.core.*; |
| 2 | +import com.google.common.base.Function; |
| 3 | +import com.google.common.util.concurrent.AsyncFunction; |
| 4 | +import com.google.common.util.concurrent.FutureCallback; |
| 5 | +import com.google.common.util.concurrent.Futures; |
| 6 | +import com.google.common.util.concurrent.ListenableFuture; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.concurrent.*; |
| 10 | +import java.util.concurrent.atomic.AtomicLong; |
| 11 | + |
| 12 | +public class MainClass { |
| 13 | + |
| 14 | + private static Cluster cluster; |
| 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 | + cluster = Cluster.builder().addContactPoints(config.node_addresses).withProtocolVersion(ProtocolVersion.V4).build(); |
| 32 | + cluster.getConfiguration().getPoolingOptions().setMaxQueueSize((int) Math.max(2048, 2 * config.concurrency)); |
| 33 | + Session session = cluster.connect(); |
| 34 | + |
| 35 | + prepareKeyspaceAndTable(session); |
| 36 | + |
| 37 | + if (!config.dont_prepare) { |
| 38 | + prepareKeyspaceAndTable(session); |
| 39 | + |
| 40 | + if (config.workload.equals(Config.Workload.Selects)) { |
| 41 | + prepareSelectsBenchmark(session); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + AtomicLong requestsSent = new AtomicLong(0); |
| 46 | + |
| 47 | + executor = Executors.newFixedThreadPool(config.threads); |
| 48 | + |
| 49 | + System.out.println("Starting the benchmark"); |
| 50 | + |
| 51 | + benchmarkStart = System.nanoTime(); |
| 52 | + ArrayList<Future<?>> arr = new ArrayList<>(); |
| 53 | + for (int i = 0; i < config.threads; i++) { |
| 54 | + arr.add( |
| 55 | + executor.submit(() -> { |
| 56 | + int permits = (int) (config.concurrency / config.threads); |
| 57 | + Semaphore semaphore = new Semaphore(permits); |
| 58 | + PreparedStatement insertQ = session.prepare(INSERT_STRING); |
| 59 | + PreparedStatement selectQ = session.prepare(SELECT_STRING); |
| 60 | + long pk; |
| 61 | + while ((pk = requestsSent.incrementAndGet()) < config.tasks) { |
| 62 | + try { |
| 63 | + semaphore.acquire(); |
| 64 | + } catch (InterruptedException e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + if (config.workload.equals(Config.Workload.Inserts) || config.workload.equals(Config.Workload.Mixed)) { |
| 68 | + ListenableFuture<ResultSet> resultSetA = session.executeAsync(insertQ.bind(pk, 2L * pk, 3L * pk)); |
| 69 | + ListenableFuture<ResultSet> resultSetB; |
| 70 | + |
| 71 | + long finalPk = pk; |
| 72 | + if (config.workload.equals(Config.Workload.Selects) || config.workload.equals(Config.Workload.Mixed)) { |
| 73 | + // Chain with select depending on workload |
| 74 | + resultSetB = Futures.transform(resultSetA, |
| 75 | + new AsyncFunction<ResultSet, ResultSet>() { |
| 76 | + public ListenableFuture<ResultSet> apply(ResultSet rs) throws Exception { |
| 77 | + return session.executeAsync(selectQ.bind(finalPk)); |
| 78 | + } |
| 79 | + }); |
| 80 | + } else { |
| 81 | + resultSetB = resultSetA; |
| 82 | + } |
| 83 | + ListenableFuture<Boolean> result; |
| 84 | + // Verify returned data depending on workload and return the permit |
| 85 | + if (config.workload.equals(Config.Workload.Selects) || config.workload.equals(Config.Workload.Mixed)) { |
| 86 | + result = Futures.transform(resultSetB, |
| 87 | + new Function<ResultSet, Boolean>() { |
| 88 | + public Boolean apply(ResultSet rs) { |
| 89 | + semaphore.release(); |
| 90 | + Row r = rs.one(); |
| 91 | + if ((r.getLong("v1") != 2 * finalPk) || (r.getLong("v2") != 3 * finalPk)) { |
| 92 | + throw new RuntimeException(String.format("Received incorrect data. " + "Expected: (%s, %s, %s). " + "Received: (%s, %s ,%s).", finalPk, 2 * finalPk, 3 * finalPk, r.getInt("pk"), r.getInt("v1"), r.getInt("v2"))); |
| 93 | + } |
| 94 | + return true; |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + } |
| 99 | + else { |
| 100 | + result = Futures.transform(resultSetB, |
| 101 | + new Function<ResultSet, Boolean>() { |
| 102 | + public Boolean apply(ResultSet rs) { |
| 103 | + semaphore.release(); |
| 104 | + return true; |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + try { |
| 111 | + //possible only if all futures ended successfully |
| 112 | + semaphore.acquire(permits); |
| 113 | + } catch (InterruptedException e) { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
| 116 | + })); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + executor.shutdown(); |
| 121 | + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
| 122 | + benchmarkEnd = System.nanoTime(); |
| 123 | + for (Future<?> f : arr) { |
| 124 | + f.get(); // make sure nothing has thrown |
| 125 | + } |
| 126 | + System.out.println(String.format("Finished\nBenchmark time: %d ms\n", (benchmarkEnd - benchmarkStart) / 1_000_000)); |
| 127 | + |
| 128 | + session.close(); |
| 129 | + if (cluster != null) cluster.close(); |
| 130 | + } |
| 131 | + |
| 132 | + static void prepareKeyspaceAndTable(Session session) { |
| 133 | + session.execute("DROP KEYSPACE IF EXISTS benchks"); |
| 134 | + session.execute("CREATE KEYSPACE IF NOT EXISTS benchks WITH REPLICATION = {'class' " + ": 'SimpleStrategy', 'replication_factor' : 1}"); |
| 135 | + session.execute("CREATE TABLE IF NOT EXISTS benchks.benchtab (pk " + "bigint PRIMARY KEY, v1 bigint, v2 bigint)"); |
| 136 | + if (!cluster.getMetadata().checkSchemaAgreement()) { |
| 137 | + throw new RuntimeException("Schema not in agreement after preparing keyspace and table."); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static void prepareSelectsBenchmark(Session session) throws InterruptedException, ExecutionException { |
| 142 | + System.out.println("Preparing a selects benchmark (inserting values)..."); |
| 143 | + |
| 144 | + AtomicLong nextBatchStart = new AtomicLong(0); |
| 145 | + executor = Executors.newFixedThreadPool((int) config.threads); |
| 146 | + |
| 147 | + ArrayList<Future<?>> arr = new ArrayList<>(); |
| 148 | + try { |
| 149 | + for (int i = 0; i < config.concurrency; i++) { |
| 150 | + arr.add(executor.submit(() -> { |
| 151 | + PreparedStatement insertQ = session.prepare(INSERT_STRING); |
| 152 | + while (true) { |
| 153 | + long curBatchStart = nextBatchStart.addAndGet(config.tasks / config.concurrency); |
| 154 | + if (curBatchStart >= config.tasks) { |
| 155 | + break; |
| 156 | + } |
| 157 | + long curBatchEnd = Math.min(curBatchStart + (config.tasks / config.concurrency), config.tasks); |
| 158 | + for (long pk = curBatchStart; pk < curBatchEnd; pk++) { |
| 159 | + session.execute(insertQ.bind(pk, 2L * pk, 3L * pk)); |
| 160 | + } |
| 161 | + } |
| 162 | + })); |
| 163 | + } |
| 164 | + } finally { |
| 165 | + executor.shutdown(); |
| 166 | + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
| 167 | + for (Future<?> f : arr) { |
| 168 | + f.get(); // make sure nothing has thrown |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | + |
0 commit comments