Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* 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.druid.java.util.common;

import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Compares {@link DateTimes#of(String)} (with the fast canonical-timestamp path added for #20927) against a frozen
* copy of the pre-optimization implementation, for both the canonical {@code used_status_last_updated} format and
* inputs that must fall back to the original lenient parser, to confirm the fallback is not slower than before.
*/
@State(Scope.Benchmark)
@Fork(value = 3)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
@BenchmarkMode({Mode.AverageTime})
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class DateTimesOfBenchmark
{
private List<String> canonicalTimestamps;
private List<String> nonCanonicalOffsetTimestamps;
private List<String> epochMillisTimestamps;

@Setup(Level.Trial)
public void setup()
{
canonicalTimestamps = new ArrayList<>();
nonCanonicalOffsetTimestamps = new ArrayList<>();
epochMillisTimestamps = new ArrayList<>();

final DateTime start = DateTimes.of("2025-01-01T00:00:00.000Z");
for (int i = 0; i < 1000; i++) {
final DateTime t = start.plusSeconds(i);
canonicalTimestamps.add(t.toString());
nonCanonicalOffsetTimestamps.add(t.withZone(DateTimes.inferTzFromString("+05:30")).toString());
epochMillisTimestamps.add(String.valueOf(t.getMillis()));
}
canonicalTimestamps = Collections.unmodifiableList(canonicalTimestamps);
nonCanonicalOffsetTimestamps = Collections.unmodifiableList(nonCanonicalOffsetTimestamps);
epochMillisTimestamps = Collections.unmodifiableList(epochMillisTimestamps);
}

@Benchmark
public void current_canonicalFormat(Blackhole blackhole)
{
for (String s : canonicalTimestamps) {
blackhole.consume(DateTimes.of(s));
}
}

@Benchmark
public void legacy_canonicalFormat(Blackhole blackhole)
{
for (String s : canonicalTimestamps) {
blackhole.consume(legacyOf(s));
}
}

@Benchmark
public void current_nonCanonicalOffsetFallback(Blackhole blackhole)
{
for (String s : nonCanonicalOffsetTimestamps) {
blackhole.consume(DateTimes.of(s));
}
}

@Benchmark
public void legacy_nonCanonicalOffsetFallback(Blackhole blackhole)
{
for (String s : nonCanonicalOffsetTimestamps) {
blackhole.consume(legacyOf(s));
}
}

@Benchmark
public void current_epochMillisFallback(Blackhole blackhole)
{
for (String s : epochMillisTimestamps) {
blackhole.consume(DateTimes.of(s));
}
}

@Benchmark
public void legacy_epochMillisFallback(Blackhole blackhole)
{
for (String s : epochMillisTimestamps) {
blackhole.consume(legacyOf(s));
}
}

/**
* Frozen copy of {@link DateTimes#of(String)} as it existed before #20927: identical except that it never
* attempts the fast canonical-timestamp path. Kept here only so this benchmark has a stable "before" baseline.
*/
private static DateTime legacyOf(String instant)
{
try {
return new DateTime(instant, ISOChronology.getInstanceUTC());
}
catch (IllegalArgumentException ex) {
try {
return new DateTime(Long.valueOf(instant), ISOChronology.getInstanceUTC());
}
catch (IllegalArgumentException ex2) {
throw ex;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* 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.druid.timeline;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.primitives.Ints;
import org.apache.druid.java.util.common.DateTimes;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Compares {@link SegmentId#tryParse} (with the fast canonical-timestamp path added for #20927) against a frozen
* copy of the pre-optimization implementation, which parsed every timestamp with the lenient
* {@link DateTimes#ISO_DATE_TIME} formatter.
*/
@State(Scope.Benchmark)
@BenchmarkMode({Mode.AverageTime})
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class SegmentIdParseBenchmark
{
private static final String DATA_SOURCE = "wikipedia";
private static final char DELIMITER = '_';
private static final Splitter DELIMITER_SPLITTER = Splitter.on(DELIMITER);
private static final Joiner DELIMITER_JOINER = Joiner.on(DELIMITER);

private List<String> idsWithExplicitPartition;
private List<String> idsWithoutExplicitPartition;
private List<String> idsWithUnderscoreInVersion;

@Setup(Level.Trial)
public void setup()
{
idsWithExplicitPartition = new ArrayList<>();
idsWithoutExplicitPartition = new ArrayList<>();
idsWithUnderscoreInVersion = new ArrayList<>();

final DateTime start = DateTimes.of("2025-01-01");
for (int i = 0; i < 1000; i++) {
final Interval interval = new Interval(start.plusDays(i), start.plusDays(i + 1));
idsWithExplicitPartition.add(SegmentId.of(DATA_SOURCE, interval, "2025-01-01T00:00:00.000Z", i + 1).toString());
idsWithoutExplicitPartition.add(SegmentId.of(DATA_SOURCE, interval, "2025-01-01T00:00:00.000Z", 0).toString());
idsWithUnderscoreInVersion.add(
SegmentId.of(DATA_SOURCE, interval, "2025-01-01T00:00:00.000Z_extra_suffix", i + 1).toString()
);
}
idsWithExplicitPartition = Collections.unmodifiableList(idsWithExplicitPartition);
idsWithoutExplicitPartition = Collections.unmodifiableList(idsWithoutExplicitPartition);
idsWithUnderscoreInVersion = Collections.unmodifiableList(idsWithUnderscoreInVersion);
}

@Benchmark
public void current_withExplicitPartition(Blackhole blackhole)
{
for (String id : idsWithExplicitPartition) {
blackhole.consume(SegmentId.tryParse(DATA_SOURCE, id));
}
}

@Benchmark
public void legacy_withExplicitPartition(Blackhole blackhole)
{
for (String id : idsWithExplicitPartition) {
blackhole.consume(legacyTryParse(DATA_SOURCE, id));
}
}

@Benchmark
public void current_withoutExplicitPartition(Blackhole blackhole)
{
for (String id : idsWithoutExplicitPartition) {
blackhole.consume(SegmentId.tryParse(DATA_SOURCE, id));
}
}

@Benchmark
public void legacy_withoutExplicitPartition(Blackhole blackhole)
{
for (String id : idsWithoutExplicitPartition) {
blackhole.consume(legacyTryParse(DATA_SOURCE, id));
}
}

@Benchmark
public void current_versionWithUnderscore(Blackhole blackhole)
{
for (String id : idsWithUnderscoreInVersion) {
blackhole.consume(SegmentId.tryParse(DATA_SOURCE, id));
}
}

@Benchmark
public void legacy_versionWithUnderscore(Blackhole blackhole)
{
for (String id : idsWithUnderscoreInVersion) {
blackhole.consume(legacyTryParse(DATA_SOURCE, id));
}
}

/**
* Frozen copy of {@link SegmentId#iteratePossibleParsingsWithDataSource} as it existed before #20927: identical
* in every respect except that it always calls the lenient {@link DateTimes#ISO_DATE_TIME} formatter directly,
* never the fast canonical-timestamp path. Kept here only so this benchmark has a stable "before" baseline to
* compare against, independent of future changes to the production method.
*/
private static SegmentId legacyTryParse(String dataSource, String segmentId)
{
if (!segmentId.startsWith(dataSource) || segmentId.charAt(dataSource.length()) != DELIMITER) {
return null;
}

String remaining = segmentId.substring(dataSource.length() + 1);
List<String> splits = DELIMITER_SPLITTER.splitToList(remaining);
if (splits.size() < 3) {
return null;
}

DateTime start;
DateTime end;
try {
start = DateTimes.ISO_DATE_TIME.parse(splits.get(0));
end = DateTimes.ISO_DATE_TIME.parse(splits.get(1));
}
catch (IllegalArgumentException e) {
return null;
}
if (start.compareTo(end) >= 0) {
return null;
}

String version = DELIMITER_JOINER.join(splits.subList(2, Math.max(splits.size() - 1, 3)));
String trail = splits.size() > 3 ? splits.get(splits.size() - 1) : null;
int partitionNum = 0;
if (trail != null) {
Integer possiblePartitionNum = Ints.tryParse(trail);
if (possiblePartitionNum != null && possiblePartitionNum > 0) {
partitionNum = possiblePartitionNum;
} else {
version = version + '_' + trail;
}
}
return SegmentId.of(dataSource, new Interval(start, end), version, partitionNum);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,65 @@ public static DateTime utc(long instant)

public static DateTime of(String instant)
{
if (instant != null
&& instant.length() == 24
&& instant.charAt(4) == '-'
&& instant.charAt(7) == '-'
&& instant.charAt(10) == 'T'
&& instant.charAt(13) == ':'
&& instant.charAt(16) == ':'
&& instant.charAt(19) == '.'
&& instant.charAt(23) == 'Z') {
final char c0 = instant.charAt(0);
final char c1 = instant.charAt(1);
final char c2 = instant.charAt(2);
final char c3 = instant.charAt(3);
final char c5 = instant.charAt(5);
final char c6 = instant.charAt(6);
final char c8 = instant.charAt(8);
final char c9 = instant.charAt(9);
final char c11 = instant.charAt(11);
final char c12 = instant.charAt(12);
final char c14 = instant.charAt(14);
final char c15 = instant.charAt(15);
final char c17 = instant.charAt(17);
final char c18 = instant.charAt(18);
final char c20 = instant.charAt(20);
final char c21 = instant.charAt(21);
final char c22 = instant.charAt(22);
if (c0 >= '0' && c0 <= '9'
&& c1 >= '0' && c1 <= '9'
&& c2 >= '0' && c2 <= '9'
&& c3 >= '0' && c3 <= '9'
&& c5 >= '0' && c5 <= '9'
&& c6 >= '0' && c6 <= '9'
&& c8 >= '0' && c8 <= '9'
&& c9 >= '0' && c9 <= '9'
&& c11 >= '0' && c11 <= '9'
&& c12 >= '0' && c12 <= '9'
&& c14 >= '0' && c14 <= '9'
&& c15 >= '0' && c15 <= '9'
&& c17 >= '0' && c17 <= '9'
&& c18 >= '0' && c18 <= '9'
&& c20 >= '0' && c20 <= '9'
&& c21 >= '0' && c21 <= '9'
&& c22 >= '0' && c22 <= '9') {
final int year = (c0 - '0') * 1000 + (c1 - '0') * 100 + (c2 - '0') * 10 + (c3 - '0');
final int month = (c5 - '0') * 10 + (c6 - '0');
final int day = (c8 - '0') * 10 + (c9 - '0');
final int hour = (c11 - '0') * 10 + (c12 - '0');
final int minute = (c14 - '0') * 10 + (c15 - '0');
final int second = (c17 - '0') * 10 + (c18 - '0');
final int millis = (c20 - '0') * 100 + (c21 - '0') * 10 + (c22 - '0');
try {
return new DateTime(year, month, day, hour, minute, second, millis, ISOChronology.getInstanceUTC());
}
catch (IllegalArgumentException e) {
// Out-of-range field (e.g. month 13): fall through below.
}
}
}

try {
return new DateTime(instant, ISOChronology.getInstanceUTC());
}
Expand Down
Loading
Loading