|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.rdf4j.sail.lmdb.benchmark; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStream; |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +import org.apache.commons.io.FileUtils; |
| 20 | +import org.assertj.core.util.Files; |
| 21 | +import org.eclipse.rdf4j.common.transaction.IsolationLevels; |
| 22 | +import org.eclipse.rdf4j.model.Model; |
| 23 | +import org.eclipse.rdf4j.repository.sail.SailRepository; |
| 24 | +import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection; |
| 25 | +import org.eclipse.rdf4j.rio.Rio; |
| 26 | +import org.eclipse.rdf4j.sail.lmdb.LmdbStore; |
| 27 | +import org.openjdk.jmh.annotations.Benchmark; |
| 28 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 29 | +import org.openjdk.jmh.annotations.Fork; |
| 30 | +import org.openjdk.jmh.annotations.Level; |
| 31 | +import org.openjdk.jmh.annotations.Measurement; |
| 32 | +import org.openjdk.jmh.annotations.Mode; |
| 33 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 34 | +import org.openjdk.jmh.annotations.Param; |
| 35 | +import org.openjdk.jmh.annotations.Scope; |
| 36 | +import org.openjdk.jmh.annotations.Setup; |
| 37 | +import org.openjdk.jmh.annotations.State; |
| 38 | +import org.openjdk.jmh.annotations.Warmup; |
| 39 | +import org.openjdk.jmh.runner.Runner; |
| 40 | +import org.openjdk.jmh.runner.RunnerException; |
| 41 | +import org.openjdk.jmh.runner.options.Options; |
| 42 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 43 | + |
| 44 | +/** |
| 45 | + * Benchmarks loading the datagovbe-valid.ttl dataset in a single transaction across all supported LMDB isolation |
| 46 | + * levels. |
| 47 | + */ |
| 48 | +@State(Scope.Benchmark) |
| 49 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 50 | +@BenchmarkMode(Mode.AverageTime) |
| 51 | +@Fork(value = 1, jvmArgs = { "-Xms2G", "-Xmx2G", "-XX:+UseG1GC" }) |
| 52 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 53 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 54 | +public class DatagovLoadIsolationBenchmark { |
| 55 | + |
| 56 | + private static final String DATA_FILE = "benchmarkFiles/datagovbe-valid.ttl"; |
| 57 | + |
| 58 | + @Param({ "NONE", "READ_COMMITTED", "SNAPSHOT_READ", "SNAPSHOT", "SERIALIZABLE" }) |
| 59 | + public IsolationLevels isolationLevel; |
| 60 | + |
| 61 | + private Model data; |
| 62 | + |
| 63 | + @Setup(Level.Trial) |
| 64 | + public void setup() throws IOException, InterruptedException { |
| 65 | + try (InputStream resourceAsStream = Objects.requireNonNull( |
| 66 | + DatagovLoadIsolationBenchmark.class.getClassLoader().getResourceAsStream(DATA_FILE), |
| 67 | + "dataset resource not found: " + DATA_FILE)) { |
| 68 | + this.data = Rio.parse(resourceAsStream, "", Rio.getParserFormatForFileName(DATA_FILE).orElseThrow()); |
| 69 | + } |
| 70 | + System.gc(); |
| 71 | + Thread.sleep(100); |
| 72 | + System.gc(); |
| 73 | + Thread.sleep(100); |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) throws RunnerException { |
| 77 | + Options opt = new OptionsBuilder() |
| 78 | + .include(DatagovLoadIsolationBenchmark.class.getSimpleName()) |
| 79 | + .forks(0) |
| 80 | + .build(); |
| 81 | + |
| 82 | + new Runner(opt).run(); |
| 83 | + } |
| 84 | + |
| 85 | + @Benchmark |
| 86 | + public boolean loadDatagovFileSingleTransaction() throws IOException { |
| 87 | + return loadOnce(); |
| 88 | + } |
| 89 | + |
| 90 | + boolean loadOnce() throws IOException { |
| 91 | + File temporaryFolder = Files.newTemporaryFolder(); |
| 92 | + SailRepository sailRepository = null; |
| 93 | + try { |
| 94 | + sailRepository = new SailRepository(new LmdbStore(temporaryFolder, ConfigUtil.createConfig())); |
| 95 | + try (SailRepositoryConnection connection = sailRepository.getConnection()) { |
| 96 | + connection.begin(isolationLevel); |
| 97 | + connection.add(data); |
| 98 | + connection.commit(); |
| 99 | + return connection.hasStatement(null, null, null, true); |
| 100 | + } |
| 101 | + } finally { |
| 102 | + try { |
| 103 | + if (sailRepository != null) { |
| 104 | + sailRepository.shutDown(); |
| 105 | + } |
| 106 | + } finally { |
| 107 | + FileUtils.deleteDirectory(temporaryFolder); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments