ThemeQueryBenchmark.java

/*******************************************************************************
 * Copyright (c) 2025 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
 *******************************************************************************/
// Some portions generated by Codex
package org.eclipse.rdf4j.sail.memory.benchmark;

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

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.eclipse.rdf4j.benchmark.common.ThemeQueryCatalog;
import org.eclipse.rdf4j.benchmark.rio.util.ThemeDataSetGenerator;
import org.eclipse.rdf4j.benchmark.rio.util.ThemeDataSetGenerator.Theme;
import org.eclipse.rdf4j.common.transaction.IsolationLevels;
import org.eclipse.rdf4j.query.explanation.Explanation;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.repository.util.RDFInserter;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
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.Param;
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.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Benchmark)
@Warmup(iterations = 2, batchSize = 1, timeUnit = TimeUnit.SECONDS, time = 3)
@BenchmarkMode({ Mode.AverageTime })
@Fork(value = 1, jvmArgs = { "-Xms32G", "-Xmx32G" })
@Measurement(iterations = 2, batchSize = 1, timeUnit = TimeUnit.MILLISECONDS, time = 100)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class ThemeQueryBenchmark {

	@Param({ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" })
	public int z_queryIndex;

	@Param({
			"MEDICAL_RECORDS",
			"SOCIAL_MEDIA",
			"LIBRARY",
			"ENGINEERING",
			"HIGHLY_CONNECTED",
			"TRAIN",
			"ELECTRICAL_GRID",
			"PHARMA"
	})
	public String themeName;

	private SailRepository repository;
	private Theme theme;
	private String query;
	private long expected;

	public static void main(String[] args) throws RunnerException {
		Options opt = new OptionsBuilder()
				.include("ThemeQueryBenchmark")
				.forks(1)
				.build();
		new Runner(opt).run();
	}

	@Setup(Level.Trial)
	public void setup() throws IOException {
		theme = Theme.valueOf(themeName);
		query = ThemeQueryCatalog.queryFor(theme, z_queryIndex);
		expected = ThemeQueryCatalog.expectedCountFor(theme, z_queryIndex);
		repository = new SailRepository(new MemoryStore());
		loadData();
	}

	private void loadData() throws IOException {
		try (SailRepositoryConnection connection = repository.getConnection()) {
			connection.begin(IsolationLevels.NONE);
			RDFInserter inserter = new RDFInserter(connection);
			ThemeDataSetGenerator.generate(theme, inserter);
			connection.commit();
		}
	}

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

	@Benchmark
	public long executeQuery() {
		try (SailRepositoryConnection connection = repository.getConnection()) {
			long count = connection
					.prepareTupleQuery(query)
					.evaluate()
					.stream()
					.count();

			if (count != expected) {
				throw new IllegalStateException("Unexpected count: expected " + expected + " but got " + count);
			}

			return count;
		}
	}

	@Test
	@Disabled
	public void testQueryCounts() throws IOException {
		String[] queryIndexes = paramValues("z_queryIndex");
		String[] themeNames = paramValues("themeName");
		for (String themeNameValue : themeNames) {
			for (String queryIndexValue : queryIndexes) {
				themeName = themeNameValue;
				z_queryIndex = Integer.parseInt(queryIndexValue);
				setup();
				try {
					long actual = executeQuery();
					long expected = ThemeQueryCatalog.expectedCountFor(theme, z_queryIndex);
					System.out.println("For theme " + themeName + " and query index " + z_queryIndex
							+ ", expected count is " + expected + " and actual count is " + actual);
					assertEquals(expected, actual,
							"Unexpected count for theme " + themeName + " and query index " + z_queryIndex);
				} finally {
					tearDown();
				}
			}
		}
	}

	@Test
	public void testQueryExplanation() throws IOException {
		String[] queryIndexes = paramValues("z_queryIndex");
		String[] themeNames = paramValues("themeName");
		for (String themeNameValue : themeNames) {
			for (String queryIndexValue : queryIndexes) {
				themeName = themeNameValue;
				z_queryIndex = Integer.parseInt(queryIndexValue);
				setup();
				try (SailRepositoryConnection connection = repository.getConnection()) {
					String explanation = connection
							.prepareTupleQuery(query)
							.explain(Explanation.Level.Executed)
							.toString();
					System.out.println("Query Explanation for theme " + themeName + " and query index " + z_queryIndex
							+ ":\n" + explanation);
				} finally {
					tearDown();
				}
			}
		}
	}

	private static String[] paramValues(String fieldName) {
		try {
			Param param = ThemeQueryBenchmark.class.getField(fieldName).getAnnotation(Param.class);
			if (param == null) {
				throw new IllegalStateException("Missing @Param annotation for field " + fieldName);
			}
			return param.value();
		} catch (NoSuchFieldException e) {
			throw new IllegalStateException("Missing field " + fieldName, e);
		}
	}
}