Created
August 19, 2016 13:26
-
-
Save mattwarren/e6856ab4625d4e306cc04b9349edd869 to your computer and use it in GitHub Desktop.
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
| [Config(typeof(Config))] | |
| public class StringSerialisationDeserialisation | |
| { | |
| static void Main(string[] args) | |
| { | |
| var test = new StringSerialisationDeserialisation(); | |
| var normalWrite = test.NormalWrite(); | |
| Console.WriteLine("Normal write: length={0}, \"{1}\"", normalWrite.Length, | |
| Encoding.UTF8.GetString(normalWrite.GetBuffer(), 0, (int)normalWrite.Length)); | |
| Console.WriteLine("Normal read: {0}", test.NormalRead()); | |
| var optimisedWrite = test.OptimisedWrite(); | |
| Console.WriteLine("Optimised write: length={0} ({1}), \"{2}\"", | |
| optimisedWrite.Length, optimisedWrite.GetBuffer()[0], | |
| Encoding.UTF8.GetString(optimisedWrite.GetBuffer(), 1, (int)optimisedWrite.Length - 1)); | |
| Console.WriteLine("Optimised read: {0}", test.OptimisedRead()); | |
| var summary = BenchmarkRunner.Run<StringSerialisationDeserialisation>(); | |
| } | |
| private class Config : ManualConfig | |
| { | |
| public Config() | |
| { | |
| Add(Job.Clr.WithLaunchCount(1).WithWarmupCount(5).WithTargetCount(5)); | |
| Add(new MemoryDiagnoser()); | |
| Add(JitOptimizationsValidator.FailOnError); | |
| Add(RPlotExporter.Default); | |
| } | |
| } | |
| private static string textToWriteShort = "Lorem ipsum gravida habitant fames senectus."; | |
| private static string textToWriteLong = "Lorem ipsum quis per at sit sollicitudin, proin pharetra vivamus maecenas vitae, lacus senectus curabitur ad donec."; | |
| private static Serializer serialiser = new Serializer(); | |
| private static SerializerSession serializerSession = new SerializerSession(serialiser); | |
| private static DeserializerSession deserializerSession = new DeserializerSession(serialiser); | |
| private static StringSerializer stringSerialiser = StringSerializer.Instance; | |
| private static MemoryStream streamNormalWrite, streamNormalRead; | |
| private static MemoryStream streamOptimisedWrite, streamOptimisedRead; | |
| static StringSerialisationDeserialisation() | |
| { | |
| // Writing | |
| streamNormalWrite = new MemoryStream(1024); | |
| streamOptimisedWrite = new MemoryStream(1024); | |
| // Reading | |
| var normalSerialisedText = Encoding.UTF8.GetBytes(textToWriteShort); | |
| streamNormalRead = new MemoryStream(normalSerialisedText, 0, normalSerialisedText.Length, writable: false, publiclyVisible: true); | |
| var tempStream = new MemoryStream(); | |
| stringSerialiser.WriteValue(tempStream, textToWriteShort, serializerSession); | |
| byte[] wireSerialisedText = new byte[tempStream.Length]; | |
| Array.Copy(tempStream.GetBuffer(), wireSerialisedText, tempStream.Length); | |
| streamOptimisedRead = new MemoryStream(wireSerialisedText); | |
| } | |
| [Benchmark] | |
| public MemoryStream NormalWrite() | |
| { | |
| streamNormalWrite.Position = 0; | |
| var bytes = Encoding.UTF8.GetBytes(textToWriteShort); | |
| streamNormalWrite.Write(bytes, 0, bytes.Length); | |
| return streamNormalWrite; | |
| } | |
| [Benchmark] | |
| public MemoryStream OptimisedWrite() | |
| { | |
| streamOptimisedWrite.Position = 0; | |
| stringSerialiser.WriteValue(streamOptimisedWrite, textToWriteShort, serializerSession); | |
| return streamOptimisedWrite; | |
| } | |
| [Benchmark] | |
| public string NormalRead() | |
| { | |
| return Encoding.UTF8.GetString(streamNormalRead.GetBuffer(), 0, (int)streamNormalRead.Length); | |
| } | |
| [Benchmark] | |
| public string OptimisedRead() | |
| { | |
| streamOptimisedRead.Position = 0; | |
| //return (string)stringSerialiser.ReadValue(streamOptimisedRead, deserializerSession); | |
| return StringSerializer.ReadValueImpl(streamOptimisedRead, deserializerSession); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.