Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Implement microbenchmark for FileCache #6886

Merged
merged 1 commit into from
Mar 30, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.benchmark.store.remote.filecache;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

import org.apache.lucene.store.IndexInput;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
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.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.opensearch.index.store.remote.filecache.CachedIndexInput;
import org.opensearch.index.store.remote.filecache.FileCache;
import org.opensearch.index.store.remote.filecache.FileCacheFactory;

/**
* Simple benchmark test of {@link FileCache}. It uses a uniform random distribution
* of keys, which is very simple but unlikely to be representative of any real life
* workload.
*/
@Warmup(iterations = 1)
@Measurement(iterations = 1)
@Fork(1)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Threads(8)
@SuppressWarnings("unused") // invoked by benchmarking framework
public class FileCacheBenchmark {
private static final CachedIndexInput INDEX_INPUT = new FixedSizeStubIndexInput();

@Benchmark
public void get(CacheParameters parameters, Blackhole blackhole) {
blackhole.consume(parameters.fileCache.get(randomKeyInCache(parameters)));
}

@Benchmark
public void replace(CacheParameters parameters, Blackhole blackhole) {
blackhole.consume(parameters.fileCache.put(randomKeyInCache(parameters), INDEX_INPUT));
}

@Benchmark
public void put(CacheParameters parameters, Blackhole blackhole) {
blackhole.consume(parameters.fileCache.put(randomKeyNotInCache(parameters), INDEX_INPUT));
}

@Benchmark
public void remove(CacheParameters parameters) {
parameters.fileCache.remove(randomKeyInCache(parameters));
}

private static Path randomKeyInCache(CacheParameters parameters) {
int i = ThreadLocalRandom.current().nextInt(parameters.maximumNumberOfEntries);
return Paths.get(Integer.toString(i));
}

private static Path randomKeyNotInCache(CacheParameters parameters) {
int i = ThreadLocalRandom.current().nextInt(parameters.maximumNumberOfEntries, parameters.maximumNumberOfEntries * 2);
return Paths.get(Integer.toString(i));
}

@State(Scope.Benchmark)
public static class CacheParameters {
@Param({ "65536", "1048576" })
int maximumNumberOfEntries;

@Param({ "1", "8" })
int concurrencyLevel;

FileCache fileCache;

@Setup
public void setup() {
fileCache = FileCacheFactory.createConcurrentLRUFileCache(
(long) maximumNumberOfEntries * INDEX_INPUT.length(),
concurrencyLevel
);
for (long i = 0; i < maximumNumberOfEntries; i++) {
final Path key = Paths.get(Long.toString(i));
fileCache.put(key, INDEX_INPUT);
fileCache.decRef(key);
}
}
}

/**
* Stubbed out IndexInput that does nothing but report a fixed size
*/
private static class FixedSizeStubIndexInput extends CachedIndexInput {
private FixedSizeStubIndexInput() {
super(FixedSizeStubIndexInput.class.getSimpleName());
}

@Override
public boolean isClosed() {
return false;
}

@Override
public void close() {}

@Override
public long getFilePointer() {
throw new UnsupportedOperationException();
}

@Override
public void seek(long pos) {
throw new UnsupportedOperationException();
}

@Override
public long length() {
return 1024 * 1024 * 8; // 8MiB
}

@Override
public IndexInput slice(String sliceDescription, long offset, long length) {
throw new UnsupportedOperationException();
}

@Override
public byte readByte() {
throw new UnsupportedOperationException();
}

@Override
public void readBytes(byte[] b, int offset, int len) {
throw new UnsupportedOperationException();
}
}
}