예제 #1
0
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: test file");
      System.exit(1);
    }
    File f = new File(args[0]);
    // First: read just keys
    System.out.println("Loading keys...");
    KeyEntry[] entries = shuffleEntries(loadKeys(f, KEY_SAMPLING_RATIO));
    // Then build tries
    System.out.println("Building raw trie data...");
    VIntValueReader kr = new VIntValueReader(f);
    SimpleVIntTrieBuilder b = new SimpleVIntTrieBuilder(kr);
    // To re-order or not? Reordering increases speed by ~10%:
    final boolean REORDER = true;
    System.out.println("Reorder entries: " + REORDER);
    b.setReorderEntries(REORDER);
    byte[] rawTrie = b.build().serialize();
    b = null; // just ensure we can GC interemediate stuff
    TrieLookup<Long> arrayBased = new ByteArrayVIntTrieLookup(rawTrie);
    ByteBuffer buffer = ByteBuffer.allocateDirect((int) rawTrie.length);
    System.out.println("ByteBuffer: is-direct? " + buffer.isDirect());
    buffer.put(rawTrie);

    TrieLookup<Long> bufferBased = new ByteBufferVIntTrieLookup(buffer, rawTrie.length);
    VIntSpeedTest test = new VIntSpeedTest(entries);
    for (int i = 0; true; ++i) {
      long start = System.currentTimeMillis();
      TrieLookup<Long> trie;
      switch (i % 2) {
        case 1:
          trie = bufferBased;
          break;
        default:
          trie = arrayBased;
      }
      long result = test.test(trie);
      long time = System.currentTimeMillis() - start;
      System.out.println(
          "Took "
              + time
              + " msecs for "
              + trie.getClass()
              + " (result "
              + Long.toHexString(result)
              + ")");
      Thread.sleep(100L);
    }
  }
예제 #2
0
 public long test(TrieLookup<Long> lookup) {
   long total = 0L;
   for (int i = 0, len = entries.length; i < len; ++i) {
     KeyEntry entry = entries[i];
     Long value = lookup.findValue(entry.rawKey);
     if (value == null || value.longValue() != entry.value) {
       throw new IllegalStateException(
           "Problem with "
               + lookup
               + ", entry #"
               + i
               + ", value "
               + value
               + "; expected "
               + entry.value);
     }
     total += value;
   }
   return total;
 }