diff --git a/guava-bench/pom.xml b/guava-bench/pom.xml new file mode 100644 index 000000000000..7e3508de40ec --- /dev/null +++ b/guava-bench/pom.xml @@ -0,0 +1,134 @@ + + + 4.0.0 + + com.google.guava + guava-parent + 1.0-HEAD-jre-SNAPSHOT + + guava-bench + Guava Benchmarks + + Benchmarks for Guava, on JMH. + + + + ${project.groupId} + guava-testlib + ${project.version} + test + + + com.google.code.findbugs + jsr305 + test + + + org.checkerframework + checker-qual + test + + + com.google.errorprone + error_prone_annotations + test + + + ${project.groupId} + guava + ${project.version} + test + + + com.google.truth + truth + ${truth.version} + test + + + com.google.truth.extensions + truth-java8-extension + ${truth.version} + test + + + com.google.jimfs + jimfs + 1.3.0 + test + + + junit + junit + 4.13.2 + test + + + com.google.caliper + caliper + 1.0-beta-3 + test + + + org.openjdk.jmh + jmh-core + 1.37 + test + + + + + + maven-compiler-plugin + + + maven-jar-plugin + + + create-test-jar + test-jar + + + + + org.codehaus.mojo + exec-maven-plugin + + com.google.guava.benchmarks.Benchmarks + test + + + + run-bench + test + + java + + + + + + + + + jvm11plus + + [11,] + + + + + maven-compiler-plugin + + + -Xlint:-removal + -Xlint:-options + + + + + + + + diff --git a/guava-tests/benchmark/com/google/common/base/AsciiBenchmark.java b/guava-bench/test/com/google/common/base/AsciiBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/AsciiBenchmark.java rename to guava-bench/test/com/google/common/base/AsciiBenchmark.java diff --git a/guava-tests/test/com/google/common/base/BenchmarkHelpers.java b/guava-bench/test/com/google/common/base/BenchmarkHelpers.java similarity index 100% rename from guava-tests/test/com/google/common/base/BenchmarkHelpers.java rename to guava-bench/test/com/google/common/base/BenchmarkHelpers.java diff --git a/guava-tests/benchmark/com/google/common/base/CharMatcherBenchmark.java b/guava-bench/test/com/google/common/base/CharMatcherBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/CharMatcherBenchmark.java rename to guava-bench/test/com/google/common/base/CharMatcherBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/base/EnumsBenchmark.java b/guava-bench/test/com/google/common/base/EnumsBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/EnumsBenchmark.java rename to guava-bench/test/com/google/common/base/EnumsBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/base/JoinerBenchmark.java b/guava-bench/test/com/google/common/base/JoinerBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/JoinerBenchmark.java rename to guava-bench/test/com/google/common/base/JoinerBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/base/LazyStackTraceBenchmark.java b/guava-bench/test/com/google/common/base/LazyStackTraceBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/LazyStackTraceBenchmark.java rename to guava-bench/test/com/google/common/base/LazyStackTraceBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/base/ObjectsBenchmark.java b/guava-bench/test/com/google/common/base/ObjectsBenchmark.java similarity index 96% rename from guava-tests/benchmark/com/google/common/base/ObjectsBenchmark.java rename to guava-bench/test/com/google/common/base/ObjectsBenchmark.java index 561f76dca98b..d26e192077ff 100644 --- a/guava-tests/benchmark/com/google/common/base/ObjectsBenchmark.java +++ b/guava-bench/test/com/google/common/base/ObjectsBenchmark.java @@ -19,7 +19,7 @@ import com.google.caliper.Benchmark; /** - * Some microbenchmarks for the {@link com.google.common.base.Objects} class. + * Some microbenchmarks for the {@link Objects} class. * * @author Ben L. Titzer */ diff --git a/guava-tests/benchmark/com/google/common/base/SplitterBenchmark.java b/guava-bench/test/com/google/common/base/SplitterBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/SplitterBenchmark.java rename to guava-bench/test/com/google/common/base/SplitterBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/base/StopwatchBenchmark.java b/guava-bench/test/com/google/common/base/StopwatchBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/StopwatchBenchmark.java rename to guava-bench/test/com/google/common/base/StopwatchBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/base/StringsRepeatBenchmark.java b/guava-bench/test/com/google/common/base/StringsRepeatBenchmark.java similarity index 97% rename from guava-tests/benchmark/com/google/common/base/StringsRepeatBenchmark.java rename to guava-bench/test/com/google/common/base/StringsRepeatBenchmark.java index 16e00ef3d2e2..b20d33dff0b0 100644 --- a/guava-tests/benchmark/com/google/common/base/StringsRepeatBenchmark.java +++ b/guava-bench/test/com/google/common/base/StringsRepeatBenchmark.java @@ -21,7 +21,7 @@ import com.google.caliper.Param; /** - * Microbenchmark for {@link com.google.common.base.Strings#repeat} + * Microbenchmark for {@link Strings#repeat} * * @author Mike Cripps */ diff --git a/guava-tests/benchmark/com/google/common/base/ToStringHelperBenchmark.java b/guava-bench/test/com/google/common/base/ToStringHelperBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/ToStringHelperBenchmark.java rename to guava-bench/test/com/google/common/base/ToStringHelperBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/base/WhitespaceMatcherBenchmark.java b/guava-bench/test/com/google/common/base/WhitespaceMatcherBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/base/WhitespaceMatcherBenchmark.java rename to guava-bench/test/com/google/common/base/WhitespaceMatcherBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/cache/ChainBenchmark.java b/guava-bench/test/com/google/common/cache/ChainBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/cache/ChainBenchmark.java rename to guava-bench/test/com/google/common/cache/ChainBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/cache/LoadingCacheSingleThreadBenchmark.java b/guava-bench/test/com/google/common/cache/LoadingCacheSingleThreadBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/cache/LoadingCacheSingleThreadBenchmark.java rename to guava-bench/test/com/google/common/cache/LoadingCacheSingleThreadBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/cache/MapMakerComparisonBenchmark.java b/guava-bench/test/com/google/common/cache/MapMakerComparisonBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/cache/MapMakerComparisonBenchmark.java rename to guava-bench/test/com/google/common/cache/MapMakerComparisonBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/cache/SegmentBenchmark.java b/guava-bench/test/com/google/common/cache/SegmentBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/cache/SegmentBenchmark.java rename to guava-bench/test/com/google/common/cache/SegmentBenchmark.java diff --git a/guava-bench/test/com/google/common/collect/BenchmarkHelpers.java b/guava-bench/test/com/google/common/collect/BenchmarkHelpers.java new file mode 100644 index 000000000000..5a5bdad7addd --- /dev/null +++ b/guava-bench/test/com/google/common/collect/BenchmarkHelpers.java @@ -0,0 +1,420 @@ +/* + * Copyright (C) 2012 The Guava Authors + * + * Licensed 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 com.google.common.collect; + +import com.google.common.base.Equivalence; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentSkipListMap; + +import static com.google.common.base.Preconditions.checkState; + +/** + * Helper classes for various benchmarks. + * + * @author Christopher Swenson + */ +final class BenchmarkHelpers { + /** So far, this is the best way to test various implementations of {@link Set} subclasses. */ + public interface CollectionsImplEnum { + > Collection create(Collection contents); + + String name(); + } + + public interface MapsImplEnum { + , V> Map create(Map contents); + + String name(); + } + + public interface InternerImplEnum { + Interner create(Collection contents); + + String name(); + } + + public enum SetImpl implements CollectionsImplEnum { + HashSetImpl { + @Override + public > Set create(Collection contents) { + return new HashSet(contents); + } + }, + LinkedHashSetImpl { + @Override + public > Set create(Collection contents) { + return new LinkedHashSet(contents); + } + }, + TreeSetImpl { + @Override + public > Set create(Collection contents) { + return new TreeSet(contents); + } + }, + UnmodifiableSetImpl { + @Override + public > Set create(Collection contents) { + return Collections.unmodifiableSet(new HashSet(contents)); + } + }, + SynchronizedSetImpl { + @Override + public > Set create(Collection contents) { + return Collections.synchronizedSet(new HashSet(contents)); + } + }, + ImmutableSetImpl { + @Override + public > Set create(Collection contents) { + return ImmutableSet.copyOf(contents); + } + }, + ImmutableSortedSetImpl { + @Override + public > Set create(Collection contents) { + return ImmutableSortedSet.copyOf(contents); + } + }, + ContiguousSetImpl { + @Override + public > Set create(Collection contents) { + return ContiguousSet.copyOf(contents); + } + }, + ; + } + + public enum ListMultimapImpl { + ArrayListMultimapImpl { + @Override + ListMultimap create(Multimap contents) { + return ArrayListMultimap.create(contents); + } + }, + LinkedListMultimapImpl { + @Override + ListMultimap create(Multimap contents) { + return LinkedListMultimap.create(contents); + } + }, + ImmutableListMultimapImpl { + @Override + ListMultimap create(Multimap contents) { + return ImmutableListMultimap.copyOf(contents); + } + }; + + abstract ListMultimap create(Multimap contents); + } + + public enum RangeSetImpl { + TreeRangeSetImpl { + @Override + > RangeSet create(RangeSet contents) { + return TreeRangeSet.create(contents); + } + }, + ImmutableRangeSetImpl { + @Override + > RangeSet create(RangeSet contents) { + return ImmutableRangeSet.copyOf(contents); + } + }; + + abstract > RangeSet create(RangeSet contents); + } + + public enum SetMultimapImpl { + HashMultimapImpl { + @Override + , V extends Comparable> SetMultimap create( + Multimap contents) { + return HashMultimap.create(contents); + } + }, + LinkedHashMultimapImpl { + @Override + , V extends Comparable> SetMultimap create( + Multimap contents) { + return LinkedHashMultimap.create(contents); + } + }, + TreeMultimapImpl { + @Override + , V extends Comparable> SetMultimap create( + Multimap contents) { + return TreeMultimap.create(contents); + } + }, + ImmutableSetMultimapImpl { + @Override + , V extends Comparable> SetMultimap create( + Multimap contents) { + return ImmutableSetMultimap.copyOf(contents); + } + }; + + abstract , V extends Comparable> SetMultimap create( + Multimap contents); + } + + public enum MapImpl implements MapsImplEnum { + HashMapImpl { + @Override + public , V> Map create(Map map) { + return Maps.newHashMap(map); + } + }, + LinkedHashMapImpl { + @Override + public , V> Map create(Map map) { + return Maps.newLinkedHashMap(map); + } + }, + ConcurrentHashMapImpl { + @Override + public , V> Map create(Map map) { + return new ConcurrentHashMap<>(map); + } + }, + ImmutableMapImpl { + @Override + public , V> Map create(Map map) { + return ImmutableMap.copyOf(map); + } + }, + MapMakerStrongKeysStrongValues { + @Override + public , V> Map create(Map map) { + // We use a "custom" equivalence to force MapMaker to make a MapMakerInternalMap. + ConcurrentMap newMap = new MapMaker().keyEquivalence(Equivalence.equals()).makeMap(); + checkState(newMap instanceof MapMakerInternalMap); + newMap.putAll(map); + return newMap; + } + }, + MapMakerStrongKeysWeakValues { + @Override + public , V> Map create(Map map) { + ConcurrentMap newMap = new MapMaker().weakValues().makeMap(); + checkState(newMap instanceof MapMakerInternalMap); + newMap.putAll(map); + return newMap; + } + }, + MapMakerWeakKeysStrongValues { + @Override + public , V> Map create(Map map) { + ConcurrentMap newMap = new MapMaker().weakKeys().makeMap(); + checkState(newMap instanceof MapMakerInternalMap); + newMap.putAll(map); + return newMap; + } + }, + MapMakerWeakKeysWeakValues { + @Override + public , V> Map create(Map map) { + ConcurrentMap newMap = new MapMaker().weakKeys().weakValues().makeMap(); + checkState(newMap instanceof MapMakerInternalMap); + newMap.putAll(map); + return newMap; + } + }; + } + + enum SortedMapImpl implements MapsImplEnum { + TreeMapImpl { + @Override + public , V> SortedMap create(Map map) { + SortedMap result = Maps.newTreeMap(); + result.putAll(map); + return result; + } + }, + ConcurrentSkipListImpl { + @Override + public , V> SortedMap create(Map map) { + return new ConcurrentSkipListMap<>(map); + } + }, + ImmutableSortedMapImpl { + @Override + public , V> SortedMap create(Map map) { + return ImmutableSortedMap.copyOf(map); + } + }; + } + + enum BiMapImpl implements MapsImplEnum { + HashBiMapImpl { + @Override + public , V> BiMap create(Map map) { + return HashBiMap.create(map); + } + }, + ImmutableBiMapImpl { + @Override + public , V> BiMap create(Map map) { + return ImmutableBiMap.copyOf(map); + } + }; + + @Override + public abstract , V> BiMap create(Map map); + } + + enum MultisetImpl implements CollectionsImplEnum { + HashMultisetImpl { + @Override + public > Multiset create(Collection contents) { + return HashMultiset.create(contents); + } + }, + LinkedHashMultisetImpl { + @Override + public > Multiset create(Collection contents) { + return LinkedHashMultiset.create(contents); + } + }, + ConcurrentHashMultisetImpl { + @Override + public > Multiset create(Collection contents) { + return ConcurrentHashMultiset.create(contents); + } + }, + ImmutableMultisetImpl { + @Override + public > Multiset create(Collection contents) { + return ImmutableMultiset.copyOf(contents); + } + }; + } + + enum SortedMultisetImpl implements CollectionsImplEnum { + TreeMultisetImpl { + @Override + public > SortedMultiset create(Collection contents) { + return TreeMultiset.create(contents); + } + }, + ImmutableSortedMultisetImpl { + @Override + public > SortedMultiset create(Collection contents) { + return ImmutableSortedMultiset.copyOf(contents); + } + }; + } + + enum QueueImpl implements CollectionsImplEnum { + MinMaxPriorityQueueImpl { + @Override + public > Queue create(Collection contents) { + return MinMaxPriorityQueue.create(contents); + } + }; + } + + enum TableImpl { + HashBasedTableImpl { + @Override + , C extends Comparable, V> Table create( + Table contents) { + return HashBasedTable.create(contents); + } + }, + TreeBasedTableImpl { + @Override + , C extends Comparable, V> Table create( + Table contents) { + Table table = TreeBasedTable.create(); + table.putAll(contents); + return table; + } + }, + ArrayTableImpl { + @Override + , C extends Comparable, V> Table create( + Table contents) { + if (contents.isEmpty()) { + return ImmutableTable.of(); + } else { + return ArrayTable.create(contents); + } + } + }, + ImmutableTableImpl { + @Override + , C extends Comparable, V> Table create( + Table contents) { + return ImmutableTable.copyOf(contents); + } + }; + + abstract , C extends Comparable, V> Table create( + Table contents); + } + + public enum InternerImpl implements InternerImplEnum { + WeakInternerImpl { + @Override + public Interner create(Collection contents) { + Interner interner = Interners.newWeakInterner(); + for (E e : contents) { + E unused = interner.intern(e); + } + return interner; + } + }, + StrongInternerImpl { + @Override + public Interner create(Collection contents) { + Interner interner = Interners.newStrongInterner(); + for (E e : contents) { + E unused = interner.intern(e); + } + return interner; + } + }; + } + + public enum Value { + INSTANCE; + } + + public enum ListSizeDistribution { + UNIFORM_0_TO_2(0, 2), + UNIFORM_0_TO_9(0, 9), + ALWAYS_0(0, 0), + ALWAYS_10(10, 10); + + final int min; + final int max; + + private ListSizeDistribution(int min, int max) { + this.min = min; + this.max = max; + } + + public int chooseSize(Random random) { + return random.nextInt(max - min + 1) + min; + } + } +} diff --git a/guava-tests/benchmark/com/google/common/collect/BinaryTreeTraverserBenchmark.java b/guava-bench/test/com/google/common/collect/BinaryTreeTraverserBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/BinaryTreeTraverserBenchmark.java rename to guava-bench/test/com/google/common/collect/BinaryTreeTraverserBenchmark.java diff --git a/guava-tests/test/com/google/common/collect/CollectionBenchmarkSampleData.java b/guava-bench/test/com/google/common/collect/CollectionBenchmarkSampleData.java similarity index 99% rename from guava-tests/test/com/google/common/collect/CollectionBenchmarkSampleData.java rename to guava-bench/test/com/google/common/collect/CollectionBenchmarkSampleData.java index 0504e6a745f6..9cd8386adeb2 100644 --- a/guava-tests/test/com/google/common/collect/CollectionBenchmarkSampleData.java +++ b/guava-bench/test/com/google/common/collect/CollectionBenchmarkSampleData.java @@ -16,13 +16,14 @@ package com.google.common.collect; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.common.primitives.Ints; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.util.Collections; import java.util.List; import java.util.Set; -import org.checkerframework.checker.nullness.qual.Nullable; + +import static com.google.common.base.Preconditions.checkNotNull; /** * Package up sample data for common collections benchmarking. diff --git a/guava-tests/benchmark/com/google/common/collect/ComparatorDelegationOverheadBenchmark.java b/guava-bench/test/com/google/common/collect/ComparatorDelegationOverheadBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/ComparatorDelegationOverheadBenchmark.java rename to guava-bench/test/com/google/common/collect/ComparatorDelegationOverheadBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/ConcurrentHashMultisetBenchmark.java b/guava-bench/test/com/google/common/collect/ConcurrentHashMultisetBenchmark.java similarity index 98% rename from guava-tests/benchmark/com/google/common/collect/ConcurrentHashMultisetBenchmark.java rename to guava-bench/test/com/google/common/collect/ConcurrentHashMultisetBenchmark.java index 322dd547f6d1..9975ba6e3335 100644 --- a/guava-tests/benchmark/com/google/common/collect/ConcurrentHashMultisetBenchmark.java +++ b/guava-bench/test/com/google/common/collect/ConcurrentHashMultisetBenchmark.java @@ -236,7 +236,7 @@ public T[] toArray(T[] array) { */ private List snapshot() { List list = Lists.newArrayListWithExpectedSize(size()); - for (Multiset.Entry entry : entrySet()) { + for (Entry entry : entrySet()) { E element = entry.getElement(); for (int i = entry.getCount(); i > 0; i--) { list.add(element); @@ -446,7 +446,7 @@ Iterator elementIterator() { private transient EntrySet entrySet; @Override - public Set> entrySet() { + public Set> entrySet() { EntrySet result = entrySet; if (result == null) { entrySet = result = new EntrySet(); @@ -474,7 +474,7 @@ public boolean hasNext() { } @Override - public Multiset.Entry next() { + public Entry next() { Map.Entry backingEntry = backingIterator.next(); return Multisets.immutableEntry(backingEntry.getKey(), backingEntry.getValue()); } @@ -517,8 +517,8 @@ public T[] toArray(T[] array) { return snapshot().toArray(array); } - private List> snapshot() { - List> list = Lists.newArrayListWithExpectedSize(size()); + private List> snapshot() { + List> list = Lists.newArrayListWithExpectedSize(size()); // not Iterables.addAll(list, this), because that'll forward back here Iterators.addAll(list, iterator()); return list; @@ -527,7 +527,7 @@ private List> snapshot() { @Override public boolean remove(Object object) { if (object instanceof Multiset.Entry) { - Multiset.Entry entry = (Multiset.Entry) object; + Entry entry = (Entry) object; Object element = entry.getElement(); int entryCount = entry.getCount(); return countMap.remove(element, entryCount); diff --git a/guava-tests/benchmark/com/google/common/collect/HashMultisetAddPresentBenchmark.java b/guava-bench/test/com/google/common/collect/HashMultisetAddPresentBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/HashMultisetAddPresentBenchmark.java rename to guava-bench/test/com/google/common/collect/HashMultisetAddPresentBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/ImmutableListCreationBenchmark.java b/guava-bench/test/com/google/common/collect/ImmutableListCreationBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/ImmutableListCreationBenchmark.java rename to guava-bench/test/com/google/common/collect/ImmutableListCreationBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/ImmutableSetHashFloodingDetectionBenchmark.java b/guava-bench/test/com/google/common/collect/ImmutableSetHashFloodingDetectionBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/ImmutableSetHashFloodingDetectionBenchmark.java rename to guava-bench/test/com/google/common/collect/ImmutableSetHashFloodingDetectionBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/InternersBenchmark.java b/guava-bench/test/com/google/common/collect/InternersBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/InternersBenchmark.java rename to guava-bench/test/com/google/common/collect/InternersBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/IteratorBenchmark.java b/guava-bench/test/com/google/common/collect/IteratorBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/IteratorBenchmark.java rename to guava-bench/test/com/google/common/collect/IteratorBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/MapBenchmark.java b/guava-bench/test/com/google/common/collect/MapBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/MapBenchmark.java rename to guava-bench/test/com/google/common/collect/MapBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/MinMaxPriorityQueueBenchmark.java b/guava-bench/test/com/google/common/collect/MinMaxPriorityQueueBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/MinMaxPriorityQueueBenchmark.java rename to guava-bench/test/com/google/common/collect/MinMaxPriorityQueueBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/MultipleSetContainsBenchmark.java b/guava-bench/test/com/google/common/collect/MultipleSetContainsBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/MultipleSetContainsBenchmark.java rename to guava-bench/test/com/google/common/collect/MultipleSetContainsBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/MultisetIteratorBenchmark.java b/guava-bench/test/com/google/common/collect/MultisetIteratorBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/MultisetIteratorBenchmark.java rename to guava-bench/test/com/google/common/collect/MultisetIteratorBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/PowerSetBenchmark.java b/guava-bench/test/com/google/common/collect/PowerSetBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/PowerSetBenchmark.java rename to guava-bench/test/com/google/common/collect/PowerSetBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/SetContainsBenchmark.java b/guava-bench/test/com/google/common/collect/SetContainsBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/SetContainsBenchmark.java rename to guava-bench/test/com/google/common/collect/SetContainsBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/SetCreationBenchmark.java b/guava-bench/test/com/google/common/collect/SetCreationBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/SetCreationBenchmark.java rename to guava-bench/test/com/google/common/collect/SetCreationBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/SetIterationBenchmark.java b/guava-bench/test/com/google/common/collect/SetIterationBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/SetIterationBenchmark.java rename to guava-bench/test/com/google/common/collect/SetIterationBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/collect/SortedCopyBenchmark.java b/guava-bench/test/com/google/common/collect/SortedCopyBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/SortedCopyBenchmark.java rename to guava-bench/test/com/google/common/collect/SortedCopyBenchmark.java diff --git a/guava-tests/test/com/google/common/collect/SpecialRandom.java b/guava-bench/test/com/google/common/collect/SpecialRandom.java similarity index 100% rename from guava-tests/test/com/google/common/collect/SpecialRandom.java rename to guava-bench/test/com/google/common/collect/SpecialRandom.java diff --git a/guava-tests/benchmark/com/google/common/collect/StreamsBenchmark.java b/guava-bench/test/com/google/common/collect/StreamsBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/collect/StreamsBenchmark.java rename to guava-bench/test/com/google/common/collect/StreamsBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/eventbus/EventBusBenchmark.java b/guava-bench/test/com/google/common/eventbus/EventBusBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/eventbus/EventBusBenchmark.java rename to guava-bench/test/com/google/common/eventbus/EventBusBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/hash/ChecksumBenchmark.java b/guava-bench/test/com/google/common/hash/ChecksumBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/hash/ChecksumBenchmark.java rename to guava-bench/test/com/google/common/hash/ChecksumBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/hash/HashCodeBenchmark.java b/guava-bench/test/com/google/common/hash/HashCodeBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/hash/HashCodeBenchmark.java rename to guava-bench/test/com/google/common/hash/HashCodeBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/hash/HashFunctionBenchmark.java b/guava-bench/test/com/google/common/hash/HashFunctionBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/hash/HashFunctionBenchmark.java rename to guava-bench/test/com/google/common/hash/HashFunctionBenchmark.java diff --git a/guava-tests/test/com/google/common/hash/HashFunctionEnum.java b/guava-bench/test/com/google/common/hash/HashFunctionEnum.java similarity index 100% rename from guava-tests/test/com/google/common/hash/HashFunctionEnum.java rename to guava-bench/test/com/google/common/hash/HashFunctionEnum.java diff --git a/guava-tests/benchmark/com/google/common/hash/HashStringBenchmark.java b/guava-bench/test/com/google/common/hash/HashStringBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/hash/HashStringBenchmark.java rename to guava-bench/test/com/google/common/hash/HashStringBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/hash/MessageDigestAlgorithmBenchmark.java b/guava-bench/test/com/google/common/hash/MessageDigestAlgorithmBenchmark.java similarity index 97% rename from guava-tests/benchmark/com/google/common/hash/MessageDigestAlgorithmBenchmark.java rename to guava-bench/test/com/google/common/hash/MessageDigestAlgorithmBenchmark.java index 2e252d127b68..e245dd14b45e 100644 --- a/guava-tests/benchmark/com/google/common/hash/MessageDigestAlgorithmBenchmark.java +++ b/guava-bench/test/com/google/common/hash/MessageDigestAlgorithmBenchmark.java @@ -24,7 +24,7 @@ import java.util.Random; /** - * Benchmarks for comparing {@link MessageDigest}s and {@link com.google.common.hash.HashFunction}s + * Benchmarks for comparing {@link MessageDigest}s and {@link HashFunction}s * that wrap {@link MessageDigest}s. * *

Parameters for the benchmark are: diff --git a/guava-tests/benchmark/com/google/common/hash/MessageDigestCreationBenchmark.java b/guava-bench/test/com/google/common/hash/MessageDigestCreationBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/hash/MessageDigestCreationBenchmark.java rename to guava-bench/test/com/google/common/hash/MessageDigestCreationBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/io/BaseEncodingBenchmark.java b/guava-bench/test/com/google/common/io/BaseEncodingBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/io/BaseEncodingBenchmark.java rename to guava-bench/test/com/google/common/io/BaseEncodingBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/io/ByteSourceAsCharSourceReadBenchmark.java b/guava-bench/test/com/google/common/io/ByteSourceAsCharSourceReadBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/io/ByteSourceAsCharSourceReadBenchmark.java rename to guava-bench/test/com/google/common/io/ByteSourceAsCharSourceReadBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/io/CharStreamsCopyBenchmark.java b/guava-bench/test/com/google/common/io/CharStreamsCopyBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/io/CharStreamsCopyBenchmark.java rename to guava-bench/test/com/google/common/io/CharStreamsCopyBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/ApacheBenchmark.java b/guava-bench/test/com/google/common/math/ApacheBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/ApacheBenchmark.java rename to guava-bench/test/com/google/common/math/ApacheBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/BigIntegerMathBenchmark.java b/guava-bench/test/com/google/common/math/BigIntegerMathBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/BigIntegerMathBenchmark.java rename to guava-bench/test/com/google/common/math/BigIntegerMathBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/BigIntegerMathRoundingBenchmark.java b/guava-bench/test/com/google/common/math/BigIntegerMathRoundingBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/BigIntegerMathRoundingBenchmark.java rename to guava-bench/test/com/google/common/math/BigIntegerMathRoundingBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/DoubleMathBenchmark.java b/guava-bench/test/com/google/common/math/DoubleMathBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/DoubleMathBenchmark.java rename to guava-bench/test/com/google/common/math/DoubleMathBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/DoubleMathRoundingBenchmark.java b/guava-bench/test/com/google/common/math/DoubleMathRoundingBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/DoubleMathRoundingBenchmark.java rename to guava-bench/test/com/google/common/math/DoubleMathRoundingBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/IntMathBenchmark.java b/guava-bench/test/com/google/common/math/IntMathBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/IntMathBenchmark.java rename to guava-bench/test/com/google/common/math/IntMathBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/IntMathRoundingBenchmark.java b/guava-bench/test/com/google/common/math/IntMathRoundingBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/IntMathRoundingBenchmark.java rename to guava-bench/test/com/google/common/math/IntMathRoundingBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/LessThanBenchmark.java b/guava-bench/test/com/google/common/math/LessThanBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/LessThanBenchmark.java rename to guava-bench/test/com/google/common/math/LessThanBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/LongMathBenchmark.java b/guava-bench/test/com/google/common/math/LongMathBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/LongMathBenchmark.java rename to guava-bench/test/com/google/common/math/LongMathBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/math/LongMathRoundingBenchmark.java b/guava-bench/test/com/google/common/math/LongMathRoundingBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/LongMathRoundingBenchmark.java rename to guava-bench/test/com/google/common/math/LongMathRoundingBenchmark.java diff --git a/guava-tests/test/com/google/common/math/MathBenchmarking.java b/guava-bench/test/com/google/common/math/MathBenchmarking.java similarity index 100% rename from guava-tests/test/com/google/common/math/MathBenchmarking.java rename to guava-bench/test/com/google/common/math/MathBenchmarking.java diff --git a/guava-tests/benchmark/com/google/common/math/QuantilesBenchmark.java b/guava-bench/test/com/google/common/math/QuantilesBenchmark.java similarity index 98% rename from guava-tests/benchmark/com/google/common/math/QuantilesBenchmark.java rename to guava-bench/test/com/google/common/math/QuantilesBenchmark.java index d97c2d56d642..7cf7e312bbcd 100644 --- a/guava-tests/benchmark/com/google/common/math/QuantilesBenchmark.java +++ b/guava-bench/test/com/google/common/math/QuantilesBenchmark.java @@ -23,6 +23,7 @@ import com.google.common.collect.DiscreteDomain; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Range; +import com.google.common.testing.math.QuantilesAlgorithm; import java.util.Random; /** Benchmarks some algorithms providing the same functionality as {@link Quantiles}. */ diff --git a/guava-tests/benchmark/com/google/common/math/StatsBenchmark.java b/guava-bench/test/com/google/common/math/StatsBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/math/StatsBenchmark.java rename to guava-bench/test/com/google/common/math/StatsBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/primitives/UnsignedBytesBenchmark.java b/guava-bench/test/com/google/common/primitives/UnsignedBytesBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/primitives/UnsignedBytesBenchmark.java rename to guava-bench/test/com/google/common/primitives/UnsignedBytesBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/primitives/UnsignedLongsBenchmark.java b/guava-bench/test/com/google/common/primitives/UnsignedLongsBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/primitives/UnsignedLongsBenchmark.java rename to guava-bench/test/com/google/common/primitives/UnsignedLongsBenchmark.java diff --git a/guava-tests/test/com/google/common/util/concurrent/AbstractFutureBenchmarks.java b/guava-bench/test/com/google/common/util/concurrent/AbstractFutureBenchmarks.java similarity index 97% rename from guava-tests/test/com/google/common/util/concurrent/AbstractFutureBenchmarks.java rename to guava-bench/test/com/google/common/util/concurrent/AbstractFutureBenchmarks.java index d90c5bbe807d..9321eb05272a 100644 --- a/guava-tests/test/com/google/common/util/concurrent/AbstractFutureBenchmarks.java +++ b/guava-bench/test/com/google/common/util/concurrent/AbstractFutureBenchmarks.java @@ -16,17 +16,14 @@ package com.google.common.util.concurrent; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.errorprone.annotations.CanIgnoreReturnValue; -import java.util.concurrent.CancellationException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executor; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.concurrent.locks.AbstractQueuedSynchronizer; import org.checkerframework.checker.nullness.qual.Nullable; +import java.util.concurrent.*; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; + +import static com.google.common.base.Preconditions.checkNotNull; + /** Utilities for the AbstractFutureBenchmarks */ final class AbstractFutureBenchmarks { private AbstractFutureBenchmarks() {} @@ -211,7 +208,7 @@ public void addListener(Runnable listener, Executor exec) { /** * Subclasses should invoke this method to set the result of the computation to {@code value}. - * This will set the state of the future to {@link OldAbstractFuture.Sync#COMPLETED} and invoke + * This will set the state of the future to {@link Sync#COMPLETED} and invoke * the listeners if the state was successfully changed. * * @param value the value that was the result of the task. @@ -228,7 +225,7 @@ protected boolean set(@Nullable V value) { /** * Subclasses should invoke this method to set the result of the computation to an error, {@code - * throwable}. This will set the state of the future to {@link OldAbstractFuture.Sync#COMPLETED} + * throwable}. This will set the state of the future to {@link Sync#COMPLETED} * and invoke the listeners if the state was successfully changed. * * @param throwable the exception that the task failed with. diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/AbstractFutureFootprintBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/AbstractFutureFootprintBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/AbstractFutureFootprintBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/AbstractFutureFootprintBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/CycleDetectingLockFactoryBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/CycleDetectingLockFactoryBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/CycleDetectingLockFactoryBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/CycleDetectingLockFactoryBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/ExecutionListBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/ExecutionListBenchmark.java similarity index 99% rename from guava-tests/benchmark/com/google/common/util/concurrent/ExecutionListBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/ExecutionListBenchmark.java index 946b85109cb8..def54109f0e4 100644 --- a/guava-tests/benchmark/com/google/common/util/concurrent/ExecutionListBenchmark.java +++ b/guava-bench/test/com/google/common/util/concurrent/ExecutionListBenchmark.java @@ -42,6 +42,7 @@ /** Benchmarks for {@link ExecutionList}. */ @VmOptions({"-Xms8g", "-Xmx8g"}) +@SuppressWarnings({"removal", "deprecation"}) public class ExecutionListBenchmark { private static final int NUM_THREADS = 10; // make a param? @@ -375,7 +376,7 @@ public void run() { // This is the old implementation of ExecutionList using a LinkedList. private static final class OldExecutionList { static final Logger log = Logger.getLogger(OldExecutionList.class.getName()); - final Queue runnables = Lists.newLinkedList(); + final Queue runnables = Lists.newLinkedList(); boolean executed = false; public void add(Runnable runnable, Executor executor) { diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/FuturesGetCheckedBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/FuturesGetCheckedBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/FuturesGetCheckedBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/FuturesGetCheckedBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/MonitorBasedArrayBlockingQueue.java b/guava-bench/test/com/google/common/util/concurrent/MonitorBasedArrayBlockingQueue.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/MonitorBasedArrayBlockingQueue.java rename to guava-bench/test/com/google/common/util/concurrent/MonitorBasedArrayBlockingQueue.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/MonitorBasedPriorityBlockingQueue.java b/guava-bench/test/com/google/common/util/concurrent/MonitorBasedPriorityBlockingQueue.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/MonitorBasedPriorityBlockingQueue.java rename to guava-bench/test/com/google/common/util/concurrent/MonitorBasedPriorityBlockingQueue.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/MonitorBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/MonitorBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/MonitorBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/MonitorBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/MoreExecutorsDirectExecutorBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/MoreExecutorsDirectExecutorBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/MoreExecutorsDirectExecutorBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/MoreExecutorsDirectExecutorBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/SingleThreadAbstractFutureBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/SingleThreadAbstractFutureBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/SingleThreadAbstractFutureBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/SingleThreadAbstractFutureBenchmark.java diff --git a/guava-tests/benchmark/com/google/common/util/concurrent/StripedBenchmark.java b/guava-bench/test/com/google/common/util/concurrent/StripedBenchmark.java similarity index 100% rename from guava-tests/benchmark/com/google/common/util/concurrent/StripedBenchmark.java rename to guava-bench/test/com/google/common/util/concurrent/StripedBenchmark.java diff --git a/guava-bench/test/com/google/guava/benchmarks/Benchmarks.java b/guava-bench/test/com/google/guava/benchmarks/Benchmarks.java new file mode 100644 index 000000000000..8f1596df3797 --- /dev/null +++ b/guava-bench/test/com/google/guava/benchmarks/Benchmarks.java @@ -0,0 +1,7 @@ +package com.google.guava.benchmarks; + +public class Benchmarks { + public static void main(String[] args) { + System.out.println("Hello, Guava Benchmarks!"); + } +} diff --git a/guava-testlib/src/com/google/common/testing/math/QuantilesAlgorithm.java b/guava-testlib/src/com/google/common/testing/math/QuantilesAlgorithm.java new file mode 100644 index 000000000000..3ac861bd1bbf --- /dev/null +++ b/guava-testlib/src/com/google/common/testing/math/QuantilesAlgorithm.java @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2014 The Guava Authors + * + * Licensed 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 com.google.common.testing.math; + +import com.google.common.collect.ImmutableMap; +import com.google.common.math.LongMath; +import com.google.common.math.Quantiles; + +import java.math.RoundingMode; +import java.util.Arrays; +import java.util.Collection; +import java.util.Map; + +/** + * Enumerates several algorithms providing equivalent functionality to {@link Quantiles}, for use in + * quantiles tests and benchmarks. These algorithms each calculate either a single quantile or + * multiple quantiles. All algorithms modify the dataset they are given (the cost of a copy to avoid + * this will be constant across algorithms). + * + * @author Pete Gillin + * @since 20.0 + */ +public enum QuantilesAlgorithm { + + /** + * Sorts the dataset, and picks values from it. When computing multiple quantiles, we sort once + * and pick multiple values. + */ + SORTING { + + @Override + public double singleQuantile(int index, int scale, double[] dataset) { + Arrays.sort(dataset); + return singleQuantileFromSorted(index, scale, dataset); + } + + @Override + public Map multipleQuantiles( + Collection indexes, int scale, double[] dataset) { + Arrays.sort(dataset); + ImmutableMap.Builder builder = ImmutableMap.builder(); + for (int index : indexes) { + builder.put(index, singleQuantileFromSorted(index, scale, dataset)); + } + return builder.buildOrThrow(); + } + + private double singleQuantileFromSorted(int index, int scale, double[] dataset) { + long numerator = (long) index * (dataset.length - 1); + int positionFloor = (int) LongMath.divide(numerator, scale, RoundingMode.DOWN); + int remainder = (int) (numerator - positionFloor * scale); + if (remainder == 0) { + return dataset[positionFloor]; + } else { + double positionFrac = (double) remainder / scale; + return dataset[positionFloor] + + positionFrac * (dataset[positionFloor + 1] - dataset[positionFloor]); + } + } + }, + + /** + * Uses quickselect. When calculating multiple quantiles, each quickselect starts from scratch. + */ + QUICKSELECT { + + @Override + public double singleQuantile(int index, int scale, double[] dataset) { + long numerator = (long) index * (dataset.length - 1); + int positionFloor = (int) LongMath.divide(numerator, scale, RoundingMode.DOWN); + int remainder = (int) (numerator - positionFloor * scale); + double percentileFloor = select(positionFloor, dataset); + if (remainder == 0) { + return percentileFloor; + } else { + double percentileCeiling = getMinValue(dataset, positionFloor + 1); + double positionFrac = (double) remainder / scale; + return percentileFloor + positionFrac * (percentileCeiling - percentileFloor); + } + } + + @Override + public Map multipleQuantiles( + Collection indexes, int scale, double[] dataset) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + for (int index : indexes) { + builder.put(index, singleQuantile(index, scale, dataset)); + } + return builder.buildOrThrow(); + } + }, + + /** Uses {@link Quantiles}. */ + TARGET { + + @Override + public double singleQuantile(int index, int scale, double[] dataset) { + return Quantiles.scale(scale).index(index).computeInPlace(dataset); + } + + @Override + public Map multipleQuantiles( + Collection indexes, int scale, double[] dataset) { + return Quantiles.scale(scale).indexes(indexes).computeInPlace(dataset); + } + }, + ; + + /** + * Calculates a single quantile. Equivalent to {@code + * Quantiles.scale(scale).index(index).computeInPlace(dataset)}. + */ + public abstract double singleQuantile(int index, int scale, double[] dataset); + + /** + * Calculates multiple quantiles. Equivalent to {@code + * Quantiles.scale(scale).indexes(indexes).computeInPlace(dataset)}. + */ + public abstract Map multipleQuantiles( + Collection indexes, int scale, double[] dataset); + + static double getMinValue(double[] array, int from) { + // This is basically a copy of com.google.math.Rank#getMinValue, with a small change in the + // method signature: we always search to the end of the array. + int min = from; + for (int i = from + 1; i < array.length; i++) { + if (array[min] > array[i]) { + min = i; + } + } + return array[min]; + } + + static double select(int k, double[] array) { + // This is basically a copy of com.google.math.Rank#select, with a small change in the method + // signature: we make k 0-based rather than 1-based; and we drop from and to, and always work on + // the whole array. + int from = 0; + int to = array.length - 1; + + while (true) { + if (to <= from + 1) { + // Two or less elements left. + if (to == from + 1 && array[to] < array[from]) { + // Exactly two elements left. + swap(array, from, to); + } + return array[k]; + } else { + int midIndex = (from + to) >>> 1; + // Choose the median of the elements at the from, to and mid indexes, + // and rearrange so that array[from]<=array[from+1], and + // array[to] => array[from + 1]. + + swap(array, midIndex, from + 1); + + if (array[from] > array[to]) { + swap(array, from, to); + } + if (array[from + 1] > array[to]) { + swap(array, from + 1, to); + } + if (array[from] > array[from + 1]) { + swap(array, from, from + 1); + } + + // Perform a partition with the selected median. + int low = from + 1, high = to; // Indexes for partitioning. + double partition = array[from + 1]; // Choose partitioning element. + while (true) { + // Skip the elements smaller than the partition. + do { + low++; + } while (array[low] < partition); + + // Skip the elements larger than the partition. + do { + high--; + } while (array[high] > partition); + if (high < low) { + break; // Pointers crossed. Partitioning complete. + } + swap(array, low, high); // End of innermost loop. + } + array[from + 1] = array[high]; // Insert partitioning element. + array[high] = partition; + + // Continue the partition that contains the kth element. + if (high >= k) { + to = high - 1; + } + if (high <= k) { + from = low; + } + } + } + } + + private static void swap(double[] array, int i, int j) { + // This is a copy of com.google.math.Rank#swap. + double temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +} diff --git a/guava-testlib/src/module-info.java b/guava-testlib/src/module-info.java index ee0cf879b3e8..51437a19dea7 100644 --- a/guava-testlib/src/module-info.java +++ b/guava-testlib/src/module-info.java @@ -27,5 +27,6 @@ exports com.google.common.collect.testing.testers; exports com.google.common.escape.testing; exports com.google.common.testing; + exports com.google.common.testing.math; exports com.google.common.util.concurrent.testing; } diff --git a/guava-tests/pom.xml b/guava-tests/pom.xml index 50a5a9ffe2eb..7010a508a4bb 100644 --- a/guava-tests/pom.xml +++ b/guava-tests/pom.xml @@ -111,23 +111,6 @@ false - - org.codehaus.mojo - build-helper-maven-plugin - 1.7 - - - add-benchmark-sources - generate-test-sources - add-test-source - - - benchmark - - - - - diff --git a/guava-tests/test/com/google/common/math/QuantilesAlgorithm.java b/guava-tests/test/com/google/common/math/QuantilesAlgorithm.java index ddb2064e20b6..f81d3fdf0e17 100644 --- a/guava-tests/test/com/google/common/math/QuantilesAlgorithm.java +++ b/guava-tests/test/com/google/common/math/QuantilesAlgorithm.java @@ -24,7 +24,7 @@ /** * Enumerates several algorithms providing equivalent functionality to {@link Quantiles}, for use in - * {@link QuantilesBenchmark}. These algorithms each calculate either a single quantile or multiple + * the Quantiles Benchmark. These algorithms each calculate either a single quantile or multiple * quantiles. All algorithms modify the dataset they are given (the cost of a copy to avoid this * will be constant across algorithms). * diff --git a/guava-tests/test/com/google/common/math/QuantilesAlgorithmTest.java b/guava-tests/test/com/google/common/math/QuantilesAlgorithmTest.java index 87a962a61299..49b972cd0659 100644 --- a/guava-tests/test/com/google/common/math/QuantilesAlgorithmTest.java +++ b/guava-tests/test/com/google/common/math/QuantilesAlgorithmTest.java @@ -27,7 +27,7 @@ import junit.framework.TestCase; /** - * Tests that the different algorithms benchmarked in {@link QuantilesBenchmark} are actually all + * Tests that the different algorithms benchmarked in the Quantiles Benchmark are actually all * returning more-or-less the same answers. */ public class QuantilesAlgorithmTest extends TestCase { diff --git a/pom.xml b/pom.xml index df33a13550a1..b061e315bfd1 100644 --- a/pom.xml +++ b/pom.xml @@ -90,6 +90,7 @@ guava-gwt guava-testlib guava-tests + guava-bench @@ -304,6 +305,11 @@ maven-jacoco-plugin 0.8.11 + + org.codehaus.mojo + exec-maven-plugin + 3.2.0 +