Skip to content

Commit

Permalink
GH-3572 benchmark and test
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Jan 5, 2022
1 parent 4f3cee4 commit 82cf2b4
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2021 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
******************************************************************************/

package org.eclipse.rdf4j.sail.memory.benchmark;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;

@State(Scope.Benchmark)
public class BaseConcurrentBenchmark {

ExecutorService executorService;

@Setup(Level.Trial)
public void setup() throws Exception {
// Logger root = (Logger) LoggerFactory.getLogger("");
// root.setLevel(ch.qos.logback.classic.Level.INFO);

executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}

@TearDown(Level.Trial)
public void tearDown() throws Exception {
executorService.shutdownNow();
}

void threads(int threadCount, Runnable runnable) throws InterruptedException {

CountDownLatch latch = new CountDownLatch(1);
CountDownLatch latchDone = new CountDownLatch(threadCount);

for (int i = 0; i < threadCount; i++) {
executorService.submit(() -> {
try {
latch.await();
runnable.run();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
latchDone.countDown();
}
});
}

latch.countDown();
latchDone.await();

}

static InputStream getResourceAsStream(String filename) {
return BaseConcurrentBenchmark.class.getClassLoader().getResourceAsStream(filename);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*******************************************************************************
* Copyright (c) 2021 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
******************************************************************************/

package org.eclipse.rdf4j.sail.memory.benchmark;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.rdf4j.common.transaction.IsolationLevels;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.util.Values;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.eclipse.rdf4j.sail.memory.model.MemValue;
import org.eclipse.rdf4j.sail.memory.model.MemValueFactory;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;

/**
* @author Håvard M. Ottestad
*/
@State(Scope.Benchmark)
@Warmup(iterations = 20)
@BenchmarkMode({ Mode.AverageTime })
@Fork(value = 1, jvmArgs = { "-Xms1G", "-Xmx1G", })
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class MemValueFactoryConcurrentBenchmark extends BaseConcurrentBenchmark {

private SailRepository repository;
private List<List<Value>> values;

@Setup(Level.Trial)
public void setup() throws Exception {
super.setup();
repository = new SailRepository(new MemoryStore());

try (SailRepositoryConnection connection = repository.getConnection()) {
connection.begin(IsolationLevels.NONE);
try (InputStream resourceAsStream = getResourceAsStream("benchmarkFiles/datagovbe-valid.ttl")) {
connection.add(resourceAsStream, RDFFormat.TURTLE);
}
connection.commit();

List<Value> collect = connection.getStatements(null, null, null)
.stream()
.flatMap(s -> Stream.of(s.getSubject(), s.getPredicate(), s.getObject()))
.distinct()
.map(v -> {
if (v.isIRI()) {
return Values.iri(v.stringValue());
} else if (v.isBNode()) {
return Values.bnode(v.stringValue());
} else if (v.isLiteral()) {
Literal literal = (Literal) v;
if (literal.getLanguage().isPresent()) {
return Values.literal(literal.stringValue(), literal.getLanguage().get());
} else {
return Values.literal(literal.stringValue(), literal.getDatatype());
}
}
throw new IllegalStateException("Could not map '" + v + "'");
})
.collect(Collectors.toList());

Collections.shuffle(collect, new Random(4583295));

values = Lists.partition(collect, 10000);

}

}

public static void main(String[] args) throws Exception {
MemValueFactoryConcurrentBenchmark memValueFactoryConcurrentBenchmark = new MemValueFactoryConcurrentBenchmark();
memValueFactoryConcurrentBenchmark.setup();

System.out.println("Setup complete");

Stopwatch stopwatch = Stopwatch.createStarted();
while (stopwatch.elapsed(TimeUnit.MINUTES) < 5) {
memValueFactoryConcurrentBenchmark.getMemValues(new Blackhole(
"Today's password is swordfish. I understand instantiating Blackholes directly is dangerous."));
}

}

@TearDown(Level.Trial)
public void tearDown() throws Exception {
super.tearDown();
repository.shutDown();
}

@Benchmark
public void getMemValues(Blackhole blackhole) throws Exception {

MemValueFactory valueFactory = (MemValueFactory) repository.getValueFactory();

Random random = new Random(48593);

threads(100, () -> {

List<Value> values = this.values.get(random.nextInt(this.values.size()));

for (Value value : values) {
MemValue memValue = valueFactory.getMemValue(value);
blackhole.consume(memValue);
}

});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2022 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
******************************************************************************/

package org.eclipse.rdf4j.sail.memory.model;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.util.Values;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

public class WeakObjectRegistryTest {

@Test
@Timeout(5)
public void testGC() throws InterruptedException {

WeakObjectRegistry<IRI> objects = new WeakObjectRegistry<>();

IRI iri = Values.iri("http://example.com/1");

objects.add(iri);

assertTrue(objects.contains(iri));

iri = null;

while (objects.contains(Values.iri("http://example.com/1"))) {
System.gc();
Thread.sleep(10);
}

}

}

0 comments on commit 82cf2b4

Please sign in to comment.