diff --git a/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java b/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java index e1f5c76c18d..85eb6734b5e 100644 --- a/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java +++ b/solr/src/test/org/apache/solr/spelling/suggest/SuggesterTest.java @@ -27,9 +27,13 @@ import org.apache.solr.util.TermFreqIterator; import org.junit.BeforeClass; import org.junit.Test; +import com.google.common.collect.Lists; + import java.io.File; +import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Random; @@ -130,10 +134,53 @@ public class SuggesterTest extends SolrTestCaseJ4 { return tfit; } - private void _benchmark(Lookup lookup, Map ref, boolean estimate, Bench bench) throws Exception { + static class Bench { + long buildTime; + long lookupTime; + } + + @Test + public void testBenchmark() throws Exception { + // this benchmark is very time consuming + boolean doTest = true; + if (!doTest) { + return; + } + + final List> benchmarkClasses = Lists.newArrayList(); + benchmarkClasses.add(JaspellLookup.class); + benchmarkClasses.add(TSTLookup.class); + + // Run a single pass just to see if everything works fine and provide size estimates. + final RamUsageEstimator rue = new RamUsageEstimator(); + for (Class cls : benchmarkClasses) { + Lookup lookup = singleBenchmark(cls, null); + System.err.println( + String.format(Locale.ENGLISH, + "%20s, size[B]=%,d", + lookup.getClass().getSimpleName(), + rue.estimateRamUsage(lookup))); + } + + int warmupCount = 10; + int measuredCount = 100; + for (Class cls : benchmarkClasses) { + Bench b = fullBenchmark(cls, warmupCount, measuredCount); + System.err.println(String.format(Locale.ENGLISH, + "%s: buildTime[ms]=%,d lookupTime[ms]=%,d", + cls.getSimpleName(), + (b.buildTime / measuredCount), + (b.lookupTime / measuredCount / 1000000))); + } + } + + private Lookup singleBenchmark(Class cls, Bench bench) throws Exception { + Lookup lookup = cls.newInstance(); + long start = System.currentTimeMillis(); lookup.build(getTFIT()); long buildTime = System.currentTimeMillis() - start; + TermFreqIterator tfit = getTFIT(); long elapsed = 0; while (tfit.hasNext()) { @@ -148,78 +195,37 @@ public class SuggesterTest extends SolrTestCaseJ4 { for (LookupResult lr : res) { assertTrue(lr.key.startsWith(prefix)); } - if (ref != null) { // verify the counts - Integer Cnt = ref.get(key); - if (Cnt == null) { // first pass - ref.put(key, res.size()); - } else { - assertEquals(key + ", prefix: " + prefix, Cnt.intValue(), res.size()); - } - } - } - if (estimate) { - RamUsageEstimator rue = new RamUsageEstimator(); - long size = rue.estimateRamUsage(lookup); - System.err.println(lookup.getClass().getSimpleName() + " - size=" + size); } + if (bench != null) { bench.buildTime += buildTime; bench.lookupTime += elapsed; } - } - - class Bench { - long buildTime; - long lookupTime; + + return lookup; } - @Test - public void testBenchmark() throws Exception { - // this benchmark is very time consuming - boolean doTest = false; - if (!doTest) { - return; - } - Map ref = new HashMap(); - JaspellLookup jaspell = new JaspellLookup(); - TSTLookup tst = new TSTLookup(); - - _benchmark(tst, ref, true, null); - _benchmark(jaspell, ref, true, null); - jaspell = null; - tst = null; - int count = 100; - Bench b = runBenchmark(JaspellLookup.class, count); - System.err.println(JaspellLookup.class.getSimpleName() + ": buildTime[ms]=" + (b.buildTime / count) + - " lookupTime[ms]=" + (b.lookupTime / count / 1000000)); - b = runBenchmark(TSTLookup.class, count); - System.err.println(TSTLookup.class.getSimpleName() + ": buildTime[ms]=" + (b.buildTime / count) + - " lookupTime[ms]=" + (b.lookupTime / count / 1000000)); - } - - private Bench runBenchmark(Class cls, int count) throws Exception { - System.err.println("* Running " + count + " iterations for " + cls.getSimpleName() + " ..."); - System.err.println(" - warm-up 10 iterations..."); - for (int i = 0; i < 10; i++) { + private Bench fullBenchmark(Class cls, int warmupCount, int measuredCount) throws Exception { + System.err.println("* Running " + measuredCount + " iterations for " + cls.getSimpleName() + " ..."); + System.err.println(" - warm-up " + warmupCount + " iterations..."); + for (int i = 0; i < warmupCount; i++) { System.runFinalization(); System.gc(); - Lookup lookup = cls.newInstance(); - _benchmark(lookup, null, false, null); - lookup = null; + singleBenchmark(cls, null); } + Bench b = new Bench(); System.err.print(" - main iterations:"); System.err.flush(); - for (int i = 0; i < count; i++) { + for (int i = 0; i < measuredCount; i++) { System.runFinalization(); System.gc(); - Lookup lookup = cls.newInstance(); - _benchmark(lookup, null, false, b); - lookup = null; + singleBenchmark(cls, b); if (i > 0 && (i % 10 == 0)) { System.err.print(" " + i); System.err.flush(); } } + System.err.println(); return b; }