Improve data generation speed - from Jens Ebbesen.

This commit is contained in:
Mark Nellemann 2023-06-27 21:51:02 +02:00
parent 9ea6a1eecb
commit e6db0b6f90

View file

@ -30,28 +30,41 @@ public class MyDatabase {
private final int maxRowsPerTable; private final int maxRowsPerTable;
private final int maxDataPerRow; private final int maxDataPerRow;
private int idx = 0;
private int idx2 = 0;
private char[] baseCar;
private byte[] byteBase;
public MyDatabase(int tables, int rows, int size) { public MyDatabase(int tables, int rows, int size) {
this.maxTables = tables; this.maxTables = tables;
this.maxRowsPerTable = rows; this.maxRowsPerTable = rows;
this.maxDataPerRow = size; this.maxDataPerRow = size;
baseCar = new char[128];
byteBase = new byte[BYTE_SIZE_1MB];
for (int i = 0; i < 128; i++) {
baseCar[i] = 'A';
}
for (int i = 0; i < byteBase.length; i++) {
byteBase[i] = 0;
}
} }
public Database build(String dbName) { public Database build(String dbName) {
Instant instant1 = Instant.now(); Instant instant1 = Instant.now();
Database database = databaseManager.createDatabase(dbName); Database database = databaseManager.createDatabase(dbName);
for(int t = 1; t <= maxTables; t++) { for (int t = 1; t <= maxTables; t++) {
String tableName = String.format("table_%d", t); String tableName = String.format("table_%d", t);
log.info("Creating table \"{}\"", tableName); log.info("Creating table \"{}\"", tableName);
Table table = database.createTable(tableName); Table table = database.createTable(tableName);
for(int r = 1; r <= maxRowsPerTable; r++) { for (int r = 1; r <= maxRowsPerTable; r++) {
String rowIdx = String.format("%d_of_%d", r, maxRowsPerTable); String rowIdx = String.format("%d_of_%d", r, maxRowsPerTable);
HashMap<String, ByteBuffer> map = new HashMap<String,ByteBuffer>(); HashMap<String, ByteBuffer> map = new HashMap<String, ByteBuffer>();
for(int m = 1; m <= maxDataPerRow; m++) { for (int m = 1; m <= maxDataPerRow; m++) {
map.put(randomString(128), ByteBuffer.wrap(randomBytes(BYTE_SIZE_1MB)) ); map.put(randomString(), randomBytes());
} }
table.insertEntry(rowIdx, map); table.insertEntry(rowIdx, map);
} }
@ -64,22 +77,20 @@ public class MyDatabase {
return database; return database;
} }
String randomString() {
String randomString(final int length) { baseCar[(idx++) % 128]++;
StringBuilder sb = new StringBuilder(length); String s = new String(baseCar);
for(int l = 0; l < length; l++) { return s;
sb.append( (char) random.nextInt(65_535));
}
return sb.toString();
} }
ByteBuffer randomBytes() {
byte[] randomBytes(final int length) { byteBase[(idx2++) % byteBase.length]++;
byte[] randomArray = new byte[length]; byte[] bytes = new byte[byteBase.length];
random.nextBytes(randomArray); for (int i = 0; i < bytes.length; i++) {
return randomArray; bytes[i] = byteBase[i];
}
return ByteBuffer.wrap(bytes);
} }
void destroy(final String dbName) { void destroy(final String dbName) {
databaseManager.deleteDatabase(dbName); databaseManager.deleteDatabase(dbName);