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

Commit 078ccec

Browse files
committed
Add benchmarks
1 parent c604b92 commit 078ccec

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-1
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using BenchmarkDotNet.Configs;
3+
using BenchmarkDotNet.Jobs;
4+
using BenchmarkDotNet.Running;
5+
using BenchmarkDotNet.Validators;
6+
7+
namespace ServiceStack.Text.Benchmarks
8+
{
9+
public class Program
10+
{
11+
public static void Main(string[] args)
12+
{
13+
Console.WriteLine("Hello World!");
14+
BenchmarkRunner.Run<JsonSerializationBenchmarks>(
15+
ManualConfig
16+
.Create(DefaultConfig.Instance)
17+
//.With(Job.RyuJitX64)
18+
.With(Job.Core)
19+
.With(new BenchmarkDotNet.Diagnosers.CompositeDiagnoser())
20+
.With(ExecutionValidator.FailOnError)
21+
);
22+
}
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"buildOptions": {
3+
"debugType": "portable",
4+
"emitEntryPoint": true
5+
},
6+
"dependencies": {
7+
"ServiceStack.Text": "1.0.*",
8+
"ServiceStack.Text.Tests": "1.0.*"
9+
},
10+
"frameworks": {
11+
"netcoreapp1.1": {
12+
"dependencies": {
13+
"Microsoft.NETCore.App": {
14+
"type": "platform",
15+
"version": "1.1.0"
16+
},
17+
"BenchmarkDotNet": "0.10.1"
18+
},
19+
"imports": "dnxcore50"
20+
}
21+
},
22+
"version": "1.0.0-*"
23+
}

global.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"projects" : [
33
"src",
4-
"tests"
4+
"tests",
5+
"benchmarks"
56
]
67
}

0 commit comments

Comments
 (0)