|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using ServiceStack.Text; |
| 6 | +using ServiceStack.Text.Tests.DynamicModels; |
| 7 | +using ServiceStack.Text.Json; |
| 8 | + |
| 9 | +namespace ServiceStack.Text.Benchmarks |
| 10 | +{ |
| 11 | + public class ModelWithCommonTypes |
| 12 | + { |
| 13 | + public char CharValue { get; set; } |
| 14 | + |
| 15 | + public byte ByteValue { get; set; } |
| 16 | + |
| 17 | + public sbyte SByteValue { get; set; } |
| 18 | + |
| 19 | + public short ShortValue { get; set; } |
| 20 | + |
| 21 | + public ushort UShortValue { get; set; } |
| 22 | + |
| 23 | + public int IntValue { get; set; } |
| 24 | + |
| 25 | + public uint UIntValue { get; set; } |
| 26 | + |
| 27 | + public long LongValue { get; set; } |
| 28 | + |
| 29 | + public ulong ULongValue { get; set; } |
| 30 | + |
| 31 | + public float FloatValue { get; set; } |
| 32 | + |
| 33 | + public double DoubleValue { get; set; } |
| 34 | + |
| 35 | + public decimal DecimalValue { get; set; } |
| 36 | + |
| 37 | + public DateTime DateTimeValue { get; set; } |
| 38 | + |
| 39 | + public TimeSpan TimeSpanValue { get; set; } |
| 40 | + |
| 41 | + public Guid GuidValue { get; set; } |
| 42 | + |
| 43 | + public static ModelWithCommonTypes Create(byte i) |
| 44 | + { |
| 45 | + return new ModelWithCommonTypes |
| 46 | + { |
| 47 | + ByteValue = i, |
| 48 | + CharValue = (char)i, |
| 49 | + DateTimeValue = new DateTime(2000, 1, 1 + i), |
| 50 | + DecimalValue = i, |
| 51 | + DoubleValue = i, |
| 52 | + FloatValue = i, |
| 53 | + IntValue = i, |
| 54 | + LongValue = i, |
| 55 | + SByteValue = (sbyte)i, |
| 56 | + ShortValue = i, |
| 57 | + TimeSpanValue = new TimeSpan(i), |
| 58 | + UIntValue = i, |
| 59 | + ULongValue = i, |
| 60 | + UShortValue = i, |
| 61 | + GuidValue = Guid.NewGuid(), |
| 62 | + }; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public class JsonSerializationBenchmarks |
| 67 | + { |
| 68 | + static ModelWithAllTypes allTypesModel = ModelWithAllTypes.Create(3); |
| 69 | + static ModelWithCommonTypes commonTypesModel = ModelWithCommonTypes.Create(3); |
| 70 | + static MemoryStream stream = new MemoryStream(16384); |
| 71 | + const string serializedString = "this is the test string"; |
| 72 | + |
| 73 | + [Benchmark] |
| 74 | + public void SerializeJsonAllTypes() |
| 75 | + { |
| 76 | + string result = JsonSerializer.SerializeToString<ModelWithAllTypes>(allTypesModel); |
| 77 | + } |
| 78 | + |
| 79 | + [Benchmark] |
| 80 | + public void SerializeJsonCommonTypes() |
| 81 | + { |
| 82 | + string result = JsonSerializer.SerializeToString<ModelWithCommonTypes>(commonTypesModel); |
| 83 | + } |
| 84 | + |
| 85 | + [Benchmark] |
| 86 | + public void SerializeJsonString() |
| 87 | + { |
| 88 | + string result = JsonSerializer.SerializeToString<string>(serializedString); |
| 89 | + } |
| 90 | + |
| 91 | + [Benchmark] |
| 92 | + public void SerializeJsonStringToStream() |
| 93 | + { |
| 94 | + stream.Position = 0; |
| 95 | + JsonSerializer.SerializeToStream<string>(serializedString, stream); |
| 96 | + } |
| 97 | + |
| 98 | + [Benchmark] |
| 99 | + public void SerializeJsonStringToStreamDirectly() |
| 100 | + { |
| 101 | + stream.Position = 0; |
| 102 | + string tmp = JsonSerializer.SerializeToString<string>(serializedString); |
| 103 | + byte[] arr = Encoding.UTF8.GetBytes(tmp); |
| 104 | + stream.Write(arr, 0, arr.Length); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + [Benchmark] |
| 109 | + public void SerializeJsonAllTypesToStream() |
| 110 | + { |
| 111 | + stream.Position = 0; |
| 112 | + JsonSerializer.SerializeToStream<ModelWithAllTypes>(allTypesModel, stream); |
| 113 | + } |
| 114 | + |
| 115 | + [Benchmark] |
| 116 | + public void SerializeJsonCommonTypesToStream() |
| 117 | + { |
| 118 | + stream.Position = 0; |
| 119 | + JsonSerializer.SerializeToStream<ModelWithCommonTypes>(commonTypesModel, stream); |
| 120 | + } |
| 121 | + |
| 122 | +/* [Benchmark] |
| 123 | + public void SerializeJsonStringToStreamUsingDirectStreamWriter() |
| 124 | + { |
| 125 | + stream.Position = 0; |
| 126 | + var writer = new DirectStreamWriter(stream); |
| 127 | + JsonWriter<string>.WriteRootObject(writer, serializedString); |
| 128 | + writer.Flush(); |
| 129 | + } |
| 130 | +*/ |
| 131 | + } |
| 132 | +} |
0 commit comments