|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common; |
| 21 | + |
| 22 | +import org.elasticsearch.common.time.DateFormatter; |
| 23 | +import org.elasticsearch.common.unit.TimeValue; |
| 24 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; |
| 25 | +import org.openjdk.jmh.annotations.Benchmark; |
| 26 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 27 | +import org.openjdk.jmh.annotations.Fork; |
| 28 | +import org.openjdk.jmh.annotations.Measurement; |
| 29 | +import org.openjdk.jmh.annotations.Mode; |
| 30 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Param; |
| 32 | +import org.openjdk.jmh.annotations.Scope; |
| 33 | +import org.openjdk.jmh.annotations.Setup; |
| 34 | +import org.openjdk.jmh.annotations.State; |
| 35 | +import org.openjdk.jmh.annotations.Warmup; |
| 36 | +import org.openjdk.jmh.infra.Blackhole; |
| 37 | + |
| 38 | +import java.time.ZoneId; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | +import java.util.function.Supplier; |
| 41 | + |
| 42 | +@Fork(2) |
| 43 | +@Warmup(iterations = 10) |
| 44 | +@Measurement(iterations = 5) |
| 45 | +@BenchmarkMode(Mode.AverageTime) |
| 46 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 47 | +@State(Scope.Benchmark) |
| 48 | +public class RoundingBenchmark { |
| 49 | + private static final DateFormatter FORMATTER = DateFormatter.forPattern("date_optional_time"); |
| 50 | + |
| 51 | + @Param({ |
| 52 | + "2000-01-01 to 2020-01-01", // A super long range |
| 53 | + "2000-10-01 to 2000-11-01", // A whole month which is pretty believable |
| 54 | + "2000-10-29 to 2000-10-30", // A date right around daylight savings time. |
| 55 | + "2000-06-01 to 2000-06-02" // A date fully in one time zone. Should be much faster than above. |
| 56 | + }) |
| 57 | + public String range; |
| 58 | + |
| 59 | + @Param({ "java time", "es" }) |
| 60 | + public String rounder; |
| 61 | + |
| 62 | + @Param({ "UTC", "America/New_York" }) |
| 63 | + public String zone; |
| 64 | + |
| 65 | + @Param({ "calendar year", "calendar hour", "10d", "5d", "1h" }) |
| 66 | + public String interval; |
| 67 | + |
| 68 | + @Param({ "1", "10000", "1000000", "100000000" }) |
| 69 | + public int count; |
| 70 | + |
| 71 | + private long min; |
| 72 | + private long max; |
| 73 | + private long[] dates; |
| 74 | + private Supplier<Rounding.Prepared> rounderBuilder; |
| 75 | + |
| 76 | + @Setup |
| 77 | + public void buildDates() { |
| 78 | + String[] r = range.split(" to "); |
| 79 | + min = FORMATTER.parseMillis(r[0]); |
| 80 | + max = FORMATTER.parseMillis(r[1]); |
| 81 | + dates = new long[count]; |
| 82 | + long date = min; |
| 83 | + long diff = (max - min) / dates.length; |
| 84 | + for (int i = 0; i < dates.length; i++) { |
| 85 | + if (date >= max) { |
| 86 | + throw new IllegalStateException("made a bad date [" + date + "]"); |
| 87 | + } |
| 88 | + dates[i] = date; |
| 89 | + date += diff; |
| 90 | + } |
| 91 | + Rounding.Builder roundingBuilder; |
| 92 | + if (interval.startsWith("calendar ")) { |
| 93 | + roundingBuilder = Rounding.builder( |
| 94 | + DateHistogramAggregationBuilder.DATE_FIELD_UNITS.get(interval.substring("calendar ".length())) |
| 95 | + ); |
| 96 | + } else { |
| 97 | + roundingBuilder = Rounding.builder(TimeValue.parseTimeValue(interval, "interval")); |
| 98 | + } |
| 99 | + Rounding rounding = roundingBuilder.timeZone(ZoneId.of(zone)).build(); |
| 100 | + switch (rounder) { |
| 101 | + case "java time": |
| 102 | + rounderBuilder = rounding::prepareJavaTime; |
| 103 | + break; |
| 104 | + case "es": |
| 105 | + rounderBuilder = () -> rounding.prepare(min, max); |
| 106 | + break; |
| 107 | + default: |
| 108 | + throw new IllegalArgumentException("Expectd rounder to be [java time] or [es]"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Benchmark |
| 113 | + public void round(Blackhole bh) { |
| 114 | + Rounding.Prepared rounder = rounderBuilder.get(); |
| 115 | + for (int i = 0; i < dates.length; i++) { |
| 116 | + bh.consume(rounder.round(dates[i])); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Benchmark |
| 121 | + public void nextRoundingValue(Blackhole bh) { |
| 122 | + Rounding.Prepared rounder = rounderBuilder.get(); |
| 123 | + for (int i = 0; i < dates.length; i++) { |
| 124 | + bh.consume(rounder.nextRoundingValue(dates[i])); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments