Last active
April 1, 2019 20:04
-
-
Save murdockcrc/6fc7d0cb98f86021174b0684d17bb798 to your computer and use it in GitHub Desktop.
Console app that uses MessagePack and GZIP compression for benchmarking purposes
This file contains hidden or 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.Text; | |
using MessagePack; | |
using System.IO.Compression; | |
using System.IO; | |
namespace compress | |
{ | |
class Program | |
{ | |
private static string _messageSample = @" | |
{ | |
""specversion"" : ""0.2"", | |
""type"" : ""com.customerId.iot.deviceType.eventType"", | |
""source"" : ""device_id"", | |
""id"" : ""B234-1234-1234"", | |
""time"" : ""2018-04-05T17:31:00Z"", | |
""customer_extension_01"" : ""value"", | |
""customer_extension_02"" : { | |
""otherValue"": 5 | |
}, | |
""contenttype"" : ""application/json"", | |
""data"" : [ | |
{ | |
""signal_name_01"": ""temp_sensor_01"", | |
""values"": [24.01] | |
}, | |
{ | |
""signal_name_02"": ""temp_sensor_02"", | |
""values"": [25.02] | |
}, | |
{ | |
""signal_name_03"": ""alarms_controller"", | |
""values"": [true, false, false, true] | |
} | |
] | |
}"; | |
static void Main(string[] args) | |
{ | |
var messageInBytes = Encoding.UTF8.GetBytes(_messageSample); | |
Console.WriteLine($"Plain JSON object size: {messageInBytes.Length}"); | |
var messageAsMessagePack = MessagePackSerializer.FromJson(_messageSample); | |
Console.WriteLine($"MessagePack object size: {messageAsMessagePack.Length}"); | |
using (FileStream fileStream = File.Create("compressed.bin")) | |
using (GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress)) | |
{ | |
var memStream = new MemoryStream(messageAsMessagePack); | |
memStream.CopyTo(compressionStream); | |
Console.WriteLine($"Compressed MessagePack object size: {fileStream.Length}"); | |
} | |
using (FileStream fileStream = File.Open("compressed.bin", FileMode.Open)) | |
using (FileStream decompressedfile = File.Create("decompressed.txt")) | |
using (GZipStream decompressStream = new GZipStream(fileStream, CompressionMode.Decompress)) | |
{ | |
decompressStream.CopyTo(decompressedfile); | |
} | |
using (FileStream fileStream = File.Open("decompressed.txt", FileMode.Open)) | |
{ | |
var deserialized = MessagePackSerializer.Deserialize<object>(fileStream); | |
Console.WriteLine(MessagePackSerializer.ToJson(deserialized)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment