This commit is contained in:
neuecc 2017-03-10 22:59:56 +09:00
Родитель ac413cff1f
Коммит 723be25b05
3 изменённых файлов: 99 добавлений и 11 удалений

Просмотреть файл

@ -12,6 +12,9 @@ using ProtoBuf;
using SharedData;
using System.Collections;
using UnityEngine;
using Newtonsoft.Json;
using System.Text;
using System.IO.Compression;
namespace Sandbox
{
@ -126,12 +129,15 @@ namespace Sandbox
{
const int Iteration = 10000; // 10000
var jsonSerializer = new JsonSerializer();
var msgpack = MsgPack.Serialization.SerializationContext.Default;
msgpack.GetSerializer<T>().PackSingleObject(target);
MessagePack.MessagePackSerializer.Serialize(target);
LZ4MessagePackSerializer.Serialize(target);
ZeroFormatter.ZeroFormatterSerializer.Serialize(target);
ProtoBuf.Serializer.Serialize(new MemoryStream(), target);
jsonSerializer.Serialize(new JsonTextWriter(new StringWriter()), target);
Console.WriteLine(typeof(T).Name + " serialization test");
Console.WriteLine();
@ -142,6 +148,8 @@ namespace Sandbox
byte[] data1 = null;
byte[] data2 = null;
byte[] data3 = null;
byte[] dataJson = null;
byte[] dataGzipJson = null;
using (new Measure("MsgPack-Cli"))
{
for (int i = 0; i < Iteration; i++)
@ -158,7 +166,6 @@ namespace Sandbox
}
}
using (new Measure("MessagePack(LZ4)"))
{
for (int i = 0; i < Iteration; i++)
@ -174,6 +181,34 @@ namespace Sandbox
data1 = ZeroFormatter.ZeroFormatterSerializer.Serialize(target);
}
}
using (new Measure("JsonNet"))
{
for (int i = 0; i < Iteration; i++)
{
using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
using (var jw = new JsonTextWriter(sw))
{
jsonSerializer.Serialize(jw, target);
}
}
}
using (new Measure("JsonNet+Gzip"))
{
for (int i = 0; i < Iteration; i++)
{
using (var ms = new MemoryStream())
using (var gzip = new GZipStream(ms, CompressionLevel.Fastest))
using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true))
using (var jw = new JsonTextWriter(sw))
{
jsonSerializer.Serialize(jw, target);
}
}
}
using (new Measure("protobuf-net"))
{
for (int i = 0; i < Iteration; i++)
@ -190,11 +225,34 @@ namespace Sandbox
ProtoBuf.Serializer.Serialize(ms, target);
data2 = ms.ToArray();
}
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
using (var jw = new JsonTextWriter(sw))
{
jsonSerializer.Serialize(jw, target);
}
dataJson = ms.ToArray();
}
using (var ms = new MemoryStream())
{
using (var gzip = new GZipStream(ms, CompressionLevel.Fastest))
using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true))
using (var jw = new JsonTextWriter(sw))
{
jsonSerializer.Serialize(jw, target);
}
dataGzipJson = ms.ToArray();
}
msgpack.GetSerializer<T>().UnpackSingleObject(data);
MessagePack.MessagePackSerializer.Deserialize<T>(data0);
ZeroFormatterSerializer.Deserialize<T>(data1);
ProtoBuf.Serializer.Deserialize<T>(new MemoryStream(data2));
LZ4MessagePackSerializer.Deserialize<T>(data3);
jsonSerializer.Deserialize<T>(new JsonTextReader(new StreamReader(new MemoryStream(dataJson))));
Console.WriteLine();
Console.WriteLine("Deserialize::");
@ -231,6 +289,33 @@ namespace Sandbox
}
}
using (new Measure("JsonNet"))
{
for (int i = 0; i < Iteration; i++)
{
using (var ms = new MemoryStream(dataJson))
using (var sr = new StreamReader(ms, Encoding.UTF8))
using (var jr = new JsonTextReader(sr))
{
jsonSerializer.Deserialize<T>(jr);
}
}
}
using (new Measure("JsonNet+Gzip"))
{
for (int i = 0; i < Iteration; i++)
{
using (var ms = new MemoryStream(dataGzipJson))
using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
using (var sr = new StreamReader(gzip, Encoding.UTF8))
using (var jr = new JsonTextReader(sr))
{
jsonSerializer.Deserialize<T>(jr);
}
}
}
using (new Measure("protobuf-net"))
{
for (int i = 0; i < Iteration; i++)
@ -250,6 +335,8 @@ namespace Sandbox
label = "MessagePack(LZ4)"; Console.WriteLine($"{label,20} {data3.Length} Byte");
label = "ZeroFormatter"; Console.WriteLine($"{label,20} {data1.Length} Byte");
label = "protobuf-net"; Console.WriteLine($"{label,20} {data2.Length} Byte");
label = "JsonNet"; Console.WriteLine($"{label,20} {dataJson.Length} Byte");
label = "JsonNet+GZip"; Console.WriteLine($"{label,20} {dataGzipJson.Length} Byte");
Console.WriteLine();
Console.WriteLine();

Просмотреть файл

@ -11,7 +11,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.Portable.Compatibility" Version="1.0.1" />
<PackageReference Include="MsgPack.Cli" Version="0.9.0-beta2" />
<PackageReference Include="MsgPack.Cli" Version="0.7.1" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.1-beta1" />
<PackageReference Include="protobuf-net" Version="2.1.0" />
<PackageReference Include="ZeroFormatter" Version="1.6.2" />
</ItemGroup>

Просмотреть файл

@ -35,7 +35,7 @@ namespace MessagePack.LZ4
{
public static partial class LZ4Codec
{
#region configuration
#region configuration
/// <summary>
/// Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
@ -43,7 +43,7 @@ namespace MessagePack.LZ4
/// Reduced memory usage can improve speed, due to cache effect
/// Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
/// </summary>
private const int MEMORY_USAGE = 14;
private const int MEMORY_USAGE = 12; // mod, use 12
/// <summary>
/// Decreasing this value will make the algorithm skip faster data segments considered "incompressible"
@ -54,9 +54,9 @@ namespace MessagePack.LZ4
/// </summary>
private const int NOTCOMPRESSIBLE_DETECTIONLEVEL = 6;
#endregion
#endregion
#region consts
#region consts
private const int MINMATCH = 4;
@ -116,9 +116,9 @@ namespace MessagePack.LZ4
private const int MAX_NB_ATTEMPTS = 256;
private const int OPTIMAL_ML = (ML_MASK - 1) + MINMATCH;
#endregion
#endregion
#region public interface (common)
#region public interface (common)
/// <summary>Gets maximum the length of the output.</summary>
/// <param name="inputLength">Length of the input.</param>
@ -128,9 +128,9 @@ namespace MessagePack.LZ4
return inputLength + (inputLength / 255) + 16;
}
#endregion
#endregion
#region internal interface (common)
#region internal interface (common)
internal static void CheckArguments(
byte[] input, int inputOffset, ref int inputLength,
@ -153,7 +153,7 @@ namespace MessagePack.LZ4
throw new ArgumentException("outputOffset and outputLength are invalid for given output");
}
#endregion
#endregion
}
}