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
1 changed files with 29 additions and 18 deletions

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,23 +77,21 @@ public class MyDatabase {
return database; return database;
} }
String randomString() {
baseCar[(idx++) % 128]++;
String s = new String(baseCar);
return s;
}
String randomString(final int length) { ByteBuffer randomBytes() {
StringBuilder sb = new StringBuilder(length); byteBase[(idx2++) % byteBase.length]++;
for(int l = 0; l < length; l++) { byte[] bytes = new byte[byteBase.length];
sb.append( (char) random.nextInt(65_535)); for (int i = 0; i < bytes.length; i++) {
bytes[i] = byteBase[i];
} }
return sb.toString(); return ByteBuffer.wrap(bytes);
} }
byte[] randomBytes(final int length) {
byte[] randomArray = new byte[length];
random.nextBytes(randomArray);
return randomArray;
}
void destroy(final String dbName) { void destroy(final String dbName) {
databaseManager.deleteDatabase(dbName); databaseManager.deleteDatabase(dbName);
} }