/*
 * 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.factory.TypeSystemDescriptionFactory.*;
import static org.apache.uima.fit.benchmark.CasInitializationUtils.initRandomCas;
import static org.apache.uima.fit.util.CasUtil.indexCovered;
import static org.apache.uima.fit.util.CasUtil.select;
import static org.apache.uima.fit.util.CasUtil.selectAll;
import static org.apache.uima.fit.util.CasUtil.*;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Type;
import org.apache.uima.util.CasCreationUtils;
import org.junit.Before;
import org.junit.Test;

public class CasUtilBenchmark {
  private CAS cas;
  
  private static final String TYPE_NAME_TOKEN = "org.apache.uima.fit.type.Token";
  private static final String TYPE_NAME_SENTENCE = "org.apache.uima.fit.type.Sentence";
  
  @Before
  public void setup() throws Exception {
    if (cas == null) {
      cas = CasCreationUtils.createCas(createTypeSystemDescription(), null, null);
    }
    else {
      cas.reset();
    }
  }

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

    new Benchmark("CAS select Token and iterate", template)
      .measure(() -> select(cas, getType(cas, TYPE_NAME_TOKEN)).forEach(v -> {}))
      .run();

    new Benchmark("CAS select Sentence", template)
      .measure(() -> select(cas, getType(cas, TYPE_NAME_SENTENCE)))
      .run();

    new Benchmark("CAS select Sentence and iterate", template)
      .measure(() -> select(cas, getType(cas, TYPE_NAME_SENTENCE)).forEach(v -> {}))
      .run();
    
    new Benchmark("CAS select TOP", template)
      .measure(() -> selectFS(cas, getType(cas, CAS.TYPE_NAME_TOP)))
      .run();

    new Benchmark("CAS select TOP and iterate", template)
      .measure(() -> selectFS(cas, getType(cas, CAS.TYPE_NAME_TOP)).forEach(v -> {}))
      .run();
    
    new Benchmark("CAS select ALL", template)
      .measure(() -> selectAll(cas))
      .run();
    
    new Benchmark("CAS select ALL and iterate", template)
      .measure(() -> selectAll(cas).forEach(v -> {}))
      .run();
  }
  
  @Test
  public void benchmarkSelectCovered() {
    Benchmark template = new Benchmark("TEMPLATE")
        .initialize(n -> initRandomCas(cas, n))
        .magnitude(10)
        .magnitudeIncrement(count -> count * 10)
        .incrementTimes(4);
    
    new Benchmark("CAS selectCovered", template)
      .measure(() -> {
      	Type sentenceType = getType(cas, TYPE_NAME_SENTENCE);
      	Type tokenType = getType(cas, TYPE_NAME_TOKEN);
      	select(cas, sentenceType).forEach(s -> selectCovered(tokenType, s));
      })
      .run();

    new Benchmark("CAS indexCovered", template)
      .measure(() -> indexCovered(cas, getType(cas, TYPE_NAME_SENTENCE), getType(cas, TYPE_NAME_TOKEN)))
      .run();
  }
}

@@ -843,16 +843,98 @@
 hmark;%0A%0A
+import static org.apache.uima.fit.benchmark.CasInitializationUtils.initRandomCas;%0A
 import s
@@ -996,17 +996,43 @@
 Factory.
-*
+createTypeSystemDescription
 ;%0Aimport
@@ -1063,115 +1063,151 @@
 fit.
-benchmark.CasInitialization
+util.CasUtil.getType;%0Aimport static org.apache.uima.fit.util.CasUtil.indexCovered;%0Aimport static org.apache.uima.fit.util.Cas
 Util
-s
 .in
-itRandomCas;%0Aimport static org.apache.uima.fit.util.CasUtil.indexCovered
+dexCovering
 ;%0Aim
@@ -1364,17 +1364,149 @@
 CasUtil.
-*
+selectCovered;%0Aimport static org.apache.uima.fit.util.CasUtil.selectCovering;%0Aimport static org.apache.uima.fit.util.CasUtil.selectFS
 ;%0A%0Aimpor
@@ -4155,10 +4155,727 @@
 ();%0A  %7D%0A
+  %0A  @Test%0A  public void benchmarkSelectCovering() %7B%0A    Benchmark template = new Benchmark(%22TEMPLATE%22)%0A      .initialize(n -%3E initRandomCas(cas, n))%0A      .magnitude(10)%0A      .magnitudeIncrement(count -%3E count * 10)%0A      .incrementTimes(3);%0A    %0A    new Benchmark(%22JCas selectCovering%22, template)%0A      .measure(() -%3E %7B%0A        Type sentenceType = getType(cas, TYPE_NAME_SENTENCE);%0A        Type tokenType = getType(cas, TYPE_NAME_TOKEN);%0A        select(cas, tokenType).forEach(s -%3E selectCovering(sentenceType, s));%0A      %7D)%0A      .run();%0A%0A    new Benchmark(%22JCas indexCovering%22, template)%0A      .measure(() -%3E indexCovering(cas, getType(cas, TYPE_NAME_TOKEN), getType(cas, TYPE_NAME_SENTENCE)))%0A      .run();%0A  %7D%0A
 %7D%0A

Add basic benchmarking module