[UIMA-6291] Improve uimaFIT benchmarking module
- Added support for benchmark groups
- Avoid re-building the CAS for every single benchmark to make benchmarks in a group more comparable
- Provide informative summaries for groups
diff --git a/uimafit-benchmark/src/main/java/org/apache/uima/fit/benchmark/BenchmarkGroup.java b/uimafit-benchmark/src/main/java/org/apache/uima/fit/benchmark/BenchmarkGroup.java
new file mode 100644
index 0000000..e6bf4e9
--- /dev/null
+++ b/uimafit-benchmark/src/main/java/org/apache/uima/fit/benchmark/BenchmarkGroup.java
@@ -0,0 +1,58 @@
+/*
+ * 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 java.util.Comparator.comparing;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class BenchmarkGroup {
+ private final String name;
+ private final List<Benchmark> benchmarks = new ArrayList<>();
+
+ public BenchmarkGroup(String aName) {
+ name = aName;
+ }
+
+ public BenchmarkGroup add(Benchmark aBenchmark) {
+ benchmarks.add(aBenchmark);
+ return this;
+ }
+
+ public void runAll() {
+ System.out.printf(">>>>>>>>>>>>>>>>>>%n");
+ System.out.printf("GROUP: %s%n", name);
+
+ for (Benchmark benchmark : benchmarks) {
+ benchmark.run();
+ }
+
+ System.out.printf("%n%nSorted by execution time:%n");
+ benchmarks.stream()
+ .sorted(comparing(Benchmark::getCumulativeDuration))
+ .forEach(benchmark -> {
+ Measurement slowest = benchmark.getSlowestMeasurement();
+ System.out.printf("%6dms / %4dms -- %s%n", benchmark.getCumulativeDuration(), slowest.getDuration(),
+ benchmark.getName());
+ });
+
+ System.out.printf(">>>>>>>>>>>>>>>>>>%n%n");
+ }
+}
diff --git a/uimafit-benchmark/src/main/java/org/apache/uima/fit/benchmark/CachingRandomJCasProvider.java b/uimafit-benchmark/src/main/java/org/apache/uima/fit/benchmark/CachingRandomJCasProvider.java
new file mode 100644
index 0000000..1272dd4
--- /dev/null
+++ b/uimafit-benchmark/src/main/java/org/apache/uima/fit/benchmark/CachingRandomJCasProvider.java
@@ -0,0 +1,57 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Map;
+import org.apache.uima.cas.CASException;
+import org.apache.uima.fit.factory.JCasFactory;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+
+public class CachingRandomJCasProvider {
+ private static final long RANDOM_SEED = 12345l;
+
+ private final Map<Integer, JCas> cache = new HashMap<>();
+
+ private JCas preparedJCas;
+
+ public void prepare(int size) {
+ JCas cachedJCas = cache.get(size);
+
+ if (cachedJCas == null) {
+ try {
+ cachedJCas = JCasFactory.createJCas();
+ } catch (ResourceInitializationException | CASException e) {
+ throw new RuntimeException(e);
+ }
+
+ initRandomCas(cachedJCas.getCas(), size, RANDOM_SEED);
+ cache.put(size, cachedJCas);
+ }
+
+ preparedJCas = cachedJCas;
+ }
+
+ public JCas get() {
+ return preparedJCas;
+ }
+}