Skip to content

Instantly share code, notes, and snippets.

@seveniu
Last active August 29, 2015 14:05
Show Gist options
  • Save seveniu/e54d71a946b53d0f8167 to your computer and use it in GitHub Desktop.
Save seveniu/e54d71a946b53d0f8167 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson.JSON;
import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* User: seveniu
* Date: 8/15/14
* Time: 6:21 PM
* Project: RaidenHelp
*/
public class MsgTest {
@Message
public static class Player {
public String name;
public int money;
public boolean status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
public static void main(String[] args) {
Player player = new Player();
player.money = 100;
player.name = "tom";
player.status = true;
Player player2 = new Player();
player2.money = 200;
player2.name = "lily";
player2.status = false;
Player player3 = new Player();
player3.money = 300;
player3.name = "may";
player3.status = true;
List<Player> players = new ArrayList<>();
players.add(player);
players.add(player2);
players.add(player3);
players.add(player3);
players.add(player3);
players.add(player3);
players.add(player3);
players.add(player3);
players.add(player3);
players.add(player3);
players.add(player3);
MessagePack msgpack = new MessagePack();
ObjectMapper jackson = new ObjectMapper();
try {
String json = JSON.toJSONString(players);
String jason = jackson.writeValueAsString(players);
byte[] bytes = msgpack.write(players);
System.out.println("msgpack length:" + bytes.length);
System.out.println("json length:" + json.getBytes().length);
System.out.println("jackson length:" + jason.getBytes().length);
long time0 = System.currentTimeMillis();
int count = 100000;
for (int i = 0; i < count; i++) {
JSON.toJSONString(players, SerializerFeature.DisableCircularReferenceDetect);
}
System.out.println("write " + count + " times fastjson :" + (System.currentTimeMillis() - time0));
time0 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
msgpack.write(players);
}
System.out.println("write " + count + " times msgpack :" + (System.currentTimeMillis() - time0));
time0 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
jackson.writeValueAsString(players);
}
System.out.println("write " + count + " times jackson :" + (System.currentTimeMillis() - time0));
///////////////////////////// read /////////////////////////////
time0 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
JSON.parseObject(json,new com.alibaba.fastjson.TypeReference<List<Player>>(){});
}
System.out.println("read " + count + " times fastjson :" + (System.currentTimeMillis() - time0));
time0 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
msgpack.read(bytes, new AbstractListTemplate<Player>() {
});
}
System.out.println("read " + count + " times msgpack :" + (System.currentTimeMillis() - time0));
time0 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
jackson.readValue(jason, new TypeReference<List<Player>>() {
});
}
System.out.println("read " + count + " times jackson :" + (System.currentTimeMillis() - time0));
} catch (IOException e) {
e.printStackTrace();
}
}
}
result:
==============================================
msgpack length:98
json length:254
jackson length:454
write 100000 times fastjson :402
write 100000 times msgpack :473
write 100000 times jackson :594
read 100000 times fastjson :1120
read 100000 times msgpack :901
read 100000 times jackson :1624
==============================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment