Skip to content

Instantly share code, notes, and snippets.

@n-dobryukha
Created April 28, 2016 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n-dobryukha/d7f65f0a9aa01d30f08b95bdd294f717 to your computer and use it in GitHub Desktop.
Save n-dobryukha/d7f65f0a9aa01d30f08b95bdd294f717 to your computer and use it in GitHub Desktop.
package com.ndobryukha.tests.infinispan.entity;
/**
* @Indexed(true)
* @TypeId(42)
*/
message Book {
/**
* @IndexedField(index=true)
*/
required string title = 1;
required string description = 2;
required int32 publishYear = 3;
}
package com.ndobryukha.tests.infinispan.benchmark;
import com.ndobryukha.tests.infinispan.entity.Book;
import com.ndobryukha.tests.infinispan.marshaller.BookMarshaller;
import com.ndobryukha.tests.infinispan.marshaller.UserMarshaller;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.Search;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.marshall.ProtoStreamMarshaller;
import org.infinispan.commons.util.Util;
import org.infinispan.protostream.FileDescriptorSource;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.query.dsl.Query;
import org.infinispan.query.dsl.QueryFactory;
import org.infinispan.query.remote.client.MarshallerRegistration;
import org.infinispan.query.remote.client.ProtobufMetadataManagerConstants;
import org.openjdk.jmh.annotations.*;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.UUID;
@State(Scope.Benchmark)
@Measurement(iterations = 100)
@Fork(value = 1)
@BenchmarkMode(Mode.SingleShotTime)
public class MyBenchmark {
private static final String BOOKS_CACHE_NAME = "booksCache";
private static final RemoteCacheManager remoteCacheManager = createRemoteCacheManager();
private static final Random random = new Random();
private static final int MAX_CACHE_COUNT = 500_000;
private static RemoteCacheManager createRemoteCacheManager() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer()
.host("10.11.13.220")
.port(11222)
.marshaller(new ProtoStreamMarshaller());
return new RemoteCacheManager(builder.build());
}
@Setup
public static void configureRemoteCacheManager() throws IOException {
RemoteCache<String, String> metadataCache = remoteCacheManager.getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);
metadataCache.put("book.proto", Util.read(MyBenchmark.class.getResourceAsStream("/book.proto")));
metadataCache.put("user.proto", Util.read(MyBenchmark.class.getResourceAsStream("/user.proto")));
SerializationContext srzCtx = ProtoStreamMarshaller.getSerializationContext(remoteCacheManager);
FileDescriptorSource fds = new FileDescriptorSource();
fds.addProtoFiles("/book.proto");
fds.addProtoFiles("/user.proto");
srzCtx.registerProtoFiles(fds);
srzCtx.registerMarshaller(new BookMarshaller());
srzCtx.registerMarshaller(new UserMarshaller());
MarshallerRegistration.registerMarshallers(srzCtx);
populateCache();
}
@TearDown
public static void cleanCache() {
remoteCacheManager.getCache(BOOKS_CACHE_NAME).clear();
}
private static void populateCache() {
RemoteCache<Long, Book> cache = remoteCacheManager.getCache(BOOKS_CACHE_NAME);
for (int i = 0; i < MAX_CACHE_COUNT; i++) {
cache.put(UUID.randomUUID().getMostSignificantBits(), new Book("title" + i, "description" + i, 2000));
}
}
@Benchmark
public void testSearch() {
RemoteCache<Long, Book> cache = remoteCacheManager.getCache(BOOKS_CACHE_NAME);
QueryFactory qf = Search.getQueryFactory(cache);
Query query = qf.from(Book.class).having("title").eq("title" + random.nextInt(MAX_CACHE_COUNT)).toBuilder().build();
List<Book> list = query.list();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment