Skip to content

Instantly share code, notes, and snippets.

@shijinkui
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save shijinkui/6ac07111db4a0ab98fd8 to your computer and use it in GitHub Desktop.

Select an option

Save shijinkui/6ac07111db4a0ab98fd8 to your computer and use it in GitHub Desktop.
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.MapSerializer;
import com.google.common.io.Files;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
/**
* benchmark
* Created by 玄畅 on 16/10/14.
*/
public class KryoSampleBeanch {
private static final String path = "/Users/sjk/tmp/serial";
private static final File statFile = new File(path + "/stat");
private static final AtomicInteger hot = new AtomicInteger(0);
public static void main(String[] args) throws IOException, ClassNotFoundException {
for (int i = 0; i < 100000; i++) {
hot.addAndGet(i);
}
System.out.println(hot.get());
final boolean clearDir = false;
final int number = 10000;
for (int i = 0; i < 10; i++) {
System.out.println("====" + i + "," + number + " objects");
testKryo(number, clearDir);
javaSerial(number, clearDir);
}
}
private static void testKryo(int number, boolean bool) throws IOException {
StringBuilder sf = new StringBuilder();
Kryo kryo = new Kryo();
kryo.register(SampleBean.class);
kryo.register(ArrayList.class, new CollectionSerializer());
kryo.register(ConcurrentHashMap.class, new MapSerializer());
kryo.register(byte[].class);
kryo.setRegistrationRequired(false);
String file = path + "/kryo/file_%d.bin";
long ct = System.nanoTime();
// SampleBean obj = new SampleBean();
// byte[] bytes = new byte[1024 * 1024];
// obj.setBytes(bytes);
for (int i = 0; i < number; i++) {
OutputStream outputStream = new DeflaterOutputStream(new FileOutputStream(String.format(file, i)));
Output output = new Output(outputStream);
// Output output = new Output(new FileOutputStream(String.format(file, i)));
SampleBean obj = new SampleBean();
obj.setId(i).setTime().fillList().fillMap();
kryo.writeClassAndObject(output, obj);
output.close();
}
long cost = (System.nanoTime() - ct) / number;
System.out.println("kryo serial cost:" + cost);
sf.append("kryo ser=").append(cost).append("\r\n");
ct = System.nanoTime();
for (int i = 0; i < number; i++) {
InputStream inputStream = new InflaterInputStream(new FileInputStream(String.format(file, i)));
Input input = new Input(inputStream);
// Input input = new Input(new FileInputStream(String.format(file, i)));
SampleBean obj2 = (SampleBean) kryo.readClassAndObject(input);
obj2.getList();
input.close();
}
cost = (System.nanoTime() - ct) / number;
sf.append("kryo deser=").append(cost).append("\r\n");
System.out.println("kryo deSerial cost:" + cost + "ns");
reset(bool, "file_");
Files.append(sf.toString(), statFile, Charset.defaultCharset());
}
private static void javaSerial(int number, boolean bool) throws IOException, ClassNotFoundException {
StringBuilder sf = new StringBuilder();
String file = path + "/java/java_file_%d.bin";
long ct = System.nanoTime();
// byte[] bytes = new byte[1024 * 1024];
// obj.setBytes(bytes);
for (int i = 0; i < number; i++) {
SampleBean obj = new SampleBean();
obj.setId(i).setTime().fillList().fillMap();
FileOutputStream fos = new FileOutputStream(String.format(file, i));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.close();
}
long cost = (System.nanoTime() - ct) / number;
sf.append("java ser=").append(cost).append("\r\n");
System.out.println("java serial cost:" + cost);
ct = System.nanoTime();
for (int i = 0; i < number; i++) {
FileInputStream fis = new FileInputStream(String.format(file, i));
ObjectInputStream ois = new ObjectInputStream(fis);
SampleBean obj2 = (SampleBean) ois.readObject();
obj2.getList();
ois.close();
}
cost = (System.nanoTime() - ct) / number;
sf.append("java deser=").append(cost).append("\r\n");
System.out.println("java deSerial cost:" + cost);
reset(bool, "java_file_");
Files.append(sf.toString(), statFile, Charset.defaultCharset());
}
private static void reset(boolean bool, final String prefix) {
if (bool) {
File l = new File(path);
File[] list = l.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.contains(prefix);
}
});
assert list != null;
for (File f : list) {
f.deleteOnExit();
}
}
}
}
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.*;
/**
* bean
* Created by 玄畅 on 16/10/14.
*/
public class SampleBean implements Serializable {
private int id = 0;
private Timestamp time = new Timestamp(System.currentTimeMillis());
private final long nanoTime = System.nanoTime();
private Map<Integer, Integer> map = new HashMap<Integer, Integer>();
private final List<String> list = new ArrayList<String>();
// private byte[] bytes;
public Map<Integer, Integer> getMap() {
return map;
}
public void fillMap() {
for (int i = 0; i < 100; i++) {
map.put(i, i * i);
}
}
// public void setBytes(byte[] bytes) {
// this.bytes = bytes;
// }
// public byte[] getBytes() {
// return bytes;
// }
public List<String> getList() {
return list;
}
public SampleBean fillList() {
for (int i = 0; i < 100; i++) {
list.add("" + i);
}
return this;
}
public SampleBean setMap(Map<Integer, Integer> map) {
this.map = map;
return this;
}
public int getId() {
return id;
}
public SampleBean setId(int id) {
this.id = id;
return this;
}
public Timestamp getTime() {
return time;
}
public SampleBean setTime() {
this.time = new Timestamp(System.currentTimeMillis());
return this;
}
public void setTime(Timestamp time) {
this.time = time;
}
public long getNanoTime() {
return nanoTime;
}
public String toString() {
return id + "|" + time + "|" + nanoTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment