Skip to content

Instantly share code, notes, and snippets.

@mpfau
Created May 4, 2020 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpfau/a62cce01b83b56afde0dbb588470bc18 to your computer and use it in GitHub Desktop.
Save mpfau/a62cce01b83b56afde0dbb588470bc18 to your computer and use it in GitHub Desktop.
Test sync performance of cassandra java driver 4.5.1
package de.tutao.db.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.BoundStatement;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import de.tutao.db.client.NoSuchColumnException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
public class DriverTestNew {
String KEYSPACE = "write_perf";
Integer CF = 1;
Integer ITERATIONS = 10000;
CqlSession session;
public void connect() {
session = CqlSession.builder().build();
if (!session.getMetadata().getKeyspace(KEYSPACE).isPresent()) {
session.execute("CREATE KEYSPACE " + KEYSPACE + " WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'dc1' : " + 1 + " };");
}
KeyspaceMetadata keyspace = session.getMetadata().getKeyspace(KEYSPACE).get();
if (!keyspace.getTable("\"" + CF.toString() + "\"").isPresent()) {
this.session.execute("CREATE TABLE " + keyspace + ".\"" + CF + "\" (r blob, c blob, v blob, PRIMARY KEY (r, c)) WITH COMPACT STORAGE;");
}
}
private void write() {
PreparedStatement statement = session.prepare("INSERT INTO " + KEYSPACE + ".\"" + CF + "\" (r, c, v) VALUES (:row,:column,:value);");
long start = System.currentTimeMillis();
for (Integer i = 0; i < ITERATIONS; i++) {
BoundStatement boundStatement = statement.boundStatementBuilder()
.setByteBuffer("row", ByteBuffer.wrap("myRow".getBytes()))
.setByteBuffer("column", ByteBuffer.wrap(BigInteger.valueOf(i).toByteArray()))
.setByteBuffer("value", ByteBuffer.wrap(BigInteger.valueOf(-i).toByteArray()))
.build();
session.execute(boundStatement);
}
System.out.println("write with new cassandra driver");
System.out.println(">" + (System.currentTimeMillis() - start));
}
private void read() {
long start;
System.out.println();
System.out.println("read with new cassandra driver");
start = System.currentTimeMillis();
PreparedStatement prepared = session.prepare("SELECT v FROM " + KEYSPACE + ".\"" + CF.toString() + "\" WHERE r=:row AND c=:column;");
for (Integer i = 0; i < ITERATIONS; i++) {
BoundStatement bound = prepared.boundStatementBuilder()
.setByteBuffer("row", ByteBuffer.wrap("myRow".getBytes()))
.setByteBuffer("column", ByteBuffer.wrap(BigInteger.valueOf(i).toByteArray()))
.build();
ResultSet result = session.execute(bound);
if (new BigInteger(result.one().getByteBuffer(0).array()).intValue() != -i) {
throw new RuntimeException("read unexpected value");
}
}
System.out.println(">" + (System.currentTimeMillis() - start));
}
public static void main(String[] args) throws NoSuchColumnException {
DriverTestNew test = new DriverTestNew();
test.connect();
test.write();
test.read();
System.out.println("finished");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment