Last active
December 30, 2015 21:59
-
-
Save StephenCleary/7890983 to your computer and use it in GitHub Desktop.
Serialization size test for Azure caching.
Requires NuGet packages: Comparers, Newtonsoft.Json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Diagnostics; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Linq; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Text; | |
using Comparers; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Bson; | |
namespace AzureCacheSizeTest | |
{ | |
public class MyTypeNSer | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string Description { get; set; } | |
} | |
[Serializable] | |
public class MyTypeYSer | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string Description { get; set; } | |
} | |
class Program | |
{ | |
public static readonly IFullComparer<MyTypeYSer> ComparerYSer = Compare<MyTypeYSer>.OrderBy(x => x.FirstName).ThenBy(x => x.LastName).ThenBy(x => x.Description); | |
public static readonly IFullComparer<MyTypeNSer> ComparerNSer = Compare<MyTypeNSer>.OrderBy(x => x.FirstName).ThenBy(x => x.LastName).ThenBy(x => x.Description); | |
static void Main(string[] args) | |
{ | |
var yser = new MyTypeYSer { FirstName = "Christopher", LastName = "Dombrowski", Description = "This is a generic string solely for the purpose of searching." }; | |
var nser = new MyTypeNSer { FirstName = "Christopher", LastName = "Dombrowski", Description = "This is a generic string solely for the purpose of searching." }; | |
Console.Write("Default: "); | |
TestDefault(nser, CompressionLevel.NoCompression); | |
Console.Write("Default (compressed): "); | |
TestDefault(nser, CompressionLevel.Optimal); | |
Console.Write("Default (serializable): "); | |
TestDefault(yser, CompressionLevel.NoCompression); | |
Console.Write("Default (serializable, compressed): "); | |
TestDefault(yser, CompressionLevel.Optimal); | |
Console.Write("Binary: "); | |
TestBinary(yser, CompressionLevel.NoCompression); | |
Console.Write("Binary (compressed): "); | |
TestBinary(yser, CompressionLevel.Optimal); | |
Console.Write("JSON: "); | |
TestJson(nser, CompressionLevel.NoCompression); | |
Console.Write("JSON (compressed): "); | |
TestJson(nser, CompressionLevel.Optimal); | |
Console.Write("BSON: "); | |
TestBson(nser, CompressionLevel.NoCompression); | |
Console.Write("BSON (compressed): "); | |
TestBson(nser, CompressionLevel.Optimal); | |
Console.ReadKey(); | |
} | |
private static void Test(object value, Action<Stream, object> serialize, Func<Stream, object> deserialize, CompressionLevel compression) | |
{ | |
try | |
{ | |
var output = new MemoryStream(); | |
if (compression == CompressionLevel.NoCompression) | |
serialize(output, value); | |
else | |
{ | |
using (var compressStream = new DeflateStream(output, compression)) | |
{ | |
serialize(compressStream, value); | |
} | |
} | |
output.Flush(); | |
var data = output.ToArray(); | |
Console.WriteLine(data.Length); | |
Trace.WriteLine(""); | |
Trace.WriteLine(string.Join("", data.Select(x => | |
((x >= 32 && x <= 91) || (x >= 93 && x <= 126)) ? new string(new [] { (char)x }) : (@"\" + x.ToString("X2"))))); | |
Trace.WriteLine(string.Join("", data.Select(x => x.ToString("X2")))); | |
var input = new MemoryStream(data); | |
object result; | |
if (compression == CompressionLevel.NoCompression) | |
result = deserialize(input); | |
else | |
{ | |
using (var decompressStream = new DeflateStream(input, CompressionMode.Decompress)) | |
{ | |
result = deserialize(decompressStream); | |
} | |
} | |
var comparer = (value is MyTypeNSer) ? ComparerNSer as IEqualityComparer : ComparerYSer; | |
if (!comparer.Equals(value, result)) | |
Console.WriteLine("Compare failed!"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
public static void TestDefault(object value, CompressionLevel compression) | |
{ | |
var serializer = new NetDataContractSerializer(); | |
Test(value, serializer.WriteObject, serializer.ReadObject, compression); | |
} | |
private static void TestBinary(object value, CompressionLevel compression) | |
{ | |
var serializer = new BinaryFormatter(); | |
Test(value, serializer.Serialize, serializer.Deserialize, compression); | |
} | |
private static void TestJson(object value, CompressionLevel compression) | |
{ | |
var serializer = JsonSerializer.Create(new JsonSerializerSettings | |
{ | |
TypeNameHandling = TypeNameHandling.Auto, | |
NullValueHandling = NullValueHandling.Ignore, | |
}); | |
Test(value, | |
(stream, val) => | |
{ | |
using (var textWriter = new StreamWriter(stream, new UTF8Encoding(false))) | |
using (var writer = new JsonTextWriter(textWriter)) | |
serializer.Serialize(writer, val, typeof(object)); | |
}, | |
stream => | |
{ | |
using (var textReader = new StreamReader(stream, Encoding.UTF8, false)) | |
using (var reader = new JsonTextReader(textReader)) | |
return serializer.Deserialize(reader); | |
}, | |
compression); | |
} | |
private static void TestBson(object value, CompressionLevel compression) | |
{ | |
var serializer = JsonSerializer.Create(new JsonSerializerSettings | |
{ | |
TypeNameHandling = TypeNameHandling.Auto, | |
NullValueHandling = NullValueHandling.Ignore, | |
}); | |
Test(value, | |
(stream, val) => | |
{ | |
using (var writer = new BsonWriter(stream)) | |
serializer.Serialize(writer, val, typeof(object)); | |
}, | |
stream => | |
{ | |
using (var reader = new BsonReader(stream)) | |
return serializer.Deserialize(reader); | |
}, | |
compression); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment