diff options
author | Andy Ayers <andya@microsoft.com> | 2015-12-23 14:30:11 -0800 |
---|---|---|
committer | Andy Ayers <andya@microsoft.com> | 2015-12-29 10:38:51 -0800 |
commit | 183985f802d95758c794fd40a5134a3042254634 (patch) | |
tree | bc241b2321e5f4bdd6f630ca87b5fb80e46310f2 | |
parent | d40d047b9eb51116f05357d3dc5c59e21da5f819 (diff) | |
download | coreclr-183985f802d95758c794fd40a5134a3042254634.tar.gz coreclr-183985f802d95758c794fd40a5134a3042254634.tar.bz2 coreclr-183985f802d95758c794fd40a5134a3042254634.zip |
Simple deserialization benchmarks
As with the simple serialization benchmarks, these tests are intended to stress aspects jit code quality and are not meant to compare various deserialization approaches.
-rw-r--r-- | tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.cs | 200 | ||||
-rw-r--r-- | tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.csproj | 45 |
2 files changed, 245 insertions, 0 deletions
diff --git a/tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.cs b/tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.cs new file mode 100644 index 0000000000..8e7f4db9b5 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.cs @@ -0,0 +1,200 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; +using System.IO; +using System.Runtime.CompilerServices; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Json; +using System.Text; +using Newtonsoft.Json.Bson; +using Microsoft.Xunit.Performance; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public class JsonBenchmarks +{ + +#if DEBUG + public const int Iterations = 1; + public const int JsonNetIterations = 1; +#else + public const int Iterations = 30000; + public const int JsonNetIterations = 90000; +#endif + + const string DataContractXml = @"<JsonBenchmarks.TestObject xmlns=""http://schemas.datacontract.org/2004/07/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Id>33</Id><Name>SqMtx</Name><Results xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:double>101.3</a:double><a:double>99.8</a:double><a:double>99.6</a:double><a:double>100.4</a:double></Results><WhenRun>2015-01-01T00:00:00-08:00</WhenRun></JsonBenchmarks.TestObject>"; + + const string DataContractJson = @"{""Id"":33,""Name"":""SqMtx"",""Results"":[101.3,99.8,99.6,100.4],""WhenRun"":""\/Date(1420099200000-0800)\/""}"; + + const string JsonNetJson = @"{""Id"":33,""Name"":""SqMtx"",""Results"":[101.3,99.8,99.6,100.4],""WhenRun"":""2015-01-01T00:00:00-08:00""}"; + + byte[] JsonNetBinary = new byte[] { 0x68, 0x00, 0x00, 0x00, 0x10, 0x49, 0x64, 0x00, 0x21, 0x00, 0x00, 0x00, 0x02, 0x4E, 0x61, 0x6D, 0x65, 0x00, 0x06, 0x00, 0x00, 0x00, 0x53, 0x71, 0x4D, 0x74, 0x78, 0x00, 0x04, 0x52, 0x65, 0x73, 0x75, 0x6C, 0x74, 0x73, 0x00, 0x31, 0x00, 0x00, 0x00, 0x01, 0x30, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x53, 0x59, 0x40, 0x01, 0x31, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0xF3, 0x58, 0x40, 0x01, 0x32, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0xE6, 0x58, 0x40, 0x01, 0x33, 0x00, 0x9A, 0x99, 0x99, 0x99, 0x99, 0x19, 0x59, 0x40, 0x00, 0x09, 0x57, 0x68, 0x65, 0x6E, 0x52, 0x75, 0x6E, 0x00, 0x00, 0x24, 0x82, 0xA4, 0x4A, 0x01, 0x00, 0x00, 0x00 }; + + static volatile object VolatileObject; + + [MethodImpl(MethodImplOptions.NoInlining)] + static void Escape(object obj) { + VolatileObject = obj; + } + + [DataContract] + public class TestObject + { + [DataMember] + public int Id { get; set; } + + [DataMember] + public string Name { get; set; } + + [DataMember] + public double[] Results { get; set; } + + [DataMember] + public DateTime WhenRun { get; set; } + + public static TestObject Expected() + { + TestObject t = new TestObject(); + t.Id = 33; + t.Name = "SqMtx"; + t.Results = new double[] { 101.3, 99.8, 99.6, 100.4 }; + t.WhenRun = DateTime.Parse("Jan 1, 2015 8:00 GMT"); + return t; + } + } + + private bool Deserialize() + { + bool result = true; + DeserializeObject(); + return result; + } + + private void DeserializeObject() + { + DeserializeDataContractBench(); + DeserializeDataContractJsonBench(); + DeserializeJsonNetBinaryBench(); + DeserializeJsonNetBench(); + } + + [Benchmark] + private void DeserializeDataContract() + { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + DeserializeDataContractBench(); + } + } + } + + private void DeserializeDataContractBench() { + DataContractSerializer ds = new DataContractSerializer(typeof(TestObject)); + MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(DataContractXml)); + DeserializeDataContractBenchInner(ds, ms); + } + + private void DeserializeDataContractBenchInner(DataContractSerializer ds, MemoryStream ms) + { + TestObject t; + for (int i = 0; i < Iterations; i++) + { + t = (TestObject)ds.ReadObject(ms); + Escape(t.Name); + ms.Seek(0, SeekOrigin.Begin); + } + } + + [Benchmark] + private void DeserializeDataContractJson() + { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + DeserializeDataContractJsonBench(); + } + } + } + + private void DeserializeDataContractJsonBench() + { + DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(TestObject)); + MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(DataContractJson)); + DeserializeDataContractJsonBenchInner(ds, ms); + } + + private void DeserializeDataContractJsonBenchInner(DataContractJsonSerializer ds, MemoryStream ms) + { + TestObject t; + for (int i = 0; i < Iterations; i++) + { + t = (TestObject) ds.ReadObject(ms); + Escape(t.Name); + ms.Seek(0, SeekOrigin.Begin); + } + } + + [Benchmark] + private void DeserializeJsonNetBinary() + { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + DeserializeJsonNetBinaryBench(); + } + } + } + + private void DeserializeJsonNetBinaryBench() + { + DeserializeJsonNetBinaryBenchInner(); + } + + private void DeserializeJsonNetBinaryBenchInner() + { + Newtonsoft.Json.JsonSerializer ds = new Newtonsoft.Json.JsonSerializer(); + TestObject t; + Type ty = typeof(TestObject); + for (int i = 0; i < JsonNetIterations; i++) + { + BsonReader br = new BsonReader(new MemoryStream(JsonNetBinary)); + t = (TestObject) ds.Deserialize(br, ty); + Escape(t.Name); + } + } + + [Benchmark] + private void DeserializeJsonNet() + { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + DeserializeJsonNetBench(); + } + } + } + + private void DeserializeJsonNetBench() + { + DeserializeJsonNetBenchInner(); + } + + private void DeserializeJsonNetBenchInner() + { + Newtonsoft.Json.JsonSerializer ds = new Newtonsoft.Json.JsonSerializer(); + TestObject t; + Type ty = typeof(TestObject); + for (int i = 0; i < JsonNetIterations; i++) + { + StringReader sr = new StringReader(JsonNetJson); + t = (TestObject)ds.Deserialize(sr, ty); + Escape(t.Name); + } + } + + public static int Main() { + var tests = new JsonBenchmarks(); + bool result = tests.Deserialize(); + return result ? 100 : -1; + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.csproj b/tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.csproj new file mode 100644 index 0000000000..246310841e --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/Serialization/Deserialize.csproj @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid> + <OutputType>Exe</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <FileAlignment>512</FileAlignment> + <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> + <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp> + </PropertyGroup> + <!-- Default configurations to help VS understand the configurations --> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + </PropertyGroup> + <ItemGroup> + <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> + <Visible>False</Visible> + </CodeAnalysisDependentAssemblyPaths> + </ItemGroup> + <ItemGroup> + <None Include="$(JitPackagesConfigFileDirectory)benchmark+serialize\project.json" /> + </ItemGroup> + <ItemGroup> + <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Deserialize.cs" /> + </ItemGroup> + <PropertyGroup> + <ProjectJson>$(JitPackagesConfigFileDirectory)benchmark+serialize\project.json</ProjectJson> + <ProjectLockJson>$(JitPackagesConfigFileDirectory)benchmark+serialize\project.lock.json</ProjectLockJson> + </PropertyGroup> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> + <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "> + </PropertyGroup> +</Project> |