Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Sep 5, 2023
1 parent a037d80 commit 5abf262
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.alibaba.fastjson2.benchmark.fastcode;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class DateToString {
static final Date date = new Date(1693836447048L);

@Benchmark
public void dateToString(Blackhole bh) throws Throwable {
bh.consume(date.toString());
}

public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(DateToString.class.getName())
.mode(Mode.Throughput)
.timeUnit(TimeUnit.MILLISECONDS)
.warmupIterations(1)
.forks(1)
.build();
new Runner(options).run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.alibaba.fastjson2.benchmark.fastcode;

import org.openjdk.jmh.infra.Blackhole;

public class DateToStringTest {
static final Blackhole BH = new Blackhole("Today's password is swordfish. I understand instantiating Blackholes directly is dangerous.");
static final DateToString benchmark = new DateToString();
static final int COUNT = 100_000_000;

public static void dateToString() throws Throwable {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < COUNT; ++i) {
benchmark.dateToString(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("DateToString-dateToString millis : " + millis);
// zulu8.58.0.13 :
// zulu11.52.13 :
// zulu17.38.21 :
// jdk22-ea : 2495
// jdk22-baseline : 5669
}
}

public static void main(String[] args) throws Throwable {
dateToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.alibaba.fastjson2.fastcode;

import org.junit.jupiter.api.Test;

import java.util.Date;

public class DateTest {
@Test
public void test() {
Date date = new Date();
String str = date.toString();
char[] chars = str.toCharArray();
System.out.println(str);
}
}

0 comments on commit 5abf262

Please sign in to comment.