Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b3920ba

Browse files
committed
Add benchmarks to test proj to make it easier to debug
1 parent 3f3e71c commit b3920ba

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
using ServiceStack.Text.Tests.DynamicModels;
4+
5+
namespace ServiceStack.Text.Tests.Benchmarks
6+
{
7+
public class JsonDeserializationBenchmarks
8+
{
9+
static ModelWithAllTypes allTypesModel = ModelWithAllTypes.Create(3);
10+
static ModelWithCommonTypes commonTypesModel = ModelWithCommonTypes.Create(3);
11+
static MemoryStream stream = new MemoryStream(32768);
12+
const string serializedString = "this is the test string";
13+
readonly string serializedString256 = new string('t', 256);
14+
readonly string serializedString512 = new string('t', 512);
15+
readonly string serializedString4096 = new string('t', 4096);
16+
17+
static string commonTypesModelJson;
18+
static string stringTypeJson;
19+
20+
static JsonDeserializationBenchmarks()
21+
{
22+
commonTypesModelJson = JsonSerializer.SerializeToString<ModelWithCommonTypes>(commonTypesModel);
23+
stringTypeJson = JsonSerializer.SerializeToString<StringType>(StringType.Create());
24+
}
25+
26+
[Test]
27+
public void DeserializeJsonCommonTypes()
28+
{
29+
var result = JsonSerializer.DeserializeFromString<ModelWithCommonTypes>(commonTypesModelJson);
30+
}
31+
32+
[Test]
33+
public void DeserializeStringType()
34+
{
35+
var result = JsonSerializer.DeserializeFromString<StringType>(stringTypeJson);
36+
}
37+
}
38+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using NUnit.Framework;
5+
using ServiceStack.Text.Json;
6+
using ServiceStack.Text.Tests.DynamicModels;
7+
8+
namespace ServiceStack.Text.Tests.Benchmarks
9+
{
10+
public class JsonSerializationBenchmarks
11+
{
12+
static ModelWithAllTypes allTypesModel = ModelWithAllTypes.Create(3);
13+
static ModelWithCommonTypes commonTypesModel = ModelWithCommonTypes.Create(3);
14+
static MemoryStream stream = new MemoryStream(32768);
15+
const string serializedString = "this is the test string";
16+
readonly string serializedString256 = new string('t', 256);
17+
readonly string serializedString512 = new string('t', 512);
18+
readonly string serializedString4096 = new string('t', 4096);
19+
20+
[Test]
21+
public void SerializeJsonAllTypes()
22+
{
23+
string result = JsonSerializer.SerializeToString<ModelWithAllTypes>(allTypesModel);
24+
}
25+
26+
[Test]
27+
public void SerializeJsonCommonTypes()
28+
{
29+
string result = JsonSerializer.SerializeToString<ModelWithCommonTypes>(commonTypesModel);
30+
}
31+
32+
[Test]
33+
public void SerializeJsonString()
34+
{
35+
string result = JsonSerializer.SerializeToString<string>(serializedString);
36+
}
37+
38+
[Test]
39+
public void SerializeJsonStringToStream()
40+
{
41+
stream.Position = 0;
42+
JsonSerializer.SerializeToStream<string>(serializedString, stream);
43+
}
44+
45+
[Test]
46+
public void SerializeJsonString256ToStream()
47+
{
48+
stream.Position = 0;
49+
JsonSerializer.SerializeToStream<string>(serializedString256, stream);
50+
}
51+
52+
[Test]
53+
public void SerializeJsonString512ToStream()
54+
{
55+
stream.Position = 0;
56+
JsonSerializer.SerializeToStream<string>(serializedString512, stream);
57+
}
58+
59+
[Test]
60+
public void SerializeJsonString4096ToStream()
61+
{
62+
stream.Position = 0;
63+
JsonSerializer.SerializeToStream<string>(serializedString4096, stream);
64+
}
65+
66+
[Test]
67+
public void SerializeJsonStringToStreamDirectly()
68+
{
69+
stream.Position = 0;
70+
string tmp = JsonSerializer.SerializeToString<string>(serializedString);
71+
byte[] arr = Encoding.UTF8.GetBytes(tmp);
72+
stream.Write(arr, 0, arr.Length);
73+
}
74+
75+
76+
[Test]
77+
public void SerializeJsonAllTypesToStream()
78+
{
79+
stream.Position = 0;
80+
JsonSerializer.SerializeToStream<ModelWithAllTypes>(allTypesModel, stream);
81+
}
82+
83+
[Test]
84+
public void SerializeJsonCommonTypesToStream()
85+
{
86+
stream.Position = 0;
87+
JsonSerializer.SerializeToStream<ModelWithCommonTypes>(commonTypesModel, stream);
88+
}
89+
90+
[Test]
91+
public void SerializeJsonStringToStreamUsingDirectStreamWriter()
92+
{
93+
stream.Position = 0;
94+
var writer = new DirectStreamWriter(stream, JsonSerializer.UTF8Encoding);
95+
JsonWriter<string>.WriteRootObject(writer, serializedString);
96+
writer.Flush();
97+
}
98+
99+
[Test]
100+
public void SerializeJsonString256ToStreamUsingDirectStreamWriter()
101+
{
102+
stream.Position = 0;
103+
var writer = new DirectStreamWriter(stream, JsonSerializer.UTF8Encoding);
104+
JsonWriter<string>.WriteRootObject(writer, serializedString256);
105+
writer.Flush();
106+
}
107+
108+
[Test]
109+
public void SerializeJsonString512ToStreamUsingDirectStreamWriter()
110+
{
111+
stream.Position = 0;
112+
var writer = new DirectStreamWriter(stream, JsonSerializer.UTF8Encoding);
113+
JsonWriter<string>.WriteRootObject(writer, serializedString512);
114+
writer.Flush();
115+
}
116+
117+
[Test]
118+
public void SerializeJsonString4096ToStreamUsingDirectStreamWriter()
119+
{
120+
stream.Position = 0;
121+
var writer = new DirectStreamWriter(stream, JsonSerializer.UTF8Encoding);
122+
JsonWriter<string>.WriteRootObject(writer, serializedString4096);
123+
writer.Flush();
124+
}
125+
}
126+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
3+
namespace ServiceStack.Text.Tests.Benchmarks
4+
{
5+
public class ModelWithCommonTypes
6+
{
7+
public char CharValue { get; set; }
8+
9+
public byte ByteValue { get; set; }
10+
11+
public sbyte SByteValue { get; set; }
12+
13+
public short ShortValue { get; set; }
14+
15+
public ushort UShortValue { get; set; }
16+
17+
public int IntValue { get; set; }
18+
19+
public uint UIntValue { get; set; }
20+
21+
public long LongValue { get; set; }
22+
23+
public ulong ULongValue { get; set; }
24+
25+
public float FloatValue { get; set; }
26+
27+
public double DoubleValue { get; set; }
28+
29+
public decimal DecimalValue { get; set; }
30+
31+
public DateTime DateTimeValue { get; set; }
32+
33+
public TimeSpan TimeSpanValue { get; set; }
34+
35+
public Guid GuidValue { get; set; }
36+
37+
public static ModelWithCommonTypes Create(byte i)
38+
{
39+
return new ModelWithCommonTypes
40+
{
41+
ByteValue = i,
42+
CharValue = (char)i,
43+
DateTimeValue = new DateTime(2000, 1, 1 + i),
44+
DecimalValue = i,
45+
DoubleValue = i,
46+
FloatValue = i,
47+
IntValue = i,
48+
LongValue = i,
49+
SByteValue = (sbyte)i,
50+
ShortValue = i,
51+
TimeSpanValue = new TimeSpan(i),
52+
UIntValue = i,
53+
ULongValue = i,
54+
UShortValue = i,
55+
GuidValue = Guid.NewGuid(),
56+
};
57+
}
58+
}
59+
60+
public class StringType
61+
{
62+
public string Value1 { get; set; }
63+
public string Value2 { get; set; }
64+
public string Value3 { get; set; }
65+
public string Value4 { get; set; }
66+
public string Value5 { get; set; }
67+
public string Value6 { get; set; }
68+
public string Value7 { get; set; }
69+
70+
public static StringType Create()
71+
{
72+
var st = new StringType();
73+
st.Value1 = st.Value2 = st.Value3 = st.Value4 = st.Value5 = st.Value6 = st.Value7 = "Hello, world";
74+
return st;
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)