Skip to content

Commit

Permalink
Adjust test code to do also delete and update
Browse files Browse the repository at this point in the history
  • Loading branch information
cupuyc committed Sep 16, 2016
1 parent 76da980 commit 2668c31
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Expand Up @@ -197,14 +197,22 @@ public Set<byte[]> keys() {
}

private void updateBatchInternal(Map<byte[], byte[]> rows) throws IOException {
int putCount = 0;
int deleteCount = 0;

try (WriteBatch batch = db.createWriteBatch()) {
for (Map.Entry<byte[], byte[]> entry : rows.entrySet()) {
if (entry.getValue() == null) {
batch.delete(entry.getKey());
deleteCount++;
} else {
batch.put(entry.getKey(), entry.getValue());
putCount++;
}
}

System.out.println("updateBatchInternal puts:" + putCount + ", deletes:" + deleteCount);

db.write(batch);
}
}
Expand Down
@@ -1,6 +1,7 @@
package org.ethereum.db;

import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.util.ByteUtil;

import java.io.File;
import java.util.HashMap;
Expand All @@ -12,37 +13,56 @@ public class TestLongBatchApplication {
private static final String NAME = "database-longbatch";
private static final File dbFile = new File("database-test/" + NAME);


public static void main(String[] args) {
System.out.println("Run");
LevelDbDataSource dataSource = new LevelDbDataSource(NAME);
dataSource.destroyDB(dbFile);
System.out.println("Removed data");
dataSource.init();

final int PART_1 = 500_000;
final int PART_2 = 500_000;
final int PART_3 = 2_000_000;

System.out.println("Small test write");
dataSource.updateBatch(createBatch(10));
dataSource.updateBatch(createRandomBatch(10)); // must be persisted small set

dataSource.updateBatch(createFixedBatch(0, PART_1 + PART_2, true));

System.out.println("Prepare data");
final int batchSize = 3000000;
final Map<byte[], byte[]> batch = createBatch(batchSize);

final Map<byte[], byte[]> batch1 = createFixedBatch(0, PART_1, true);// for update
final Map<byte[], byte[]> batch2 = createFixedBatch(PART_1, PART_1 + PART_2, false); // for delete
final Map<byte[], byte[]> batch3 = createFixedBatch(PART_1 + PART_2, PART_1 + PART_2 + PART_3, true); // for add

System.out.println("Starting long batch");
dataSource.updateBatch(batch);
System.out.println("Batch ended");
final Map<byte[], byte[]> batchAll = new HashMap<>();
batchAll.putAll(batch1);
batchAll.putAll(batch2);
batchAll.putAll(batch3);
System.out.println("Starting long batch " + batchAll.size());

dataSource.updateBatch(batchAll);

System.out.println("Batch ended");
dataSource.close();
}

private static Map<byte[], byte[]> createBatch(int batchSize) {
HashMap<byte[], byte[]> result = new HashMap<byte[], byte[]>();
private static Map<byte[], byte[]> createRandomBatch(int batchSize) {
HashMap<byte[], byte[]> result = new HashMap<>();
for (int i = 0; i < batchSize; i++) {
result.put(randomBytes(32), randomBytes(412));
}
return result;
}

private static Map<byte[], byte[]> createFixedBatch(int from, int to, boolean isRandom) {
HashMap<byte[], byte[]> result = new HashMap<>();
for (int i = from; i < to; i++) {
result.put(ByteUtil.intToBytes(i), isRandom ? randomBytes(412) : null);
}
return result;
}

public static byte[] randomBytes(int length) {
byte[] result = new byte[length];
new Random().nextBytes(result);
Expand Down
2 changes: 1 addition & 1 deletion runTest.sh
Expand Up @@ -19,7 +19,7 @@ while [ $COUNTER -gt 0 ]; do
echo "[SCRIPT] Spawning writter"
java -Xmx4g -cp ethereumj-core/build/libs/ethereumj-core-1.4.0-SNAPSHOT-all.jar org.ethereum.db.TestLongBatchApplication & pid=$!

SECS=$(( ( RANDOM % 10 ) + 25 ))
SECS=$(( ( RANDOM % 10 ) + 30 ))
# in the background, sleep for 10 secs then kill that process
echo "[SCRIPT] Sleep for $SECS seconds and then kill process"
(sleep $SECS && echo "[SCRIPT] Killed batch after $SECS seconds" && kill -9 $pid)
Expand Down

0 comments on commit 2668c31

Please sign in to comment.