Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Add benchmark for search by sort #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.cloudant.fdblucene.benchmark;

import java.util.concurrent.TimeUnit;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.GroupThreads;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.Timeout;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Level;
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;

public class BasicSearchBenchmark {

@State(Scope.Benchmark)
public static class FDBSearchBenchmark {

protected FDBSearchSetup fdbSetup;

@BenchmarkMode(Mode.Throughput)
@Fork(1)
@Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.MINUTES)
@Timeout(time = 5, timeUnit = TimeUnit.MINUTES)
@OutputTimeUnit(TimeUnit.SECONDS)
@Benchmark
@Group("searchFDB")
@GroupThreads(1)
public void searchFDB() throws Exception {
int randomSearchPosition = fdbSetup.random.nextInt(fdbSetup.searchTermList.size());
String term = fdbSetup.searchTermList.get(randomSearchPosition);
// we don't actually care about the number of hits
fdbSetup.searcher.search(new TermQuery(new Term("body", term)), fdbSetup.topNDocs);
}

@Setup(Level.Trial)
public void setup() throws Exception {
fdbSetup = new FDBSearchSetup();
fdbSetup.startFDBNetworking();
fdbSetup.createReader();
}
}

@State(Scope.Benchmark)
public static class NioSearchBenchmark {
protected NIOSSearchSetup nioSetup;

@BenchmarkMode(Mode.Throughput)
@Fork(1)
@Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.MINUTES)
@Timeout(time = 5, timeUnit = TimeUnit.MINUTES)
@OutputTimeUnit(TimeUnit.SECONDS)
@Benchmark
@Group("searchNIOS")
@GroupThreads(1)
public void searchNIOS() throws Exception {
int randomSearchPosition = nioSetup.random.nextInt(nioSetup.searchTermList.size());
String term = nioSetup.searchTermList.get(randomSearchPosition);
// we don't actually care about the number of hits
nioSetup.searcher.search(new TermQuery(new Term("body", term)), nioSetup.topNDocs);
}

@Setup(Level.Trial)
public void setup() throws Exception {
nioSetup = new NIOSSearchSetup();
nioSetup.setupNIOS();
nioSetup.createReader();
}
}

public static void main(final String[] args) throws RunnerException {
final Options opt = new OptionsBuilder().include(BasicSearchBenchmark.class.getSimpleName()).build();
new Runner(opt).run();
}

}
12 changes: 12 additions & 0 deletions src/main/java/com/cloudant/fdblucene/benchmark/BenchmarkUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cloudant.fdblucene.benchmark;

import java.util.Random;
import org.apache.lucene.util.BytesRef;

public class BenchmarkUtil {

public enum SearchTypeEnum {
Default, BySort, ByGroup;
}

}
37 changes: 37 additions & 0 deletions src/main/java/com/cloudant/fdblucene/benchmark/FDBSearchSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.cloudant.fdblucene.benchmark;

import java.io.IOException;
import java.nio.file.Path;

import org.apache.lucene.store.Directory;

import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.TearDown;

import com.apple.foundationdb.FDB;
import com.cloudant.fdblucene.FDBDirectory;

public class FDBSearchSetup extends SearchSetup {

@Setup(Level.Trial)
public void startFDBNetworking() throws Exception {
FDB.selectAPIVersion(600);
db = FDB.instance().open();
super.setup();
}

@TearDown(Level.Trial)
public void closeFDB() throws Exception {
super.cleanDirectory();
db.close();
}

@Override
public Directory getDirectory(final Path path) throws IOException {
return FDBDirectory.open(db, path);
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cloudant.fdblucene.benchmark;

import java.io.IOException;

import java.nio.file.Path;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NIOFSDirectory;

import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Setup;


public class NIOSSearchSetup extends SearchSetup {

@Setup(Level.Trial)
public void setupNIOS() throws Exception{
super.setup();
}

@Override
public Directory getDirectory(final Path path) throws IOException {
return new NIOFSDirectory(path);
}

}

135 changes: 135 additions & 0 deletions src/main/java/com/cloudant/fdblucene/benchmark/SearchSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.cloudant.fdblucene.benchmark;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import java.util.List;
import java.util.ArrayList;

import org.apache.lucene.codecs.lucene80.Lucene80Codec;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LineFileDocs;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.util.BytesRef;

import org.openjdk.jmh.annotations.Benchmark;
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;

import com.apple.foundationdb.Database;
import org.openjdk.jmh.runner.FailureAssistException;

@State(Scope.Benchmark)
public abstract class SearchSetup {
public Database db;
public Directory dir;
public Document doc;
public IndexWriter writer;
public DirectoryReader reader;
public IndexSearcher searcher;
public StringField idField;
public AtomicLong counter = new AtomicLong();
public Random random;
public LineFileDocs docs;
public int docsToIndex = 100000;
public List<String> searchTermList = new ArrayList<String>();
public int topNDocs = 50;
public int maxSearchTerms = 1000;
public BenchmarkUtil.SearchTypeEnum searchType = BenchmarkUtil.SearchTypeEnum.Default;

public abstract Directory getDirectory(final Path path) throws IOException;

public void setup() throws Exception {
final IndexWriterConfig config = indexWriterConfig();
dir = getDirectory(generateTestPath());
cleanDirectory();
writer = new IndexWriter(dir, config);
random = new Random();
for (int i = 0; i < docsToIndex; i++) {
docs = new LineFileDocs(random, LuceneTestCase.DEFAULT_LINE_DOCS_FILE);
doc = docs.nextDoc();
// Look through the body's terms, grab a String term, store it
// so that it can be randomly chosen for search later on
String[] body = doc.getValues("body");
String[] terms = null;
if(body.length > 0) {
terms = body[0].split("\\s+");
}
if(terms !=null && searchTermList.size() < maxSearchTerms) {
int randomTermPosition = random.nextInt(terms.length);
searchTermList.add(terms[randomTermPosition]);
}

if (searchType == BenchmarkUtil.SearchTypeEnum.Default) {
idField = new StringField("_id", "", Store.YES);
idField.setStringValue("doc-" + counter.incrementAndGet());
doc.add(idField);
} else if (searchType == BenchmarkUtil.SearchTypeEnum.BySort) {
doc.add(new SortedDocValuesField ("_id",
new BytesRef("doc-" + counter.incrementAndGet())));
} else {
throw new IllegalArgumentException();
}
writer.addDocument(doc);
}
writer.commit();
System.out.println("Commited Indexing");
writer.close();
}

@Setup(Level.Iteration)
public void createReader() throws Exception {
reader = DirectoryReader.open(dir);
searcher = new IndexSearcher(reader);
}

@TearDown(Level.Iteration)
public void teardown() throws Exception {
reader.close();
}

protected void cleanDirectory() throws IOException {
for (final String name : dir.listAll()) {
dir.deleteFile(name);
}
}

protected Path generateTestPath() {
final String dir = System.getProperty("dir");
if (dir == null){
throw new Error("System property 'dir' not set.");
}
final FileSystem fileSystem = FileSystems.getDefault();
return fileSystem.getPath(dir);
}

protected IndexWriterConfig indexWriterConfig() {
final IndexWriterConfig config = new IndexWriterConfig();
config.setUseCompoundFile(false);
config.setCodec(new Lucene80Codec());
return config;
}

protected void setSearchType(BenchmarkUtil.SearchTypeEnum searchType) {
this.searchType = searchType;
}

protected BenchmarkUtil.SearchTypeEnum getSearchType() {
return searchType;
}
}
Loading