Created
May 4, 2020 11:29
-
-
Save mpfau/7905cea3b73d235033e4f3319e219d15 to your computer and use it in GitHub Desktop.
Test sync performance of cassandra java driver 3.9.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.datastax.driver.core.*; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.math.BigInteger; | |
import java.nio.ByteBuffer; | |
public class DriverTestOld { | |
static Logger log = LoggerFactory.getLogger("test"); | |
String KEYSPACE = "write_perf"; | |
Integer CF = 1; | |
Integer ITERATIONS = 10000; | |
Cluster cluster; | |
Session session; | |
public void connect() { | |
cluster = Cluster.builder().addContactPoints("127.0.0.1").build(); | |
Metadata metadata = cluster.getMetadata(); | |
log.info("connected to cluster: " + metadata.getClusterName()); | |
for (Host host : metadata.getAllHosts()) { | |
log.info("datacenter: " + host.getDatacenter() + ", host: " + host.getAddress() + ", rack: " + host.getRack()); | |
} | |
session = cluster.connect(); | |
if (cluster.getMetadata().getKeyspace(KEYSPACE) == null) { | |
session.execute("CREATE KEYSPACE " + KEYSPACE + " WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'dc1' : " + 1 + " };"); | |
} | |
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(KEYSPACE); | |
if ((keyspace.getTable(CF.toString()) == null)) { | |
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.bind() | |
.setBytes("row", ByteBuffer.wrap("myRow".getBytes())) | |
.setBytes("column", ByteBuffer.wrap(BigInteger.valueOf(i).toByteArray())) | |
.setBytes("value", ByteBuffer.wrap(BigInteger.valueOf(-i).toByteArray())); | |
session.execute(boundStatement); | |
} | |
System.out.println("write with old cassandra driver"); | |
System.out.println(">" + (System.currentTimeMillis() - start)); | |
} | |
private void read() { | |
long start; | |
System.out.println(); | |
System.out.println("read with old 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.bind() | |
.setBytes("row", ByteBuffer.wrap("myRow".getBytes())) | |
.setBytes("column", ByteBuffer.wrap(BigInteger.valueOf(i).toByteArray())); | |
ResultSet result = session.execute(bound); | |
if (new BigInteger(result.one().getBytes(0).array()).intValue() != -i) { | |
throw new RuntimeException("read unexpected value"); | |
} | |
} | |
System.out.println(">" + (System.currentTimeMillis() - start)); | |
} | |
public static void main(String[] args) { | |
DriverTestOld test = new DriverTestOld(); | |
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