Better tests

This commit is contained in:
Andrea Cavalli 2019-04-24 01:10:57 +02:00
parent 74e4260558
commit 8617a1848b
4 changed files with 196 additions and 60 deletions

View File

@ -8,6 +8,8 @@ import it.cavallium.strangedb.java.objects.lists.ListQuery;
import it.cavallium.strangedb.java.objects.lists.ObjectStrangeDbList;
import it.cavallium.strangedb.java.objects.lists.ValuePointer;
import it.cavallium.strangedb.java.objects.lists.operations.ContainsIgnoreCase;
import it.cavallium.strangedb.tests.performance.PerformanceListQuery;
import it.cavallium.strangedb.tests.performance.PerformanceTest;
import it.cavallium.strangedb.tests.query.*;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.cavallium.strangedb.java.objects.EnhancedObject;
@ -29,7 +31,7 @@ public class Performance {
private static Path rootDirectory;
private static Path dbDataFile;
private static Path dbReferencesFile;
private static DatabaseJava db;
public static DatabaseJava db;
private static boolean tempDirectory;
private static final int BASE_WIDTH = 35;
private static final int SPACE_WIDTH = BASE_WIDTH + 35;
@ -87,19 +89,19 @@ public class Performance {
System.out.println("Test name" + spaces.substring(0, spaces.length() - 5) + "Total Time | Time at 1 Time at 10 Time at 100 Time at 1K Time at 10K");
System.out.println(bars + "---------------+-----------------------------------------------------------------");
if (doInstantiationTests) {
testS("DatabaseCore creation", 300, Performance::deleteDb, Performance::generateDb, () -> {});
testS("DatabaseCore root creation", 300, Performance::regenDb, () -> db.loadRoot(PreloadedListContainer::new), () -> {});
testS("DatabaseCore creation", 300,PerformanceTest.createTest(Performance::deleteDb, Performance::generateDb, () -> {}));
testS("DatabaseCore root creation", 300, PerformanceTest.createTest(Performance::regenDb, () -> db.loadRoot(PreloadedListContainer::new), () -> {}));
}
final VariableWrapper<PreloadedListContainer> preloadedListContainer = new VariableWrapper<>(null);
final VariableWrapper<SimpleEnhancedObject> simpleEnhancedObjectContainer = new VariableWrapper<>(null);
if (doInstantiationTests) {
testS("ObjectStrangeDbList<Int> creation", 1000, () -> {
testS("ObjectStrangeDbList<Int> creation", 1000, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
}, () -> preloadedListContainer.var.list = new ObjectStrangeDbList<>(db), () -> {});
}, () -> preloadedListContainer.var.list = new ObjectStrangeDbList<>(db), () -> {}));
}
if (doFillTests) {
testS("ObjectStrangeDbList<Int>: Filling with 1000 items", 100, () -> {
testS("ObjectStrangeDbList<Int>: Filling with 1000 items", 100, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.list = new ObjectStrangeDbList<>(db);
@ -107,8 +109,8 @@ public class Performance {
for (int i = 0; i < 1000; i++) {
preloadedListContainer.var.list.add(1000);
}
}, () -> {});
testS("ObjectStrangeDbList<EnhancedObject>: Filling with 1000 items", 100, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<EnhancedObject>: Filling with 1000 items", 100, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.listOfEnhancedObj = new EnhancedObjectStrangeDbList<>(db, SimpleEnhancedObject.class);
@ -123,8 +125,8 @@ public class Performance {
for (int i = 0; i < 1000; i++) {
preloadedListContainer.var.listOfEnhancedObj.add(simpleEnhancedObjectContainer.var);
}
}, () -> {});
testS("ObjectStrangeDbList<Int>: Filling with 10000 items", 10, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<Int>: Filling with 10000 items", 10, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.list = new ObjectStrangeDbList<>(db);
@ -132,8 +134,8 @@ public class Performance {
for (int i = 0; i < 10000; i++) {
preloadedListContainer.var.list.add(1000);
}
}, () -> {});
testS("ObjectStrangeDbList<Int>: Filling with 100000 items", 1, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<Int>: Filling with 100000 items", 1, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.list = new ObjectStrangeDbList<>(db);
@ -141,10 +143,10 @@ public class Performance {
for (int i = 0; i < 100000; i++) {
preloadedListContainer.var.list.add(1000);
}
}, () -> {});
}, () -> {}));
}
if (doLoadTests) {
testS("ObjectStrangeDbList<Int>: Loading 1000 items", 100, () -> {
testS("ObjectStrangeDbList<Int>: Loading 1000 items", 100, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.list = new ObjectStrangeDbList<>(db);
@ -153,8 +155,8 @@ public class Performance {
}
}, () -> {
preloadedListContainer.var.list.forEachParallelUnsorted((i) -> {});
}, () -> {});
testS("ObjectStrangeDbList<EnhancedObject>: Loading with 1000 items", 100, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<EnhancedObject>: Loading with 1000 items", 100, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.listOfEnhancedObj = new EnhancedObjectStrangeDbList<>(db, SimpleEnhancedObject.class);
@ -170,8 +172,8 @@ public class Performance {
}
}, () -> {
preloadedListContainer.var.listOfEnhancedObj.forEachParallelUnsorted((i) -> {});
}, () -> {});
testS("ObjectStrangeDbList<Int>: Loading 10000 items", 10, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<Int>: Loading 10000 items", 10, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.list = new ObjectStrangeDbList<>(db);
@ -180,8 +182,8 @@ public class Performance {
}
}, () -> {
preloadedListContainer.var.list.forEachParallelUnsorted((i) -> {});
}, () -> {});
testS("ObjectStrangeDbList<Int>: getLast() with 1000 items", 100, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<Int>: getLast() with 1000 items", 100, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.list = new ObjectStrangeDbList<>(db);
@ -190,8 +192,8 @@ public class Performance {
}
}, () -> {
preloadedListContainer.var.list.getLast();
}, () -> {});
testS("ObjectStrangeDbList<EnhancedObject>: getLast() with 1000 items", 100, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<EnhancedObject>: getLast() with 1000 items", 100, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.listOfEnhancedObj = new EnhancedObjectStrangeDbList<>(db, SimpleEnhancedObject.class);
@ -207,8 +209,8 @@ public class Performance {
}
}, () -> {
preloadedListContainer.var.listOfEnhancedObj.getLast();
}, () -> {});
testS("ObjectStrangeDbList<Int>: size() with 1000 items", 100, () -> {
}, () -> {}));
testS("ObjectStrangeDbList<Int>: size() with 1000 items", 100, PerformanceTest.createTest(() -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.list = new ObjectStrangeDbList<>(db);
@ -217,39 +219,11 @@ public class Performance {
}
}, () -> {
preloadedListContainer.var.list.size();
}, () -> {});
}, () -> {}));
}
if (doQueryTests) {
for (int items = 1000; items <= 100000; items *= 10) {
final int itemsF = items;
testS("ListQuery: query with " + items + " items", 100 / (items / 1000), () -> {
regenDb();
preloadedListContainer.var = db.loadRoot(PreloadedListContainer::new);
preloadedListContainer.var.listOfMessages = new EnhancedObjectStrangeDbList<>(db);
Random random = new Random();
for (int i = 0; i < itemsF; i++) {
EMessageContent content;
if (random.nextBoolean()) {
List<String> stringList = new ArrayList<>();
for (int j = 0; j < 10; j++) {
stringList.add("[entity]");
}
byte[] stringBytes = new byte[200];
random.nextBytes(stringBytes);
content = new EMessageText(db, new EFormattedText(db, new String(stringBytes, StandardCharsets.UTF_8) + (random.nextBoolean() ? "not found" : " text to find!"), stringList.toArray(new String[0])));
} else {
content = new EMessageOtherContent(db, "EMPTY ABCDEFG");
}
EMessage message = new EMessage(db, content);
preloadedListContainer.var.listOfMessages.add(message);
}
}, () -> {
ListQuery query = ListQuery.create(
ValuePointer.ofField(EMessage.class, "content").field(EMessageText.class, "text").field(EFormattedText.class, "text"),
ContainsIgnoreCase.containsValue("text to find"));
ArrayList<EMessage> results = preloadedListContainer.var.listOfMessages.query(query).asList();
}, () -> {});
testS("ListQuery: query with " + items + " items", 100 / (items / 1000), new PerformanceListQuery(items));
}
}
System.out.println(bars + "---------------+-----------------------------------------------------------------");
@ -263,7 +237,7 @@ public class Performance {
private static void NtestS(String description, int times, RunnableWithIO beforeAction, RunnableWithIO action, RunnableWithIO afterAction) throws IOException, InterruptedException {
}
private static void testS(String description, int times, RunnableWithIO beforeAction, RunnableWithIO action, RunnableWithIO afterAction) throws IOException, InterruptedException {
private static void testS(String description, int times, PerformanceTest test) throws IOException, InterruptedException {
if (times >= 5 * DIVISOR) {
times /= 5 * DIVISOR;
@ -281,7 +255,7 @@ public class Performance {
for (int i = 0; i < spacesCount; i++) {
spaces.append(' ');
}
double[] results = test(times, beforeAction, action, afterAction);
double[] results = test(times, test);
if (cutAt > 0) {
System.out.println(description.substring(0, cutAt) + " |");
}
@ -302,19 +276,21 @@ public class Performance {
return spaces + String.format("%.2fms", result);
}
private static double[] test(int times, RunnableWithIO beforeAction, RunnableWithIO action, RunnableWithIO afterAction) throws IOException, InterruptedException {
private static double[] test(int times, PerformanceTest test) throws IOException, InterruptedException {
LongArrayList results = new LongArrayList(times);
Thread.sleep(100);
System.gc();
Thread.sleep(100);
test.setup();
for (int i = 0; i < times; i++) {
beforeAction.run();
test.perTestSetup();
long startTime = System.nanoTime();
action.run();
test.test();
long elapsedTime = System.nanoTime() - startTime;
afterAction.run();
test.perTestEnd();
results.add(elapsedTime);
}
test.end();
double result1 = results.stream().limit(1).mapToLong(val -> val).average().orElse(0.0) / 1000000d;
double result10 = results.stream().limit(10).mapToLong(val -> val).average().orElse(0.0) / 1000000d;
double result100 = results.stream().limit(100).mapToLong(val -> val).average().orElse(0.0) / 1000000d;

View File

@ -0,0 +1,67 @@
package it.cavallium.strangedb.tests.performance;
import it.cavallium.strangedb.java.objects.lists.EnhancedObjectStrangeDbList;
import it.cavallium.strangedb.java.objects.lists.ListQuery;
import it.cavallium.strangedb.java.objects.lists.ValuePointer;
import it.cavallium.strangedb.java.objects.lists.operations.ContainsIgnoreCase;
import it.cavallium.strangedb.tests.Performance;
import it.cavallium.strangedb.tests.query.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PerformanceListQuery implements PerformanceTest {
private final int items;
private Performance.PreloadedListContainer preloadedListContainer;
public PerformanceListQuery(int items) {
this.items = items;
}
@Override
public void setup() throws IOException {
Performance.regenDb();
preloadedListContainer = Performance.db.loadRoot(Performance.PreloadedListContainer::new);
}
@Override
public void perTestSetup() throws IOException {
preloadedListContainer.listOfMessages = new EnhancedObjectStrangeDbList<>(Performance.db);
Random random = new Random();
for (int i = 0; i < items; i++) {
EMessageContent content;
if (random.nextBoolean()) {
List<String> stringList = new ArrayList<>();
for (int j = 0; j < 10; j++) {
stringList.add("[entity]");
}
byte[] stringBytes = new byte[200];
random.nextBytes(stringBytes);
content = new EMessageText(Performance.db, new EFormattedText(Performance.db, new String(stringBytes, StandardCharsets.UTF_8) + (random.nextBoolean() ? "not found" : " text to find!"), stringList.toArray(new String[0])));
} else {
content = new EMessageOtherContent(Performance.db, "EMPTY ABCDEFG");
}
EMessage message = new EMessage(Performance.db, content);
preloadedListContainer.listOfMessages.add(message);
}
}
@Override
public void test() throws IOException {
PerformanceListQueryTest.test(preloadedListContainer);
}
@Override
public void perTestEnd() throws IOException {
}
@Override
public void end() throws IOException {
}
}

View File

@ -0,0 +1,22 @@
package it.cavallium.strangedb.tests.performance;
import it.cavallium.strangedb.java.objects.lists.ListQuery;
import it.cavallium.strangedb.java.objects.lists.ValuePointer;
import it.cavallium.strangedb.java.objects.lists.operations.ContainsIgnoreCase;
import it.cavallium.strangedb.tests.Performance;
import it.cavallium.strangedb.tests.query.EFormattedText;
import it.cavallium.strangedb.tests.query.EMessage;
import it.cavallium.strangedb.tests.query.EMessageText;
import java.io.IOException;
import java.util.ArrayList;
public class PerformanceListQueryTest {
public static void test(Performance.PreloadedListContainer preloadedListContainer) throws IOException {
ListQuery query = ListQuery.create(
ValuePointer.ofField(EMessage.class, "content").field(EMessageText.class, "text").field(EFormattedText.class, "text"),
ContainsIgnoreCase.containsValue("text to find"));
ArrayList<EMessage> results = preloadedListContainer.listOfMessages.query(query).asList();
}
}

View File

@ -0,0 +1,71 @@
package it.cavallium.strangedb.tests.performance;
import it.cavallium.strangedb.functionalinterfaces.RunnableWithIO;
import java.io.IOException;
public interface PerformanceTest {
void setup() throws IOException;
void perTestSetup() throws IOException;
void test() throws IOException;
void perTestEnd() throws IOException;
void end() throws IOException;
static PerformanceTest createTest(RunnableWithIO setupRunnable, RunnableWithIO perTestSetupRunnable, RunnableWithIO testRunnable, RunnableWithIO perTestEndRunnable, RunnableWithIO endRunnable) {
return new PerformanceTest() {
@Override
public void setup() throws IOException {
setupRunnable.run();
}
@Override
public void perTestSetup() throws IOException {
perTestSetupRunnable.run();
}
@Override
public void test() throws IOException {
testRunnable.run();
}
@Override
public void perTestEnd() throws IOException {
perTestEndRunnable.run();
}
@Override
public void end() throws IOException {
endRunnable.run();
}
};
}
static PerformanceTest createTest(RunnableWithIO perTestSetupRunnable, RunnableWithIO testRunnable, RunnableWithIO perTestEndRunnable) {
return new PerformanceTest() {
@Override
public void setup() {
}
@Override
public void perTestSetup() throws IOException {
perTestSetupRunnable.run();
}
@Override
public void test() throws IOException {
testRunnable.run();
}
@Override
public void perTestEnd() throws IOException {
perTestEndRunnable.run();
}
@Override
public void end() {
}
};
}
}