Skip to content
Closed
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,161 @@
/**
* Copyright (c) 2022 PhonePe India Pvt. Ltd.
*
* 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.
*/
package com.phonepe.mustang.benchmark.benchmarks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

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.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Sets;
import com.phonepe.mustang.MustangEngine;
import com.phonepe.mustang.benchmark.utils.Utils;
import com.phonepe.mustang.common.RequestContext;
import com.phonepe.mustang.composition.impl.Conjunction;
import com.phonepe.mustang.criteria.Criteria;
import com.phonepe.mustang.criteria.impl.DNFCriteria;
import com.phonepe.mustang.predicate.impl.IncludedPredicate;

import lombok.Getter;

/**
* Search latency for indexes dominated by EQUALITY keys, with and without the equality key lookup.
*
* <ul>
* <li>{@code DISTINCT_VALUES}: every criteria matches one distinct value on a single path (e.g. one geo-fence id per
* offer). The number of distinct keys grows with the index size.</li>
* <li>{@code VALUE_LISTS}: every criteria lists 50 values from a large pool on one path, plus one of 5 values on a
* second path (e.g. allowed pricing plans per offer, per tenant).</li>
* </ul>
*/
public class MustangEqualitySearchBenchmark {

private static final int REQUEST_POOL_SIZE = 1024;

@Getter
@State(Scope.Benchmark)
public static class BenchmarkContext {
@Param({ "1000", "10000" })
private int indexSize;

@Param({ "DISTINCT_VALUES", "VALUE_LISTS" })
private String shape;

@Param({ "true", "false" })
private boolean equalityKeyLookupEnabled;

private final ObjectMapper mapper = new ObjectMapper();
private MustangEngine engine;
private RequestContext[] requests;

@Setup(Level.Trial)
public void setUp() {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
engine = MustangEngine.builder()
.mapper(mapper)
.equalityKeyLookupEnabled(equalityKeyLookupEnabled)
.build();
final Random random = new Random(7);
final List<Criteria> criterias = new ArrayList<>(indexSize);
final int valuePool = indexSize * 10;
for (int i = 0; i < indexSize; i++) {
final Conjunction.ConjunctionBuilder conjunction = Conjunction.builder();
if ("DISTINCT_VALUES".equals(shape)) {
conjunction.predicate(IncludedPredicate.builder()
.lhs("$.fenceId")
.values(Sets.newHashSet("FE" + i))
.build());
} else {
final Set<Object> plans = new HashSet<>();
while (plans.size() < 50) {
plans.add("PLAN" + random.nextInt(valuePool));
}
conjunction.predicate(IncludedPredicate.builder()
.lhs("$.pricingPlanId")
.values(plans)
.build())
.predicate(IncludedPredicate.builder()
.lhs("$.tenant")
.values(Sets.newHashSet("T" + (i % 5)))
.build());
}
criterias.add(DNFCriteria.builder()
.id("C" + i)
.conjunction(conjunction.build())
.build());
}
engine.add(Utils.INDEX_NAME, criterias);

requests = new RequestContext[REQUEST_POOL_SIZE];
for (int r = 0; r < REQUEST_POOL_SIZE; r++) {
final Map<String, Object> request = new HashMap<>();
if ("DISTINCT_VALUES".equals(shape)) {
request.put("fenceId", "FE" + random.nextInt(indexSize * 2)); // ~50% hit rate
} else {
request.put("pricingPlanId", "PLAN" + random.nextInt(valuePool));
request.put("tenant", "T" + random.nextInt(5));
}
requests[r] = RequestContext.builder()
.node(mapper.valueToTree(request))
.build();
}
}
}

@State(Scope.Thread)
public static class Cursor {
private int next;

int next() {
next = (next + 1) & (REQUEST_POOL_SIZE - 1);
return next;
}
}

@Benchmark
@Fork(value = 1, warmups = 0)
@Warmup(iterations = 2, time = 5)
@Measurement(iterations = 3, time = 5)
@Threads(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void search(final Blackhole blackhole, final BenchmarkContext context, final Cursor cursor) {
blackhole.consume(context.getEngine()
.search(Utils.INDEX_NAME, context.getRequests()[cursor.next()], false));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public static class BenchmarkContext {
@Param({ "10", "100", "1000", "10000" })
private int indexSize;

@Param({ "true", "false" })
private boolean equalityKeyLookupEnabled;

private final ObjectMapper mapper = new ObjectMapper();
private MustangEngine engine;
private RequestContext requestContext;
Expand All @@ -66,6 +69,7 @@ public void setUp() {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
engine = MustangEngine.builder()
.mapper(mapper)
.equalityKeyLookupEnabled(equalityKeyLookupEnabled)
.build();

for (int i = 0; i < indexSize; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public static class BenchmarkContext {
@Param({ "10", "100", "1000", "10000" })
private int indexSize;

@Param({ "true", "false" })
private boolean equalityKeyLookupEnabled;

private final ObjectMapper mapper = new ObjectMapper();
private MustangEngine engine;
private RequestContext requestContext;
Expand All @@ -66,6 +69,7 @@ public void setUp() {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
engine = MustangEngine.builder()
.mapper(mapper)
.equalityKeyLookupEnabled(equalityKeyLookupEnabled)
.build();

for (int i = 0; i < indexSize; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static class BenchmarkContext {
@Param({ "10", "100", "1000", "10000" })
private int indexSize;

@Param({ "true", "false" })
private boolean equalityKeyLookupEnabled;

private final ObjectMapper mapper = new ObjectMapper();
private MustangEngine engine;
private RequestContext requestContext;
Expand All @@ -68,6 +71,7 @@ public void setUp() {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
engine = MustangEngine.builder()
.mapper(mapper)
.equalityKeyLookupEnabled(equalityKeyLookupEnabled)
.build();

for (int i = 0; i < indexSize; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public class MustangEngine {
private ObjectMapper mapper;
@Builder.Default
private RankingStrategy rankingStrategy = RankingStrategy.EXPLICIT_WEIGHTS;
/**
* Resolve EQUALITY keys during search via a hash lookup instead of evaluating every key. Search results are
* identical either way; this only changes how matching keys are found. Enabled by default.
*/
@Builder.Default
private boolean equalityKeyLookupEnabled = true;
private final IndexingFacade indexingFacde = IndexingFacade.builder()
.build();
private final SearchFacade searchFacade = SearchFacade.builder()
Expand Down Expand Up @@ -107,12 +113,12 @@ public Set<String> search(final String indexName, final RequestContext context)

public Set<String> search(final String indexName, final RequestContext context, final int topN) {
final Query query = QueryBuilder.buildQuery(context);
return searchFacade.search(indexName, query, topN, true);
return searchFacade.search(indexName, query, topN, true, equalityKeyLookupEnabled);
}

public Set<String> search(final String indexName, final RequestContext context, final boolean score) {
final Query query = QueryBuilder.buildQuery(context);
return searchFacade.search(indexName, query, -1, score);
return searchFacade.search(indexName, query, -1, score, equalityKeyLookupEnabled);
}

public Set<String> scan(final String indexName, final RequestContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ public class SearchFacade {
private final IndexingFacade indexingFacade;

public Set<String> search(final String indexName, final Query query, final int topN, final boolean score) {
return search(indexName, query, topN, score, true);
}

public Set<String> search(final String indexName,
final Query query,
final int topN,
final boolean score,
final boolean equalityKeyLookupEnabled) {
final Map<String, Double> result = CriteriaSearchHandler.builder()
.indexGroup(indexingFacade.getIndexGroup(indexName))
.query(query)
.score(score)
.equalityKeyLookupEnabled(equalityKeyLookupEnabled)
.build()
.handle();
if (!score) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class CriteriaSearchHandler implements CriteriaForm.Visitor<Matches> {
@NotNull
private final Query query;
private final boolean score;
@Builder.Default
private final boolean equalityKeyLookupEnabled = true;
private final Map<String, Object> pathValues = Maps.newHashMap();

public Map<String, Double> handle() {
Expand Down Expand Up @@ -73,6 +75,7 @@ public Matches visitDNF() {
.allCriterias(indexGroup.getAllCriterias())
.pathValues(pathValues)
.score(score)
.equalityKeyLookupEnabled(equalityKeyLookupEnabled)
.build()
.getMatches())
.build();
Expand All @@ -87,6 +90,7 @@ public Matches visitCNF() {
.allCriterias(indexGroup.getAllCriterias())
.pathValues(pathValues)
.score(score)
.equalityKeyLookupEnabled(equalityKeyLookupEnabled)
.build()
.getMatches())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import com.phonepe.mustang.index.core.Key;
import com.phonepe.mustang.index.core.impl.CNFInvertedIndex;
import com.phonepe.mustang.predicate.PredicateType;
import com.phonepe.mustang.preoperation.ChainOperator;
import com.phonepe.mustang.search.Query;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
Expand All @@ -35,7 +35,6 @@
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -60,6 +59,8 @@ public class CNFMatcher {
private final Map<String, Criteria> allCriterias;
private final Map<String, Object> pathValues;
private final boolean score;
@Builder.Default
private final boolean equalityKeyLookupEnabled = true;

private static DisjunctionPostingEntry getDisjunctionPostingEntry(
final TreeMap<Integer, DisjunctionPostingEntry> map,
Expand Down Expand Up @@ -164,17 +165,14 @@ private Map.Entry<Key, MutablePair<Integer, TreeMap<Integer, DisjunctionPostingE
final Map<Integer, Map<Key, TreeMap<Integer, DisjunctionPostingEntry>>> table,
final int k) {
final Map<Key, TreeMap<Integer, DisjunctionPostingEntry>> map = table.getOrDefault(k, Collections.emptyMap());
return getMatchingKeys(map).collect(Collectors.toMap(x -> x, x -> MutablePair.of(0, map.get(x))))
return getMatchingKeys(k, map).stream()
.collect(Collectors.toMap(x -> x, x -> MutablePair.of(0, map.get(x))))
.entrySet()
.toArray(Entry[]::new);
}

private Stream<Key> getMatchingKeys(final Map<Key, TreeMap<Integer, DisjunctionPostingEntry>> map) {
return map.keySet()
.stream()
.filter(key -> key.getCaveat()
.visit(new CaveatEnforcer(key,
ChainOperator.operate(key.getPreOps(), pathValues.get(key.getName())))));
private List<Key> getMatchingKeys(final int k, final Map<Key, TreeMap<Integer, DisjunctionPostingEntry>> map) {
return MatchingKeyResolver.resolve(invertedIndex, k, map, pathValues, equalityKeyLookupEnabled);
}

private void initializeCurrentEntriesCNF(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import com.phonepe.mustang.index.core.Key;
import com.phonepe.mustang.index.core.impl.DNFInvertedIndex;
import com.phonepe.mustang.predicate.PredicateType;
import com.phonepe.mustang.preoperation.ChainOperator;
import com.phonepe.mustang.search.Query;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
Expand All @@ -35,7 +35,6 @@
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -55,6 +54,8 @@ public class DNFMatcher {
private final Map<String, Criteria> allCriterias;
private final Map<String, Object> pathValues;
private final boolean score;
@Builder.Default
private final boolean equalityKeyLookupEnabled = true;

private static ConjunctionPostingEntry getConjunctionPostingEntry(
final TreeMap<Integer, ConjunctionPostingEntry> map,
Expand Down Expand Up @@ -138,17 +139,14 @@ private Map.Entry<Key, MutablePair<Integer, TreeMap<Integer, ConjunctionPostingE
final Map<Integer, Map<Key, TreeMap<Integer, ConjunctionPostingEntry>>> table,
final int k) {
final Map<Key, TreeMap<Integer, ConjunctionPostingEntry>> map = table.getOrDefault(k, Collections.emptyMap());
return getMatchingKeys(map).collect(Collectors.toMap(x -> x, x -> MutablePair.of(0, map.get(x))))
return getMatchingKeys(k, map).stream()
.collect(Collectors.toMap(x -> x, x -> MutablePair.of(0, map.get(x))))
.entrySet()
.toArray(Entry[]::new);
}

private Stream<Key> getMatchingKeys(final Map<Key, TreeMap<Integer, ConjunctionPostingEntry>> map) {
return map.keySet()
.stream()
.filter(key -> key.getCaveat()
.visit(new CaveatEnforcer(key,
ChainOperator.operate(key.getPreOps(), pathValues.get(key.getName())))));
private List<Key> getMatchingKeys(final int k, final Map<Key, TreeMap<Integer, ConjunctionPostingEntry>> map) {
return MatchingKeyResolver.resolve(invertedIndex, k, map, pathValues, equalityKeyLookupEnabled);
}

private void initializeCurrentEntriesDNF(
Expand Down
Loading
Loading