Skip to content

Instantly share code, notes, and snippets.

@komamitsu
Created August 9, 2023 03:41
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 komamitsu/6df9fd53312c33b46b4bb0c9018652b9 to your computer and use it in GitHub Desktop.
Save komamitsu/6df9fd53312c33b46b4bb0c9018652b9 to your computer and use it in GitHub Desktop.
package org.example;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.example.ConnectionFactory.Id;
public class YugabyteTest {
private static class Stats {
private final ConnectionFactory.Id connFactoryId;
AtomicInteger insertedCount = new AtomicInteger();
AtomicInteger deletedCount = new AtomicInteger();
AtomicInteger selectedCount = new AtomicInteger();
CopyOnWriteArrayList<Exception> exceptions = new CopyOnWriteArrayList<>();
public Stats(Id connFactoryId) {
this.connFactoryId = connFactoryId;
}
@Override
public String toString() {
return "Stats{" +
"connFactoryId=" + connFactoryId +
", insertedCount=" + insertedCount +
", deletedCount=" + deletedCount +
", selectedCount=" + selectedCount +
", exceptions=" + aggregateExceptions() +
'}';
}
private Map<String, Integer> aggregateExceptions() {
Map<String, Integer> summary = new HashMap<>();
exceptions.forEach(ex -> {
String key = ex.getMessage();
summary.put(key, summary.computeIfAbsent(key, k -> 0) + 1);
});
return summary;
}
}
private void init(ConnectionFactory connFactory) throws SQLException {
try (Connection conn = connFactory.connect();
Statement stmt = conn.createStatement()) {
stmt.execute("CREATE TABLE IF NOT EXISTS float_tbl (pk double precision PRIMARY KEY)");
}
try (Connection conn = connFactory.connect();
Statement stmt = conn.createStatement()) {
stmt.execute("TRUNCATE TABLE float_tbl");
}
}
private void execute(ConnectionFactory connFactory, Stats stats, float key) throws SQLException {
try (Connection conn = connFactory.connect();
PreparedStatement ps = conn.prepareStatement("INSERT INTO float_tbl (pk) VALUES (?)")) {
ps.setFloat(1, key);
if (ps.executeUpdate() > 0) {
stats.insertedCount.incrementAndGet();
}
}
try (Connection conn = connFactory.connect();
PreparedStatement ps = conn.prepareStatement("DELETE FROM float_tbl WHERE pk = ?")) {
ps.setFloat(1, key);
if (ps.executeUpdate() > 0) {
stats.deletedCount.incrementAndGet();
}
}
try (Connection conn = connFactory.connect();
PreparedStatement ps = conn.prepareStatement("SELECT pk FROM float_tbl WHERE pk = ?")) {
ps.setFloat(1, key);
ResultSet resultSet = ps.executeQuery();
if (resultSet.next()) {
stats.selectedCount.incrementAndGet();
}
}
}
public static void main(String[] args) throws SQLException {
YugabyteTest yugabyteTest = new YugabyteTest();
for (ConnectionFactory.Id connFactoryId : Arrays.asList(Id.YUGABYTE, Id.PG, Id.MYSQL)) {
ConnectionFactory connFactory = ConnectionFactory.of(connFactoryId);
yugabyteTest.init(connFactory);
int n = 10000;
Stats stats = new Stats(connFactoryId);
Random random = new Random();
AtomicInteger count = new AtomicInteger();
random.doubles(n, Float.MIN_VALUE, Float.MAX_VALUE).parallel().forEach(key -> {
int currentCount = count.getAndIncrement();
if (currentCount % 1000 == 0) {
// System.out.println("count: " + currentCount);
}
// System.out.println(key);
try {
yugabyteTest.execute(connFactory, stats, (float) key);
}
catch (Exception e) {
stats.exceptions.add(e);
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
});
System.out.println(stats);
}
}
}
@komamitsu
Copy link
Author

STDOUT

Stats{connFactoryId=YUGABYTE, insertedCount=9998, deletedCount=0, selectedCount=9998, exceptions={ERROR: duplicate key value violates unique constraint "float_tbl_pkey"=2}}
Stats{connFactoryId=PG, insertedCount=10000, deletedCount=10000, selectedCount=0, exceptions={}}
Stats{connFactoryId=MYSQL, insertedCount=10000, deletedCount=10000, selectedCount=0, exceptions={}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment