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

Commit a661821

Browse files
committed
Add benchmarks for data deserialization
1 parent 870658e commit a661821

File tree

4 files changed

+174
-1
lines changed

4 files changed

+174
-1
lines changed

benchmarks/ServiceStack.Text.Benchmarks/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class Program
1010
{
1111
public static void Main(string[] args)
1212
{
13-
Console.WriteLine("Hello World!");
1413
//BenchmarkRunner.Run<JsonSerializationBenchmarks>(
1514
BenchmarkRunner.Run<ParseBuiltinBenchmarks>(
1615
ManualConfig
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 StringType
67+
{
68+
public string Value1 { get; set; }
69+
public string Value2 { get; set; }
70+
public string Value3 { get; set; }
71+
public string Value4 { get; set; }
72+
public string Value5 { get; set; }
73+
public string Value6 { get; set; }
74+
public string Value7 { get; set; }
75+
76+
public static StringType Create()
77+
{
78+
var st = new StringType();
79+
st.Value1 = st.Value2 = st.Value3 = st.Value4 = st.Value5 = st.Value6 = st.Value7 = "Hello, world";
80+
return st;
81+
}
82+
}
83+
84+
[MemoryDiagnoser]
85+
public class JsonSerializationBenchmarks
86+
{
87+
static ModelWithAllTypes allTypesModel = ModelWithAllTypes.Create(3);
88+
static ModelWithCommonTypes commonTypesModel = ModelWithCommonTypes.Create(3);
89+
static MemoryStream stream = new MemoryStream(32768);
90+
const string serializedString = "this is the test string";
91+
readonly string serializedString256 = new string('t', 256);
92+
readonly string serializedString512 = new string('t', 512);
93+
readonly string serializedString4096 = new string('t', 4096);
94+
95+
static string commonTypesModelJson;
96+
static string stringTypeJson;
97+
98+
static JsonSerializationBenchmarks()
99+
{
100+
commonTypesModelJson = JsonSerializer.SerializeToString<ModelWithCommonTypes>(commonTypesModel);
101+
stringTypeJson = JsonSerializer.SerializeToString<StringType>(StringType.Create());
102+
}
103+
104+
[Benchmark]
105+
public void DeserializeJsonCommonTypes()
106+
{
107+
var result = JsonSerializer.DeserializeFromString<ModelWithCommonTypes>(commonTypesModelJson);
108+
}
109+
110+
[Benchmark]
111+
public void DeserializeStringType()
112+
{
113+
var result = JsonSerializer.DeserializeFromString<StringType>(stringTypeJson);
114+
}
115+
116+
117+
}
118+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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(Job.Clr)
20+
.With(new BenchmarkDotNet.Diagnosers.CompositeDiagnoser())
21+
.With(ExecutionValidator.FailOnError)
22+
);
23+
}
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net45;</TargetFrameworks>
5+
<AssemblyName>ServiceStack.Text.VersionCompareBenchmarks</AssemblyName>
6+
<OutputType>Exe</OutputType>
7+
<PackageId>ServiceStack.Text.VersionCompareBenchmarks</PackageId>
8+
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
9+
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
10+
</PropertyGroup>
11+
12+
<PropertyGroup Condition=" '$(Configuration)' != 'Debug' ">
13+
<Optimize>true</Optimize>
14+
</PropertyGroup>
15+
16+
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
17+
<DebugType>portable</DebugType>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\..\src\ServiceStack.Text\ServiceStack.Text.csproj" Version="1.0.*" />
22+
<!-- <PackageReference Include="ServiceStack.Text" Version="4.5.8" /> -->
23+
<ProjectReference Include="..\..\tests\ServiceStack.Text.Tests\ServiceStack.Text.Tests.csproj" Version="1.0.*" />
24+
<PackageReference Include="BenchmarkDotNet" Version="0.10.3" />
25+
</ItemGroup>
26+
27+
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
28+
<PackageReference Include="Microsoft.Extensions.Primitives" Version="1.1.1" />
29+
</ItemGroup>
30+
31+
</Project>

0 commit comments

Comments
 (0)