Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #3275

Merged
merged 16 commits into from
Jan 13, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.alibaba.fastjson2.benchmark.jjb;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.runtime.Settings;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
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.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

public class ClientsWriteCNBytes {
static Clients clients;
static final ObjectMapper mapper = new ObjectMapper();
static final Gson gson = new Gson();
static final DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime().includeServiceLoader());
static final ThreadLocal<ByteArrayOutputStream> bytesOutLocal = ThreadLocal.withInitial(() -> new ByteArrayOutputStream());

static {
try {
InputStream is = ClientsWriteCNBytes.class.getClassLoader().getResourceAsStream("data/jjb/client_cn.json");
String str = IOUtils.toString(is, "UTF-8");
clients = JSONReader.of(str)
.read(Clients.class);
} catch (Throwable ex) {
ex.printStackTrace();
}
}

@Benchmark
public void fastjson2(Blackhole bh) {
bh.consume(JSON.toJSONBytes(clients));
}

@Benchmark
public void wast(Blackhole bh) {
bh.consume(io.github.wycst.wast.json.JSON.toJsonBytes(clients));
}

public void jsonb(Blackhole bh) {
bh.consume(JSONB.toBytes(clients));
}

public void jsonb_beanToArray(Blackhole bh) {
bh.consume(JSONB.toBytes(clients, JSONWriter.Feature.BeanToArray, JSONWriter.Feature.FieldBased));
}

public void fastjson2_str(Blackhole bh) {
bh.consume(JSON.toJSONString(clients));
}

@Benchmark
public void dsljson(Blackhole bh) throws IOException {
ByteArrayOutputStream bytesOut = bytesOutLocal.get();
bytesOut.reset();
dslJson.serialize(clients, bytesOut);
byte[] bytes = bytesOut.toByteArray();
bh.consume(bytes);
}

// @Benchmark
public void jackson(Blackhole bh) throws Exception {
bh.consume(mapper.writeValueAsBytes(clients));
}

// @Benchmark
public void gson(Blackhole bh) throws Exception {
bh.consume(gson
.toJson(clients)
.getBytes(StandardCharsets.UTF_8)
);
}

public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(ClientsWriteCNBytes.class.getName())
.mode(Mode.Throughput)
.timeUnit(TimeUnit.MILLISECONDS)
.warmupIterations(3)
.forks(2)
.threads(16)
.build();
new Runner(options).run();
}
}
1 change: 1 addition & 0 deletions benchmark/src/main/resources/data/jjb/client_cn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"clients":[{"about":"中文yH1fix9pXbKEh5fJuly1","address":"中文Nljq0lukStDonHEcVRHE","age":64,"balance":0.44240027590133413,"company":"中文FgeVau3mS5w9mhcWwuKS","emails":["MfNsDmpsUsXpnDzvrcKm","jAyzPlasKbeYAvTdClOi","FODRICnLkoeXkifYBDZW","AIGvokIjTNJjgRQlcGAG","HvwnRvNkYdlVCrbBBZkD","EoaXVUulXKvnguaSuqrz","MjTFZwEgvEbGexOLOUCe","VFXuEiWfszLdGjWpVyXY"],"eyeColor":"BLUE","gender":"MrJ369tihTC7lZNK5bPo","guid":"70a95e90-425e-f2a7-db42-b83c18442df3","id":5917278237879270907,"index":500779379,"isActive":true,"latitude":3.237861088027525,"longitude":28.303947340611323,"name":"pVwnJvTLvrlferKSaG9o","partners":[{"id":-4992443277785453447,"name":"QvqPmFvFeLNggovkrLJLTHMehPHXFv","since":"1917-12-23T06:56:19.5013299Z"},{"id":7414865681841083540,"name":"lomuqksUvvBkkdyOUOfYVDopkTQGMK","since":"1983-12-24T09:34:01.284158669Z"},{"id":8483963072105131018,"name":"LOcRXNwppbCbrKtbnZlEFDFoNaHfAm","since":"1973-06-09T03:42:43.661968037Z"},{"id":-2059367369353171635,"name":"KQZVaVAxxaagquXdlVqaEbirVREPrc","since":"1914-04-26T17:12:57.205244091Z"},{"id":216128505382995309,"name":"bQpPRcRPsmnXSUApCtLoeIoWLFcylD","since":"1925-03-25T00:02:16.950150228Z"},{"id":-6331778364589206153,"name":"TpRJOziKUAgGJlbeleTcHwDgagmits","since":"1987-09-13T01:20:47.997888071Z"},{"id":-3449909979167971348,"name":"wCAsmHghlJNNONapFCDDiDDsixNKTu","since":"1982-10-25T04:05:34.720262937Z"},{"id":-5177986292895188953,"name":"dYhtDCbappBRjNHmUZOFpNyeglbwiw","since":"1919-10-14T10:28:31.669682461Z"},{"id":6239205551293135613,"name":"qUmJvesyiWMIZzBNLIOCjGkLaUMavd","since":"1940-10-14T04:47:37.631870618Z"},{"id":8851381720745793026,"name":"RsYbNUldwNFbAkxIYlemjptpecueDz","since":"1923-03-01T03:26:34.449016475Z"}],"phones":[1554466975,1263898911],"picture":"pVJdkPiSIno4Lr4n6T48xftyqIVnbuOWFkISpRY5gJAS0HDGfNM4Xs5Aw7L0yMS9Pu39coV6V6M11MXspNSMLoBO17CU4pmnrP4X","registered":"1930-06-24","tags":["1LGhuAhMHT","d2CMWFnmKs","I5Kz2sl3ea","nEjMMcLHOG","Wf5vWHhffW","3o5XjSt8bK","403BW3jTeF","Yqo14HhTmq","tOK5V9eaM3","EEFvDelYvP","LAmXdWfKw7","5xZCukg63f","muqvClzcXO","HEVG0HHKXn","5EdKtQZX6D","nVNmXvckwP","5d940ocGOK","7EUFIBtKW0","TGL1fImxEw","K9TnndP7Or","aA8GoXyO7W","a3RMQL8a51","7Y7RyeHXVs"]}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.alibaba.fastjson2.benchmark.jjb;

import static com.alibaba.fastjson2.benchmark.JMH.BH;

public class ClientsWriteCNBytesTest {
static final ClientsWriteCNBytes benchmark = new ClientsWriteCNBytes();

public static void jsonb() {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.jsonb(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("ClientsWriteUTF8Bytes-jsonb millis : " + millis);
// zulu17.40.19 : 609
// zulu17.40.19_vec :
}
}

public static void jsonb_beanToArray() {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.jsonb_beanToArray(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("ClientsWriteUTF8Bytes-jsonb_beanToArray millis : " + millis);
// zulu17.40.19 : 311 303
// zulu17.40.19_vec :
}
}

public static void fastjson2() {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.fastjson2(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("ClientsWriteUTF8Bytes-fastjson2 millis : " + millis);
// zulu8.70.0.23 :
// zulu17.40.19 : 1027
// zulu17.40.19_vec :
// zulu17.40.19_reflect :
}
}

public static void wast() {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.wast(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("ClientsWriteUTF8Bytes-wast millis : " + millis);
// zulu8.70.0.23 :
// zulu17.40.19 : 1364
// zulu17.40.19_vec :
// zulu17.40.19_reflect :
}
}

public static void fastjson2_str() {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.fastjson2_str(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("ClientsWriteUTF8Bytes-fastjson2 millis : " + millis);
// zulu17.40.19 :
// zulu17.40.19_vec : 1139 1313 1307 1206
}
}

public static void dsljson() throws Exception {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.dsljson(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("ClientsWriteUTF8Bytes-dsljson millis : " + millis);
// zulu17.40.19 : 2169 1487
}
}

public static void jackson() throws Exception {
for (int j = 0; j < 10; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
benchmark.jackson(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("jackson millis : " + millis);
}
}

public static void main(String[] args) throws Exception {
fastjson2();
// wast();
// jsonb();
// jsonb_beanToArray();
// fastjson2_str();
// dsljson();
// jackson();
}
}
7 changes: 3 additions & 4 deletions core/src/main/java/com/alibaba/fastjson2/JSONBDump.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.fastjson2;

import com.alibaba.fastjson2.util.IOUtils;
import com.alibaba.fastjson2.util.JDKUtils;

import java.math.BigDecimal;
Expand Down Expand Up @@ -279,11 +280,9 @@ private void dumpAny() {
);
break;
case BC_INT64:
long unscaledValueLong = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
unscaledValue = BigInteger.valueOf(
BIG_ENDIAN
? unscaledValueLong
: Long.reverseBytes(unscaledValueLong));
IOUtils.getLongBE(bytes, offset)
);
offset += 8;
break;
default:
Expand Down
Loading
Loading