All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.druid.benchmark.MergeSequenceBenchmark Maven / Gradle / Ivy

/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets licenses this file
 * to you 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 io.druid.benchmark;

import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import com.metamx.common.guava.Accumulator;
import com.metamx.common.guava.MergeSequence;
import com.metamx.common.guava.Sequence;
import com.metamx.common.guava.Sequences;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
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.infra.Blackhole;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
public class MergeSequenceBenchmark
{

  // Number of Sequences to Merge
  @Param({"1000"})
  int count;

  // Number of elements in each sequence
  @Param({"1000", "10000"})
  int sequenceLength;

  // Number of sequences to merge at once
  @Param({"10", "100"})
  int mergeAtOnce;

  private List> sequences;

  @Setup
  public void setup()
  {
    Random rand = new Random(0);
    sequences = Lists.newArrayList();
    for (int i = 0; i < count; i++) {
      int[] sequence = new int[sequenceLength];
      for (int j = 0; j < sequenceLength; j++) {
        sequence[j] = rand.nextInt();
      }
      sequences.add(Sequences.simple(Ints.asList(sequence)));
    }

  }

  @Benchmark
  @BenchmarkMode(Mode.AverageTime)
  @OutputTimeUnit(TimeUnit.MILLISECONDS)
  public void mergeHierarchical(Blackhole blackhole)
  {
    Iterator> iterator = sequences.iterator();
    List> partialMerged = new ArrayList>();
    List> toMerge = new ArrayList>();

    while (iterator.hasNext()) {
      toMerge.add(iterator.next());
      if (toMerge.size() == mergeAtOnce) {
        partialMerged.add(new MergeSequence(Ordering.natural(), Sequences.simple(toMerge)));
        toMerge = new ArrayList>();
      }
    }

    if (!toMerge.isEmpty()) {
      partialMerged.add(new MergeSequence(Ordering.natural(), Sequences.simple(toMerge)));
    }
    MergeSequence mergeSequence = new MergeSequence(
        Ordering.natural(),
        Sequences.simple(partialMerged)
    );
    Integer accumulate = mergeSequence.accumulate(
        0, new Accumulator()
        {
          @Override
          public Integer accumulate(Integer accumulated, Integer in)
          {
            return accumulated + in;
          }
        }
    );
    blackhole.consume(accumulate);


  }

  @Benchmark
  @BenchmarkMode(Mode.AverageTime)
  @OutputTimeUnit(TimeUnit.MILLISECONDS)
  public void mergeFlat(final Blackhole blackhole)
  {
    MergeSequence mergeSequence = new MergeSequence(Ordering.natural(), Sequences.simple(sequences));
    Integer accumulate = mergeSequence.accumulate(
        0, new Accumulator()
        {
          @Override
          public Integer accumulate(Integer accumulated, Integer in)
          {
            return accumulated + in;
          }
        }
    );
    blackhole.consume(accumulate);

  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy