/*
 * 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.uima.fit.benchmark;

import static org.apache.uima.fit.benchmark.CasInitializationUtils.initRandomCas;
import static org.apache.uima.fit.util.JCasUtil.indexCovered;
import static org.apache.uima.fit.util.JCasUtil.select;
import static org.apache.uima.fit.util.JCasUtil.selectAll;
import static org.apache.uima.fit.util.JCasUtil.selectCovered;

import org.apache.uima.fit.factory.JCasFactory;
import org.apache.uima.fit.type.Sentence;
import org.apache.uima.fit.type.Token;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.TOP;
import org.junit.Before;
import org.junit.Test;

public class JCasUtilBenchmark {
  private JCas jcas;
  
  @Before
  public void setup() throws Exception {
    if (jcas == null) {
      jcas = JCasFactory.createJCas();
    }
    else {
      jcas.reset();
    }
  }

  @Test
  public void benchmarkSelect() {
    Benchmark template = new Benchmark("TEMPLATE")
      .initialize(n -> initRandomCas(jcas.getCas(), n))
      .magnitude(10)
      .magnitudeIncrement(count -> count * 10)
      .incrementTimes(5);
    
    new Benchmark("JCas select Token", template)
      .measure(() -> select(jcas, Token.class))
      .run();

    new Benchmark("JCas select Token v3", template)
      .measure(() -> jcas.select(Token.class))
      .run();

    new Benchmark("JCas select Token and iterate", template)
      .measure(() -> select(jcas, Token.class).forEach(v -> {}))
      .run();

    new Benchmark("JCas select Token and iterate v3", template)
      .measure(() -> jcas.select(Token.class).forEach(v -> {}))
      .run();

    new Benchmark("JCas select Sentence", template)
      .measure(() -> select(jcas, Sentence.class))
      .run();

    new Benchmark("JCas select Sentence and iterate", template)
      .measure(() -> select(jcas, Sentence.class).forEach(v -> {}))
      .run();
    
    new Benchmark("JCas select TOP", template)
      .measure(() -> select(jcas, TOP.class))
      .run();

    new Benchmark("JCas select TOP and iterate", template)
      .measure(() -> select(jcas, TOP.class).forEach(v -> {}))
      .run();
    
    new Benchmark("JCas select TOP and iterate v3", template)
      .measure(() -> jcas.select(TOP.class).forEach(v -> {}))
      .run();

    new Benchmark("JCas select ALL", template)
      .measure(() -> selectAll(jcas))
      .run();
    
    new Benchmark("JCas select ALL and iterate", template)
      .measure(() -> selectAll(jcas).forEach(v -> {}))
      .run();
    
    new Benchmark("JCas select ALL and iterate v3", template)
      .measure(() -> jcas.select().forEach(v -> {}))
      .run();
  }
  
  @Test
  public void benchmarkSelectCovered() {
    Benchmark template = new Benchmark("TEMPLATE")
        .initialize(n -> initRandomCas(jcas.getCas(), n))
        .magnitude(10)
        .magnitudeIncrement(count -> count * 10)
        .incrementTimes(4);
    
    new Benchmark("JCas selectCovered", template)
      .measure(() -> select(jcas, Sentence.class).forEach(s -> selectCovered(Token.class, s)))
      .run();

    new Benchmark("JCas selectCovered v3", template)
      .measure(() -> {
          jcas.select(Sentence.class).forEach(s -> jcas.select(Token.class).coveredBy(s).forEach(t -> {}));
      })
    .run();

    new Benchmark("JCas indexCovered", template)
      .measure(() -> indexCovered(jcas, Sentence.class, Token.class))
      .run();
  }
}

@@ -987,16 +987,79 @@
 overed;%0A
+import static org.apache.uima.fit.util.JCasUtil.indexCovering;%0A
 import s
@@ -1227,16 +1227,80 @@
 Covered;
+%0Aimport static org.apache.uima.fit.util.JCasUtil.selectCovering;
 %0A%0Aimport
@@ -3651,26 +3651,24 @@
 ATE%22)%0A      
-  
 .initialize(
@@ -3701,26 +3701,24 @@
 tCas(), n))%0A
-  
       .magni
@@ -3728,26 +3728,24 @@
 e(10)%0A      
-  
 .magnitudeIn
@@ -3769,26 +3769,24 @@
 count * 10)%0A
-  
       .incre
@@ -4211,32 +4211,32 @@
 red%22, template)%0A
-
       .measure((
@@ -4307,10 +4307,570 @@
 ();%0A  %7D%0A
+  %0A  @Test%0A  public void benchmarkSelectCovering() %7B%0A    Benchmark template = new Benchmark(%22TEMPLATE%22)%0A      .initialize(n -%3E initRandomCas(jcas.getCas(), n))%0A      .magnitude(10)%0A      .magnitudeIncrement(count -%3E count * 10)%0A      .incrementTimes(3);%0A    %0A    new Benchmark(%22JCas selectCovering%22, template)%0A      .measure(() -%3E select(jcas, Token.class).forEach(s -%3E selectCovering(Sentence.class, s)))%0A      .run();%0A%0A    new Benchmark(%22JCas indexCovering%22, template)%0A      .measure(() -%3E indexCovering(jcas, Token.class, Sentence.class))%0A      .run();%0A  %7D%0A
 %7D%0A

Add basic benchmarking module