Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fdb-relational-core/fdb-relational-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

plugins {
id 'java-test-fixtures'
alias(libs.plugins.jmh)
}

apply from: rootProject.file('gradle/antlr.gradle')
Expand Down Expand Up @@ -84,3 +85,12 @@ publishing {
}
}
}

jmh {
environment = rootProject.ext.fdbEnvironment
resultFormat = 'text'
}

project.tasks.named("processJmhResources") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.apple.foundationdb.relational.api.RelationalConnection;
import com.apple.foundationdb.relational.api.RelationalStatement;
import com.apple.foundationdb.relational.api.RelationalStruct;
import com.apple.foundationdb.relational.api.catalog.DatabaseTemplate;
import com.apple.foundationdb.relational.api.exceptions.RelationalException;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand Down Expand Up @@ -82,13 +81,13 @@ public void trialDown() throws RelationalException {
@Setup(Level.Iteration)
public void setUp(ThreadScopedDatabases databases) throws RelationalException, SQLException {
databases.createDatabase(
DatabaseTemplate.newBuilder()
.withSchema(singleReadSchema, schemaTemplateName)
.withSchema(singleWriteSchema, schemaTemplateName)
.build(),
dbName);
getUri(dbName, false),
schemaTemplateName,
singleReadSchema,
singleWriteSchema);

try (RelationalConnection dbConn = DriverManager.getConnection(getUri(dbName, true).toString()).unwrap(RelationalConnection.class)) {
try (java.sql.Connection rawConn = DriverManager.getConnection(getUri(dbName, true).toString());
RelationalConnection dbConn = rawConn.unwrap(RelationalConnection.class)) {
dbConn.setSchema(singleReadSchema);
try (RelationalStatement stmt = dbConn.createStatement()) {
stmt.executeInsert(restaurantRecordTable, newRestaurantRecord(42));
Expand All @@ -98,7 +97,8 @@ public void setUp(ThreadScopedDatabases databases) throws RelationalException, S

@Benchmark
public void singleWrite(Blackhole bh) throws SQLException {
try (RelationalConnection dbConn = DriverManager.getConnection(getUri(dbName, true).toString()).unwrap(RelationalConnection.class)) {
try (java.sql.Connection rawConn = DriverManager.getConnection(getUri(dbName, true).toString());
RelationalConnection dbConn = rawConn.unwrap(RelationalConnection.class)) {
dbConn.setSchema(singleWriteSchema);
try (RelationalStatement stmt = dbConn.createStatement()) {
bh.consume(stmt.executeInsert(restaurantRecordTable, newRestaurantRecord()));
Expand All @@ -108,7 +108,8 @@ public void singleWrite(Blackhole bh) throws SQLException {

@Benchmark
public void singlePkRead(Blackhole bh) throws SQLException {
try (RelationalConnection dbConn = DriverManager.getConnection(getUri(dbName, true).toString()).unwrap(RelationalConnection.class)) {
try (java.sql.Connection rawConn = DriverManager.getConnection(getUri(dbName, true).toString());
RelationalConnection dbConn = rawConn.unwrap(RelationalConnection.class)) {
dbConn.setSchema(singleReadSchema);
try (RelationalStatement stmt = dbConn.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * FROM \"RestaurantRecord\" WHERE \"rest_no\" = 42")) {
Expand All @@ -121,7 +122,8 @@ public void singlePkRead(Blackhole bh) throws SQLException {

@Benchmark
public void singleNonPkRead(Blackhole bh) throws SQLException {
try (RelationalConnection dbConn = DriverManager.getConnection(getUri(dbName, true).toString()).unwrap(RelationalConnection.class)) {
try (java.sql.Connection rawConn = DriverManager.getConnection(getUri(dbName, true).toString());
RelationalConnection dbConn = rawConn.unwrap(RelationalConnection.class)) {
dbConn.setSchema(singleReadSchema);
try (RelationalStatement stmt = dbConn.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * from \"RestaurantRecord\" WHERE \"name\" = 'testName'")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import com.apple.foundationdb.annotation.API;

import com.apple.foundationdb.relational.api.catalog.DatabaseTemplate;
import com.apple.foundationdb.relational.api.exceptions.RelationalException;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand All @@ -42,6 +41,7 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.net.URI;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -80,10 +80,9 @@ public String getNextDbName() {
@Benchmark
public void createDatabase(ThreadScopedDatabases databases, DbNameGenerator dbNameGenerator) throws RelationalException, SQLException {
databases.createDatabase(
DatabaseTemplate.newBuilder()
.withSchema(schema, schemaTemplateName)
.build(),
dbNameGenerator.getNextDbName());
URI.create(dbNameGenerator.getNextDbName()),
schemaTemplateName,
schema);
}

public static void main(String[] args) throws RunnerException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.apple.foundationdb.relational.api.Options;
import com.apple.foundationdb.relational.api.Transaction;
import com.apple.foundationdb.relational.api.RelationalDriver;
import com.apple.foundationdb.relational.api.catalog.DatabaseTemplate;
import com.apple.foundationdb.relational.api.catalog.StoreCatalog;
import com.apple.foundationdb.relational.api.exceptions.ErrorCode;
import com.apple.foundationdb.relational.api.exceptions.UncheckedRelationalException;
Expand All @@ -49,7 +48,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -87,7 +85,7 @@ public Driver() {
}

public Driver(RelationalPlanCache planCache) {
this(schemaTemplateName,templateDefinition,planCache);
this(schemaTemplateName, templateDefinition, planCache);
}

public Driver(String templateName, String templateDef) {
Expand Down Expand Up @@ -141,6 +139,11 @@ private void createSchemaTemplate() throws SQLException {
try (final var conn = DriverManager.getConnection("jdbc:embed:/__SYS")) {
conn.setSchema("CATALOG");
try (final var statement = conn.createStatement()) {
try {
statement.executeUpdate("DROP SCHEMA TEMPLATE \"" + templateName + "\"");
} catch (SQLException ignored) {
// Template didn't exist yet — that's fine
}
statement.executeUpdate("CREATE SCHEMA TEMPLATE \"" + templateName + "\" " + templateDef);
}
}
Expand All @@ -156,11 +159,6 @@ public void down() throws RelationalException {
deleteDatabases(databases);
}

public void createDatabase(DatabaseTemplate dbTemplate, String dbName) throws RelationalException, SQLException {
EmbeddedRelationalBenchmark.createDatabase(dbTemplate, getUri(dbName, false));
databases.add(dbName);
}

public void createDatabase(URI path, String templateName, String... schemas) throws RelationalException, SQLException {
EmbeddedRelationalBenchmark.createDatabase(path, templateName, schemas);
databases.add(path.getPath());
Expand All @@ -171,15 +169,16 @@ public static class BenchmarkScopedDatabases {
List<String> databases = new ArrayList<>();

public void createMultipleDatabases(
DatabaseTemplate dbTemplate,
String templateName,
int dbCount,
Function<Integer, String> dbName,
Consumer<URI> populateDatabase) throws RelationalException {
Consumer<URI> populateDatabase,
String... schemas) throws RelationalException {
try {
IntStream.range(0, dbCount).parallel().forEach(i ->
{
try {
EmbeddedRelationalBenchmark.createDatabase(dbTemplate, getUri(dbName.apply(i), false));
EmbeddedRelationalBenchmark.createDatabase(getUri(dbName.apply(i), false), templateName, schemas);
populateDatabase.accept(getUri(dbName.apply(i), true));
} catch (RelationalException e) {
throw e.toUncheckedWrappedException();
Expand Down Expand Up @@ -208,18 +207,6 @@ static URI getUri(String dbName, boolean fullyQualified) {
}
}

private static void createDatabase(DatabaseTemplate dbTemplate, URI dbUri) throws RelationalException, SQLException {
try (final var conn = DriverManager.getConnection("jdbc:embed:/__SYS")) {
conn.setSchema("CATALOG");
try (final var statement = conn.createStatement()) {
statement.executeUpdate("CREATE DATABASE \"" + dbUri + "\"");
for (Map.Entry<String, String> schemaTemplateEntry : dbTemplate.getSchemaToTemplateNameMap().entrySet()) {
statement.executeUpdate("CREATE SCHEMA \"" + dbUri + "/" + schemaTemplateEntry.getKey() + "\" WITH TEMPLATE \"" + schemaTemplateEntry.getValue() + "\"");
}
}
}
}

public static void createDatabase(@Nonnull URI dbUri, String templateName, String... schemas) throws RelationalException, SQLException {
try (final var conn = DriverManager.getConnection("jdbc:embed:/__SYS")) {
conn.setSchema("CATALOG");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.apple.foundationdb.relational.api.RelationalConnection;
import com.apple.foundationdb.relational.api.RelationalStatement;
import com.apple.foundationdb.relational.api.RelationalStruct;
import com.apple.foundationdb.relational.api.catalog.DatabaseTemplate;
import com.apple.foundationdb.relational.api.exceptions.RelationalException;
import com.apple.foundationdb.relational.recordlayer.util.ExceptionUtil;
import org.openjdk.jmh.annotations.Benchmark;
Expand Down Expand Up @@ -69,8 +68,8 @@
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@API(API.Status.EXPERIMENTAL)
public class ManyDatabasesBenchmark extends EmbeddedRelationalBenchmark {
final String schema = "schema";
final int dbSize = 1000;
static final String schema = "schema";
static final int dbSize = 1000;

@Param({"1", "10", "100", "1000", "10000"})
int dbCount;
Expand All @@ -84,12 +83,11 @@ public void setUp() throws RelationalException, SQLException {
System.out.printf("Creating %s databases...%n", dbCount);
long startTime = System.nanoTime();
databases.createMultipleDatabases(
DatabaseTemplate.newBuilder()
.withSchema(schema, schemaTemplateName)
.build(),
schemaTemplateName,
dbCount,
this::dbName,
this::populateDatabase);
this::populateDatabase,
schema);
long endTime = System.nanoTime();
System.out.printf("Done in %s %n.", Duration.ofNanos(endTime - startTime));
}
Expand All @@ -116,7 +114,8 @@ public void singleRead(Blackhole bh) throws SQLException {
}

private void populateDatabase(URI uri) {
try (RelationalConnection dbConn = DriverManager.getConnection(uri.toString()).unwrap(RelationalConnection.class)) {
try (java.sql.Connection rawConn = DriverManager.getConnection(uri.toString());
RelationalConnection dbConn = rawConn.unwrap(RelationalConnection.class)) {
dbConn.setSchema(schema);
try (RelationalStatement stmt = dbConn.createStatement()) {
stmt.executeInsert(restaurantRecordTable, createRecords());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public void scan(Blackhole bh, RelationalConnHolder ignored) throws RelationalEx
.setMetaDataProvider(accessor.getProvider(new RecordContextTransaction(ctx)))
.setSerializer(DynamicMessageRecordSerializer.instance())
.setUserVersionChecker((oldUserVersion, oldMetaDataVersion, metaData) -> CompletableFuture.completedFuture(oldMetaDataVersion))
.setFormatVersion(1)
.setContext(ctx)
.open();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class RelationalConnectionBenchmark extends EmbeddedRelationalBenchmark {
@Param(value = {"true", "false"})
boolean setSchemaAtConnTime;

final String schemaName = "testSchema";
static final String schemaName = "testSchema";
URI dbUri = URI.create("/BENCHMARKS/connectionDb");

@Setup(Level.Trial)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class RelationalScanBenchmark extends EmbeddedRelationalBenchmark {

static final URI dbUri = URI.create("/BENCHMARKS/putAndScan");

@Param({"1", "10", "100","1000"})
@Param({"1", "10", "100", "1000"})
int rowCount;

Driver driver = new Driver(templateName, templateDefinition);
Expand All @@ -98,7 +98,8 @@ public void trialUp() throws SQLException, RelationalException {

@Benchmark
public void scan(Blackhole bh, RelationalConnHolder connHolder) throws RelationalException, SQLException {
try (final var stmt = connHolder.connection.createStatement().unwrap(RelationalStatement.class)) {
try (final var rawStmt = connHolder.connection.createStatement();
final var stmt = rawStmt.unwrap(RelationalStatement.class)) {

final Options scanOpts = Options.NONE; //Options.builder().withOption(Options.Name.REQUIRED_METADATA_TABLE_VERSION, -1).build();
List<Map.Entry<String, byte[]>> data = new ArrayList<>();
Expand Down Expand Up @@ -153,7 +154,8 @@ public List<RelationalStruct> data() {

public void insertData(@Nonnull DataSet dataSet) throws SQLException {
//insert about 10 records
try (RelationalConnection conn = DriverManager.getConnection("jdbc:embed:" + dbUri).unwrap(RelationalConnection.class)) {
try (java.sql.Connection rawConn = DriverManager.getConnection("jdbc:embed:" + dbUri);
RelationalConnection conn = rawConn.unwrap(RelationalConnection.class)) {
conn.setSchema(schema);
try (RelationalStatement vs = conn.createStatement()) {
final RelationalStructBuilder builder = EmbeddedRelationalStruct.newBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.apple.foundationdb.relational.api.RelationalConnection;
import com.apple.foundationdb.relational.api.RelationalStatement;
import com.apple.foundationdb.relational.api.RelationalStruct;
import com.apple.foundationdb.relational.api.catalog.DatabaseTemplate;
import com.apple.foundationdb.relational.api.exceptions.RelationalException;
import com.apple.foundationdb.relational.recordlayer.query.cache.RelationalPlanCache;
import com.apple.foundationdb.relational.recordlayer.util.ExceptionUtil;
Expand Down Expand Up @@ -79,7 +78,7 @@ public class SimplePlanCachingBenchmark extends EmbeddedRelationalBenchmark {

static final String cacheSchema = "cacheSchema";

final int dbSize = 5;
static final int dbSize = 5;

@Param({"NONE", "MULTI-STAGED"})
String cacheType;
Expand All @@ -97,12 +96,11 @@ public void trialUp() throws SQLException, RelationalException {
driver.up();

databases.createMultipleDatabases(
DatabaseTemplate.newBuilder()
.withSchema(cacheSchema, schemaTemplateName)
.build(),
schemaTemplateName,
dbCount,
this::dbName,
this::populateDatabase);
this::populateDatabase,
cacheSchema);
}

@TearDown(Level.Trial)
Expand Down Expand Up @@ -141,7 +139,8 @@ private String dbName(long dbId) {
}

private void populateDatabase(URI uri) {
try (RelationalConnection dbConn = DriverManager.getConnection(uri.toString()).unwrap(RelationalConnection.class)) {
try (java.sql.Connection rawConn = DriverManager.getConnection(uri.toString());
RelationalConnection dbConn = rawConn.unwrap(RelationalConnection.class)) {
dbConn.setSchema(cacheSchema);
try (RelationalStatement stmt = dbConn.createStatement()) {
stmt.executeInsert(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* package-info.java
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021-2025 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* JMH microbenchmarks for the RecordLayer-based relational implementation.
*/
package com.apple.foundationdb.relational.recordlayer;
Loading