|
| 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 | + |
| 12 | +package org.eclipse.rdf4j.sail.lmdb.benchmark; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Random; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import org.apache.commons.io.FileUtils; |
| 22 | +import org.assertj.core.util.Files; |
| 23 | +import org.eclipse.rdf4j.common.transaction.IsolationLevels; |
| 24 | +import org.eclipse.rdf4j.model.IRI; |
| 25 | +import org.eclipse.rdf4j.repository.sail.SailRepository; |
| 26 | +import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection; |
| 27 | +import org.eclipse.rdf4j.sail.lmdb.LmdbStore; |
| 28 | +import org.openjdk.jmh.annotations.Benchmark; |
| 29 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 30 | +import org.openjdk.jmh.annotations.Fork; |
| 31 | +import org.openjdk.jmh.annotations.Level; |
| 32 | +import org.openjdk.jmh.annotations.Measurement; |
| 33 | +import org.openjdk.jmh.annotations.Mode; |
| 34 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 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.TearDown; |
| 39 | +import org.openjdk.jmh.annotations.Threads; |
| 40 | +import org.openjdk.jmh.annotations.Warmup; |
| 41 | +import org.openjdk.jmh.runner.Runner; |
| 42 | +import org.openjdk.jmh.runner.RunnerException; |
| 43 | +import org.openjdk.jmh.runner.options.Options; |
| 44 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 45 | + |
| 46 | +/** |
| 47 | + * Benchmarks insertion performance with synthetic data using multiple threads. |
| 48 | + */ |
| 49 | +@State(Scope.Benchmark) |
| 50 | +@Warmup(iterations = 2) |
| 51 | +@BenchmarkMode({ Mode.Throughput }) |
| 52 | +@Fork(value = 1, jvmArgs = { "-Xms2G", "-Xmx2G", "-XX:+UseG1GC" }) |
| 53 | +@Measurement(iterations = 3) |
| 54 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 55 | +@Threads(4) |
| 56 | +public class TransactionsPerSecondMultithreadedBenchmark { |
| 57 | + |
| 58 | + SailRepositoryConnection connection; |
| 59 | + RandomLiteralGenerator literalGenerator; |
| 60 | + Random random; |
| 61 | + int i; |
| 62 | + List<IRI> resources; |
| 63 | + List<IRI> predicates; |
| 64 | + protected SailRepository repository; |
| 65 | + protected File file; |
| 66 | + protected boolean forceSync = false; |
| 67 | + |
| 68 | + public static void main(String[] args) throws RunnerException { |
| 69 | + Options opt = new OptionsBuilder() |
| 70 | + .include("TransactionsPerSecondBenchmark\\.") // adapt to control which benchmarks to run |
| 71 | + .forks(1) |
| 72 | + .build(); |
| 73 | + |
| 74 | + new Runner(opt).run(); |
| 75 | + } |
| 76 | + |
| 77 | + @Setup(Level.Iteration) |
| 78 | + public void beforeClass() { |
| 79 | + if (connection != null) { |
| 80 | + connection.close(); |
| 81 | + connection = null; |
| 82 | + } |
| 83 | + i = 0; |
| 84 | + file = Files.newTemporaryFolder(); |
| 85 | + |
| 86 | + LmdbStore sail = new LmdbStore(file, ConfigUtil.createConfig().setForceSync(forceSync)); |
| 87 | + repository = new SailRepository(sail); |
| 88 | + connection = repository.getConnection(); |
| 89 | + random = new Random(1337); |
| 90 | + literalGenerator = new RandomLiteralGenerator(connection.getValueFactory(), random); |
| 91 | + resources = new ArrayList<>(); |
| 92 | + for (int i = 0; i < 10; i++) { |
| 93 | + resources.add(connection.getValueFactory().createIRI("some:resource-" + i)); |
| 94 | + } |
| 95 | + predicates = new ArrayList<>(); |
| 96 | + for (int i = 0; i < 10; i++) { |
| 97 | + predicates.add(connection.getValueFactory().createIRI("some:predicate-" + i)); |
| 98 | + } |
| 99 | + |
| 100 | + System.gc(); |
| 101 | + } |
| 102 | + |
| 103 | + IRI randomResource() { |
| 104 | + return resources.get(random.nextInt(resources.size())); |
| 105 | + } |
| 106 | + |
| 107 | + IRI randomPredicate() { |
| 108 | + return predicates.get(random.nextInt(predicates.size())); |
| 109 | + } |
| 110 | + |
| 111 | + @TearDown(Level.Iteration) |
| 112 | + public void afterClass() throws IOException { |
| 113 | + if (connection != null) { |
| 114 | + connection.close(); |
| 115 | + connection = null; |
| 116 | + } |
| 117 | + repository.shutDown(); |
| 118 | + FileUtils.deleteDirectory(file); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + @Benchmark |
| 123 | + public void transactions() { |
| 124 | + connection.begin(); |
| 125 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 126 | + connection.commit(); |
| 127 | + } |
| 128 | + |
| 129 | + @Benchmark |
| 130 | + public void transactionsLevelNone() { |
| 131 | + connection.begin(IsolationLevels.NONE); |
| 132 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 133 | + connection.commit(); |
| 134 | + } |
| 135 | + |
| 136 | + @Benchmark |
| 137 | + public void mediumTransactionsLevelNone() { |
| 138 | + connection.begin(IsolationLevels.NONE); |
| 139 | + for (int k = 0; k < 10; k++) { |
| 140 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 141 | + } |
| 142 | + connection.commit(); |
| 143 | + } |
| 144 | + |
| 145 | + @Benchmark |
| 146 | + public void mediumTransactionsLevelSnapshot() { |
| 147 | + connection.begin(IsolationLevels.SNAPSHOT); |
| 148 | + for (int k = 0; k < 10; k++) { |
| 149 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 150 | + } |
| 151 | + connection.commit(); |
| 152 | + } |
| 153 | + |
| 154 | + @Benchmark |
| 155 | + public void mediumTransactionsLevelSerializable() { |
| 156 | + connection.begin(IsolationLevels.SERIALIZABLE); |
| 157 | + for (int k = 0; k < 10; k++) { |
| 158 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 159 | + } |
| 160 | + connection.commit(); |
| 161 | + } |
| 162 | + |
| 163 | + @Benchmark |
| 164 | + public void largerTransaction() { |
| 165 | + connection.begin(); |
| 166 | + for (int k = 0; k < 10000; k++) { |
| 167 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 168 | + } |
| 169 | + connection.commit(); |
| 170 | + } |
| 171 | + |
| 172 | + @Benchmark |
| 173 | + public void largerTransactionLevelNone() { |
| 174 | + connection.begin(IsolationLevels.NONE); |
| 175 | + for (int k = 0; k < 10000; k++) { |
| 176 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 177 | + } |
| 178 | + connection.commit(); |
| 179 | + } |
| 180 | + |
| 181 | + @Benchmark |
| 182 | + public void veryLargerTransactionLevelNone() { |
| 183 | + connection.begin(IsolationLevels.NONE); |
| 184 | + for (int k = 0; k < 1000000; k++) { |
| 185 | + connection.add(randomResource(), randomPredicate(), literalGenerator.createRandomLiteral()); |
| 186 | + } |
| 187 | + connection.commit(); |
| 188 | + } |
| 189 | +} |
0 commit comments