alibaba / alibaba/fastjson2

[QUESTION]某些场景下json字符串转为json对象性能比fastjson1差

Open
#2,418 0 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

Int100是官方测试文档中字段数为100、类型为Int的类,在本地测试发现fastjson1的运行时长反而小于fastjson2的运行时长,请问这是什么原因
// java对象序列化为字符串
@Test
public void testFastjson1JsonObjectToString() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
int num = 100000;
// 对象
Int100 int100 = new Int100();
long start = System.currentTimeMillis();
long startCpuTime = threadMXBean.getCurrentThreadCpuTime();

for (int i = 0; i < num; i++) {
// 循环计算处理时间
String jsonString = JSON.toJSONString(int100);
}

long end = System.currentTimeMillis();
long endCpuTime = threadMXBean.getCurrentThreadCpuTime();

long elapsedTime = end - start;
long elapsedCpuTime = endCpuTime - startCpuTime;

// cpu时间单位为纳秒,1纳秒 = 1000000毫秒
double cpuUsage = (elapsedCpuTime / (elapsedTime * 1000000.0)) * 100.0;
// 计算时间
System.out.println("运行时间:" + elapsedTime);
System.out.println("CPU 占用率: " + cpuUsage + "%");
}

// java对象序列化为字符串
@Test
public void testFastjson2JsonObjectToString() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
int num = 100000;
// 对象
Int100 int100 = new Int100();
long start = System.currentTimeMillis();
long startCpuTime = threadMXBean.getCurrentThreadCpuTime();

for (int i = 0; i < num; i++) {
// 循环计算处理时间
String jsonString = com.alibaba.fastjson2.JSON.toJSONString(int100);
}

long end = System.currentTimeMillis();
long endCpuTime = threadMXBean.getCurrentThreadCpuTime();

long elapsedTime = end - start;
long elapsedCpuTime = endCpuTime - startCpuTime;

double cpuUsage = (elapsedCpuTime / (elapsedTime * 1000000.0)) * 100.0;

System.out.println("运行时间:" + elapsedTime);
System.out.println("CPU 占用率: " + cpuUsage + "%");
}

Contributor guide

Open the contributing guide

Research direction

Start with the two inline tests, testFastjson1JsonObjectToString and testFastjson2JsonObjectToString, and reproduce them with the referenced Int100 class under identical Java and library versions. Record comparable runtime and CPU measurements; done means the performance difference is explained or reduced to a reproducible benchmark result.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.