Skip to content

Commit 9db0a6d

Browse files
committed
add benchmark
1 parent ee9d553 commit 9db0a6d

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2025, Alibaba Group Holding Limited. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.bench.java.time.format;
24+
25+
import java.time.Duration;
26+
import java.time.Instant;
27+
import java.time.LocalDate;
28+
import java.time.LocalDateTime;
29+
import java.time.LocalTime;
30+
import java.time.OffsetDateTime;
31+
import java.time.ZonedDateTime;
32+
import java.time.format.DateTimeFormatter;
33+
import java.time.temporal.ChronoUnit;
34+
35+
import java.util.Locale;
36+
import java.util.Random;
37+
import java.util.TimeZone;
38+
import java.util.concurrent.TimeUnit;
39+
import java.util.stream.IntStream;
40+
import java.util.stream.Stream;
41+
42+
import org.openjdk.jmh.annotations.Benchmark;
43+
import org.openjdk.jmh.annotations.BenchmarkMode;
44+
import org.openjdk.jmh.annotations.Fork;
45+
import org.openjdk.jmh.annotations.Measurement;
46+
import org.openjdk.jmh.annotations.Mode;
47+
import org.openjdk.jmh.annotations.OutputTimeUnit;
48+
import org.openjdk.jmh.annotations.Param;
49+
import org.openjdk.jmh.annotations.Scope;
50+
import org.openjdk.jmh.annotations.Setup;
51+
import org.openjdk.jmh.annotations.State;
52+
import org.openjdk.jmh.annotations.Warmup;
53+
import org.openjdk.jmh.infra.Blackhole;
54+
55+
@BenchmarkMode(Mode.Throughput)
56+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
57+
@Warmup(iterations = 5, time = 1)
58+
@Measurement(iterations = 5, time = 1)
59+
@Fork(3)
60+
@State(Scope.Thread)
61+
public class DateTimeFormatterParse {
62+
static final DateTimeFormatter formatterLocalTime = DateTimeFormatter.ofPattern("HH:mm:ss");
63+
static final DateTimeFormatter formatterLocalTimeWithNano = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
64+
static final DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
65+
static final DateTimeFormatter formatterLocalDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
66+
static final DateTimeFormatter formatterLocalDateTimeWithNano = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
67+
static final DateTimeFormatter formatterOffsetDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ");
68+
static final DateTimeFormatter formatterZonedDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ'['VV']'");
69+
70+
static final String STR_LOCALTIME = "21:11:48";
71+
static final String STR_LOCALTIME_WITH_NANO = "21:11:48.456";
72+
static final String STR_LOCALDATE = "2024-08-01";
73+
static final String STR_LOCALDATETIME = "2024-08-01T21:11:48";
74+
static final String STR_LOCALDATETIME_WITH_NANO = "2024-08-01T21:11:48.456";
75+
static final String STR_INSTANT = "2024-08-12T03:25:54.980339Z";
76+
static final String STR_OFFSETDATETIME = "2024-08-12T11:50:46.731509+08:00";
77+
static final String STR_ZONEDDATETIME = "2024-08-12T11:50:46.731509+08:00[Asia/Shanghai]";
78+
79+
@Benchmark
80+
public LocalTime parseLocalTime() {
81+
return LocalTime.parse(STR_LOCALTIME, formatterLocalTime);
82+
}
83+
84+
@Benchmark
85+
public LocalTime parseLocalTimeWithNano() {
86+
return LocalTime.parse(STR_LOCALTIME_WITH_NANO, formatterLocalTimeWithNano);
87+
}
88+
89+
@Benchmark
90+
public LocalDate parseLocalDate() {
91+
return LocalDate.parse(STR_LOCALDATE, formatterLocalDate);
92+
}
93+
94+
@Benchmark
95+
public LocalDateTime parseLocalDateTime() {
96+
return LocalDateTime.parse(STR_LOCALDATETIME, formatterLocalDateTime);
97+
}
98+
99+
@Benchmark
100+
public LocalDateTime parseLocalDateTimeWithNano() {
101+
return LocalDateTime.parse(STR_LOCALDATETIME_WITH_NANO, formatterLocalDateTimeWithNano);
102+
}
103+
104+
@Benchmark
105+
public OffsetDateTime parseOffsetDateTime() {
106+
return OffsetDateTime.parse(STR_OFFSETDATETIME, formatterOffsetDateTime);
107+
}
108+
109+
@Benchmark
110+
public ZonedDateTime parseZonedDateTime() {
111+
return ZonedDateTime.parse(STR_ZONEDDATETIME, formatterZonedDateTime);
112+
}
113+
114+
@Benchmark
115+
public Instant parseInstant() {
116+
return Instant.parse(STR_INSTANT);
117+
}
118+
}

0 commit comments

Comments
 (0)