论坛首页 Java企业应用论坛

Fastjson技术内幕

浏览 87707 次
该帖已经被评为精华帖
作者 正文
   发表时间:2011-09-06  
期待温少给我们带来的下一个惊喜,赞
0 请登录后投票
   发表时间:2011-09-06  
JSON.toJSONString(obj);
有内存溢出BUG
0 请登录后投票
   发表时间:2011-09-08  
>1、自行编写类似StringBuilder的工具类SerializeWriter。
没试试Rope?
0 请登录后投票
   发表时间:2011-09-14  
fastjson不能在play!框架里使用,说什么class not found exception.
0 请登录后投票
   发表时间:2011-09-14  
Arden 写道
fastjson不能在play!框架里使用,说什么class not found exception.


我对play!框架不了解,是否能够把错误堆栈信息贴出来?
0 请登录后投票
   发表时间:2011-09-16  
我觉得应该加上JSON.writeValueAsBytes(),JSON.parseObject(Byte[] b)等字节处理方法,网络传输中以字节处理的比较多,而且byte后还可以gzip.
0 请登录后投票
   发表时间:2011-09-16  
whzhaha 写道
我觉得应该加上JSON.writeValueAsBytes(),JSON.parseObject(Byte[] b)等字节处理方法,网络传输中以字节处理的比较多,而且byte后还可以gzip.


目前就有啊
0 请登录后投票
   发表时间:2011-11-01  
不知道为什么我的测试结果fastjson的反序列化效率最高,但是序列化效率却远远低于jackson和gson,附上我的代码:

序列化的对象CustomerResult
public class CustomerResult  {
	private String result;	
	private int size;	
	private List<Customer> detail;
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public List<Customer> getDetail() {
		return detail;
	}
	public void setDetail(List<Customer> detail) {
		this.detail = detail;
	}
}


实现类:
public class FastJsonImpl implements JsonHelper {

	@Override
	public Object decode(String json, Class pojoClass) {
		return JSON.parseObject(json, pojoClass);
	}

	@Override
	public String encode(Object o) {
		SerializeConfig sc = new SerializeConfig();
		sc.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
		return JSON.toJSONString(o,sc);
	}
}


测试代码:
List l=new ArrayList();
		for(int i=0;i<records;i++){
			Customer c=new Customer();
			c.setName(Str.randomString(18));
			//c.setName("测试中文显示"+Str.randomString(7));
			c.setBirthDate(new Date());
			c.setGender(i);		
			l.add(c);
		}

		CustomerResult r=new CustomerResult();
		r.setDetail(l);
		r.setResult("ok");
		r.setSize(l.size());
		
		System.out.println("json encode .......");		
		long encodeCost=0,decodeCost=0;
		for(int i=0;i<count;i++){
			long start=System.currentTimeMillis();
			String json=test.encode(r);			
			//System.out.println(json);
			long end=System.currentTimeMillis();
			encodeCost+=(end-start);
			
			if(onlyEncode==0){
			    start=System.currentTimeMillis();
				test.decode(json,CustomerResult.class);
				end=System.currentTimeMillis();
				decodeCost+=(end-start);
			}
			
		}
		System.out.println("json序列化时间:"+(encodeCost)+"ms,反序列化时间"+decodeCost);

0 请登录后投票
   发表时间:2011-11-02   最后修改:2011-11-02
做了个fastJson与gson对比的压力测试,fastJson性能表现很不错:

Gson:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Document Length:        10304 bytes

Concurrency Level:      400
Time taken for tests:   27.965457 seconds
Complete requests:      40000
Failed requests:        0
Write errors:           0
Total transferred:      414480000 bytes
HTML transferred:       412160000 bytes
Requests per second:    1430.34 [#/sec] (mean)
Time per request:       279.655 [ms] (mean)
Time per request:       0.699 [ms] (mean, across all concurrent requests)
Transfer rate:          14473.75 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    7 146.8      0    3000
Processing:     7  270 213.4    255    2419
Waiting:        6  270 213.2    254    2419
Total:          8  277 257.8    255    3273

Percentage of the requests served within a certain time (ms)
  50%    255
  66%    260
  75%    263
  80%    265
  90%    272
  95%    280
  98%    292
  99%   2370
100%   3273 (longest request)


fastJson:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Document Length:        10304 bytes

Concurrency Level:      400
Time taken for tests:   8.41320 seconds
Complete requests:      40000
Failed requests:        0
Write errors:           0
Total transferred:      414480000 bytes
HTML transferred:       412160000 bytes
Requests per second:    4974.31 [#/sec] (mean)
Time per request:       80.413 [ms] (mean)
Time per request:       0.201 [ms] (mean, across all concurrent requests)
Transfer rate:          50335.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6 141.3      0    3000
Processing:     4   72  10.4     76      94
Waiting:        4   72  10.3     75      94
Total:          5   79 141.8     76    3083

Percentage of the requests served within a certain time (ms)
  50%     76
  66%     79
  75%     81
  80%     82
  90%     84
  95%     86
  98%     88
  99%     89
100%   3083 (longest request)

fastJson在我本机上的速度差不多是gson的10倍!
0 请登录后投票
   发表时间:2011-11-03  
比java序列化还是要差哦:
Json length:    1171
Json 耗时      :    398012nm
Java length:    867
Java 耗时      :    2499nm

public static void main(String[] args) throws Exception {

Usr usr = getUsr();

ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outputstream);

String jsonUsr = null;
jsonUsr = JSON.toJSONString(usr);
jsonUsr = JSON.toJSONString(usr);
jsonUsr = JSON.toJSONString(usr);
jsonUsr = JSON.toJSONString(usr);
jsonUsr = JSON.toJSONString(usr);
long jsonStart = System.nanoTime();
jsonUsr = JSON.toJSONString(usr);
long jsonEnd = System.nanoTime();
long jsonLimit = jsonEnd - jsonStart;
System.out.println(jsonUsr);

byte[] jsonBytes = jsonUsr.getBytes();

out.writeObject(usr);
long javaStart = System.nanoTime();
out.writeObject(usr);
long javaEnd = System.nanoTime();
byte[] javaBytes = outputstream.toByteArray();
long javaLimit = javaEnd - javaStart;

System.out.println("Json length:    " + jsonBytes.length);
System.out.println("Json 耗时      :    " + jsonLimit + "nm");
System.out.println("Java length:    " + javaBytes.length);
System.out.println("Java 耗时      :    " + javaLimit + "nm");
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics