对比protostuff和JDK序列化性能测试

maven配置如下,导入protostuff


            com.dyuproject.protostuff
            protostuff-core
            1.0.8
        
        
            com.dyuproject.protostuff
            protostuff-runtime
            1.0.8
        
  • 测试代码
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import module.BaseModule;

public class Main {
    private static RuntimeSchema runtimeSchema = RuntimeSchema.createFrom(BaseModule.class);

    public Main() {
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException {
        String jdkFileName = "a.txt";
        String utilFileName = "b.txt";
        File fileA = new File(jdkFileName);
        if (!fileA.exists()) {
            fileA.createNewFile();
        }

        File fileB = new File(utilFileName);
        if (!fileB.exists()) {
            fileB.createNewFile();
        }

        long timeAFileWrite = System.currentTimeMillis();
        BaseModule module = new BaseModule(10, "'name'", "我是對象");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileA));
        objectOutputStream.writeObject(module);
        objectOutputStream.flush();
        objectOutputStream.close();
        long sumA = System.currentTimeMillis() - timeAFileWrite;
        System.out.println("jdk写入时间:" + sumA);
        long timeBFileWrite = System.currentTimeMillis();
        byte[] bytes = ProtostuffIOUtil.toByteArray(module, runtimeSchema, LinkedBuffer.allocate(512));
        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(new FileOutputStream(fileB));
        objectOutputStream1.write(bytes);
        objectOutputStream1.flush();
        objectOutputStream1.close();
        long sumB = System.currentTimeMillis() - timeBFileWrite;
        System.out.println("util写入时间" + sumB);
        long timeAFileRead = System.currentTimeMillis();
        ObjectInputStream readA = new ObjectInputStream(new FileInputStream(jdkFileName));
        BaseModule moduleA = (BaseModule)readA.readObject();
        readA.close();
        long sumReadA = System.currentTimeMillis() - timeAFileRead;
        System.out.println("jdk读取时间" + sumReadA);
        long timeBFileRead = System.currentTimeMillis();
        ObjectInputStream readB = new ObjectInputStream(new FileInputStream(utilFileName));
        int len = readB.available();
        byte[] byteB = new byte[len];
        readB.read(byteB, 0, len);
        BaseModule moduleB = (BaseModule)runtimeSchema.newMessage();
        ProtostuffIOUtil.mergeFrom(byteB, moduleB, runtimeSchema);
        readA.close();
        long sumBReadB = System.currentTimeMillis() - timeBFileRead;
        System.out.println("util读取时间" + sumBReadB);
        System.out.println(moduleA);
        System.out.println(moduleB);
    }
}

对比protostuff和JDK序列化性能测试_第1张图片
对比protostuff和JDK序列化性能测试_第2张图片

你可能感兴趣的:(Java)