forked from eclipse-rdf4j/rdf4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fffdfc
commit ac834fb
Showing
2 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...y/src/test/java/org/eclipse/rdf4j/sail/memory/benchmark/BaseConcurrentBenchmark.java.save
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/******************************************************************************* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.sail.memory.benchmark; | ||
|
||
import java.io.InputStream; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.rdf4j.common.transaction.IsolationLevel; | ||
import org.eclipse.rdf4j.repository.Repository; | ||
import org.eclipse.rdf4j.repository.RepositoryConnection; | ||
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 { | ||
|
||
Repository repository; | ||
private ExecutorService executorService; | ||
|
||
static InputStream getResourceAsStream(String filename) { | ||
return BaseConcurrentBenchmark.class.getClassLoader().getResourceAsStream(filename); | ||
} | ||
|
||
@Setup(Level.Trial) | ||
public void setup() throws Exception { | ||
if (executorService != null) { | ||
executorService.shutdownNow(); | ||
} | ||
executorService = Executors.new | ||
} | ||
|
||
@TearDown(Level.Trial) | ||
public void tearDown() throws Exception { | ||
if (executorService != null) { | ||
executorService.shutdownNow(); | ||
executorService = null; | ||
} | ||
} | ||
|
||
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(); | ||
|
||
} | ||
|
||
Future<?> submit(Runnable runnable) { | ||
return executorService.submit(runnable); | ||
} | ||
|
||
Runnable getRunnable(CountDownLatch startSignal, RepositoryConnection connection, | ||
IsolationLevel isolationLevel, Consumer<RepositoryConnection> workload) { | ||
|
||
return () -> { | ||
try { | ||
startSignal.await(); | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(); | ||
} | ||
RepositoryConnection localConnection = connection; | ||
try { | ||
if (localConnection == null) { | ||
localConnection = repository.getConnection(); | ||
} | ||
|
||
if (isolationLevel == null) { | ||
localConnection.begin(); | ||
} else { | ||
localConnection.begin(isolationLevel); | ||
} | ||
workload.accept(localConnection); | ||
localConnection.commit(); | ||
|
||
} finally { | ||
if (connection == null) { | ||
assert localConnection != null; | ||
localConnection.close(); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
<T> Runnable getRunnable(CountDownLatch startSignal, T inputData, | ||
Consumer<T> workload) { | ||
|
||
return () -> { | ||
try { | ||
startSignal.await(); | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(); | ||
} | ||
workload.accept(inputData); | ||
}; | ||
} | ||
|
||
<T, S> Runnable getRunnable(CountDownLatch startSignal, T inputData1, | ||
S inputData2, BiConsumer<T, S> workload) { | ||
|
||
return () -> { | ||
try { | ||
startSignal.await(); | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(); | ||
} | ||
workload.accept(inputData1, inputData2); | ||
}; | ||
} | ||
|
||
} |