/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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 org.apache.druid.benchmark;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Benchmark to compare the performance of two implementations of CustomTierSelectorStrategy comparator:
 * - Old: Uses containsKey() + get() (2 map lookups)
 * - New: Uses get() + null check (1 map lookup)
 *
 * Run with: java -jar benchmarks/target/benchmarks.jar CustomTierSelectorComparatorBenchmark
 */
@State(Scope.Benchmark)
@Fork(value = 1)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class CustomTierSelectorComparatorBenchmark
{
  private Comparator<Integer> oldComparator;
  private Comparator<Integer> newComparator;
  private Map<Integer, Integer> lookup;
  private Integer[] priorities;
  private Random random;

  @Setup
  public void setup()
  {
    // Create a lookup map with 10 priorities
    lookup = new HashMap<>();
    for (int i = 0; i < 10; i++) {
      lookup.put(i * 10, i); // Priorities: 0, 10, 20, ..., 90
    }

    // Create test data with mix of in-map and out-of-map values
    priorities = new Integer[100];
    random = new Random(12345);
    for (int i = 0; i < priorities.length; i++) {
      // 50% chance of being in the map, 50% chance of being outside
      if (random.nextBoolean()) {
        priorities[i] = (random.nextInt(10)) * 10; // In map: 0, 10, 20, ..., 90
      } else {
        priorities[i] = (random.nextInt(10)) * 10 + 5; // Not in map: 5, 15, 25, ..., 95
      }
    }

    // Old implementation: containsKey + get (2 map lookups)
    oldComparator = (p1, p2) -> {
      if (lookup.containsKey(p1) && lookup.containsKey(p2)) {
        return Integer.compare(lookup.get(p1), lookup.get(p2));
      } else if (lookup.containsKey(p1)) {
        return -1;
      } else if (lookup.containsKey(p2)) {
        return 1;
      } else {
        return Integer.compare(p2, p1);
      }
    };

    // New implementation: get + null check (1 map lookup)
    newComparator = (p1, p2) -> {
      final Integer rank1 = lookup.get(p1);
      final Integer rank2 = lookup.get(p2);

      if (rank1 != null && rank2 != null) {
        return Integer.compare(rank1, rank2);
      }
      if (rank1 != null) {
        return -1;
      }
      if (rank2 != null) {
        return 1;
      }
      return Integer.compare(p2, p1);
    };
  }

  @Benchmark
  public void oldImplementation(Blackhole blackhole)
  {
    for (int i = 0; i < priorities.length - 1; i++) {
      int result = oldComparator.compare(priorities[i], priorities[i + 1]);
      blackhole.consume(result);
    }
  }

  @Benchmark
  public void newImplementation(Blackhole blackhole)
  {
    for (int i = 0; i < priorities.length - 1; i++) {
      int result = newComparator.compare(priorities[i], priorities[i + 1]);
      blackhole.consume(result);
    }
  }

  public static void main(String[] args) throws RunnerException
  {
    Options opt = new OptionsBuilder()
        .include(CustomTierSelectorComparatorBenchmark.class.getSimpleName())
        .forks(1)
        .build();
    new Runner(opt).run();
  }
}

