|
| 1 | +import com.datastax.driver.core.*; |
| 2 | +import com.google.common.util.concurrent.*; |
| 3 | + |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.concurrent.*; |
| 6 | + |
| 7 | +public class MainClass { |
| 8 | + |
| 9 | + private static Cluster cluster; |
| 10 | + private static Config config; |
| 11 | + private static final String INSERT_STRING = "INSERT INTO benchks.benchtab (pk, v1, v2) VALUES(?, ?, ?)"; |
| 12 | + private static final String SELECT_STRING = "SELECT v1, v2 FROM benchks.benchtab WHERE pk = ?"; |
| 13 | + |
| 14 | + private static PreparedStatement INSERT_PS; |
| 15 | + private static PreparedStatement SELECT_PS; |
| 16 | + |
| 17 | + public static void main(String[] args) throws InterruptedException, ExecutionException { |
| 18 | + |
| 19 | + config = new Config(args); |
| 20 | + System.out.println("Parsed config: "); |
| 21 | + System.out.println(config.toString()); |
| 22 | + |
| 23 | + cluster = Cluster.builder().addContactPoints(config.node_addresses).withProtocolVersion(ProtocolVersion.V4).build(); |
| 24 | + cluster.getConfiguration().getPoolingOptions().setMaxQueueSize((int) Math.max(2048, 2 * config.concurrency)); |
| 25 | + Session session = cluster.connect(); |
| 26 | + |
| 27 | + prepareKeyspaceAndTable(session); |
| 28 | + |
| 29 | + if (!config.dont_prepare) { |
| 30 | + prepareKeyspaceAndTable(session); |
| 31 | + |
| 32 | + if (config.workload.equals(Config.Workload.Selects)) { |
| 33 | + prepareSelectsBenchmark(session); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + ArrayList<CompletableFuture<ResultSet>> arr = new ArrayList<>(); |
| 39 | + |
| 40 | + System.out.println("Starting the benchmark"); |
| 41 | + |
| 42 | + long benchmarkStart = System.nanoTime(); |
| 43 | + |
| 44 | + INSERT_PS = session.prepare(INSERT_STRING); |
| 45 | + SELECT_PS = session.prepare(SELECT_STRING); |
| 46 | + |
| 47 | + for (int i = 0; i < config.concurrency; i++) { |
| 48 | + if (i + 1 == config.concurrency) { |
| 49 | + arr.add(execute(session, i * (config.tasks / config.concurrency), config.tasks)); |
| 50 | + } else { |
| 51 | + arr.add(execute(session, i * (config.tasks / config.concurrency), (i + 1) * (config.tasks / config.concurrency))); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + for (Future<?> f : arr) { |
| 56 | + f.get(); // make sure nothing has thrown and everything finished |
| 57 | + } |
| 58 | + |
| 59 | + long benchmarkEnd = System.nanoTime(); |
| 60 | + System.out.println(String.format("Finished\nBenchmark time: %d ms\n", (benchmarkEnd - benchmarkStart) / 1_000_000)); |
| 61 | + |
| 62 | + session.close(); |
| 63 | + if (cluster != null) cluster.close(); |
| 64 | + } |
| 65 | + |
| 66 | + static void prepareKeyspaceAndTable(Session session) { |
| 67 | + session.execute("DROP KEYSPACE IF EXISTS benchks"); |
| 68 | + session.execute("CREATE KEYSPACE IF NOT EXISTS benchks WITH REPLICATION = {'class' " + ": 'SimpleStrategy', 'replication_factor' : 1}"); |
| 69 | + session.execute("CREATE TABLE IF NOT EXISTS benchks.benchtab (pk " + "bigint PRIMARY KEY, v1 bigint, v2 bigint)"); |
| 70 | + if (!cluster.getMetadata().checkSchemaAgreement()) { |
| 71 | + throw new RuntimeException("Schema not in agreement after preparing keyspace and table."); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static void prepareSelectsBenchmark(Session session) throws InterruptedException, ExecutionException { |
| 76 | + System.out.println("Preparing a selects benchmark (inserting values)..."); |
| 77 | + |
| 78 | + ArrayList<CompletableFuture<ResultSet>> arr = new ArrayList<>(); |
| 79 | + INSERT_PS = session.prepare(INSERT_STRING); |
| 80 | + |
| 81 | + Config.Workload originalWorkload = config.workload; |
| 82 | + config.workload = Config.Workload.Inserts; // Switch for setup purposes |
| 83 | + |
| 84 | + for (int i = 0; i < config.concurrency; i++) { |
| 85 | + if (i + 1 == config.concurrency) { |
| 86 | + arr.add(execute(session, i * (config.tasks / config.concurrency), config.tasks)); |
| 87 | + } else { |
| 88 | + arr.add(execute(session, i * (config.tasks / config.concurrency), (i + 1) * (config.tasks / config.concurrency))); |
| 89 | + } |
| 90 | + } |
| 91 | + for (Future<?> f : arr) { |
| 92 | + f.get(); // make sure nothing has thrown and everything finished |
| 93 | + } |
| 94 | + |
| 95 | + config.workload = originalWorkload; |
| 96 | + } |
| 97 | + |
| 98 | + public static CompletableFuture<ResultSet> execute(Session s, long currentIter, long maxIter) { |
| 99 | + if (currentIter >= maxIter) { |
| 100 | + // No more iterations |
| 101 | + return CompletableFuture.completedFuture(null); |
| 102 | + } |
| 103 | + |
| 104 | + ListenableFuture<ResultSet> fut = null; |
| 105 | + if (config.workload.equals(Config.Workload.Inserts) || config.workload.equals(Config.Workload.Mixed)) { |
| 106 | + fut = s.executeAsync(INSERT_PS.bind(currentIter, 2L * currentIter, 3L * currentIter)); |
| 107 | + } |
| 108 | + |
| 109 | + if (config.workload.equals(Config.Workload.Selects)) { |
| 110 | + fut = s.executeAsync(SELECT_PS.bind(currentIter)); |
| 111 | + |
| 112 | + } else if (config.workload.equals(Config.Workload.Mixed)) { |
| 113 | + fut = Futures.transform(fut, new AsyncFunction<ResultSet, ResultSet>() { |
| 114 | + public ListenableFuture<ResultSet> apply(ResultSet rs) throws Exception { |
| 115 | + return (s.executeAsync(SELECT_PS.bind(currentIter))); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + if (config.workload.equals(Config.Workload.Selects) || config.workload.equals(Config.Workload.Mixed)) { |
| 121 | + fut = Futures.transform(fut, new AsyncFunction<ResultSet, ResultSet>() { |
| 122 | + public ListenableFuture<ResultSet> apply(ResultSet rs) throws Exception { |
| 123 | + Row r = rs.one(); |
| 124 | + if ((r.getLong("v1") != 2L * currentIter) || (r.getLong("v2") != 3L * currentIter)) { |
| 125 | + throw new RuntimeException(String.format("Received incorrect data. " + "Expected: (%s, %s, %s). " + "Received: (%s, %s ,%s).", currentIter, 2L * currentIter, 3L * currentIter, r.getLong("pk"), r.getLong("v1"), r.getLong("v2"))); |
| 126 | + } |
| 127 | + return Futures.immediateFuture(rs); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + // Convert ResultSetFuture to CompletableFuture |
| 133 | + CompletableFuture<ResultSet> futCompletable = new CompletableFuture<>(); |
| 134 | + Futures.addCallback(fut, new FutureCallback<ResultSet>() { |
| 135 | + @Override |
| 136 | + public void onSuccess(ResultSet result) { |
| 137 | + futCompletable.complete(result); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void onFailure(Throwable t) { |
| 142 | + futCompletable.completeExceptionally(t); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + // Execute next iteration after that |
| 147 | + return futCompletable.thenCompose(rs -> execute(s, currentIter + 1, maxIter)); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | + |
0 commit comments