Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\PerformanceTests.cs
Symbol Coverage: 93.80% (257 of 274)
Branch Coverage: 94.55% (52 of 55)
Cyclomatic Complexity Avg: 1.48 Max:6
Code Lines: 284
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#if !SILVERLIGHT && !PocketPC && !NET20 |
|
2 |
using System; |
|
3 |
using System.Collections.Generic; |
|
4 |
using System.Globalization; |
|
5 |
using System.Diagnostics; |
|
6 |
using System.IO; |
|
7 |
using System.Runtime.Serialization; |
|
8 |
using System.Web.Script.Serialization; |
|
9 |
using System.Xml.Serialization; |
|
10 |
using Newtonsoft.Json.Utilities; |
|
11 |
using NUnit.Framework; |
|
12 |
using System.Runtime.Serialization.Json; |
|
13 |
using System.Text; |
|
14 |
using Newtonsoft.Json.Bson; |
|
15 |
using System.Runtime.Serialization.Formatters.Binary; |
|
16 |
using Newtonsoft.Json.Linq; |
|
17 |
||
18 |
namespace Newtonsoft.Json.Tests |
|
19 |
{ |
|
20 |
[Serializable] |
|
21 |
[DataContract] |
|
22 |
public class Image |
|
23 |
{ |
|
24 |
[DataMember] |
|
25 |
public string FileName { get; set; } |
|
26 |
[DataMember] |
|
27 |
public string Author { get; set; } |
|
28 |
[DataMember] |
|
29 |
public string Caption { get; set; } |
|
30 |
[DataMember] |
|
31 |
public byte[] Data { get; set; } |
|
32 |
} |
|
33 |
||
34 |
public class PerformanceTests : TestFixtureBase |
|
35 |
{ |
|
36 |
//private const int Iterations = 100; |
|
37 |
private const int Iterations = 5000; |
|
38 |
||
39 |
#region Data |
|
40 |
private const string BsonHex = |
|
41 |
@"A9-01-00-00-04-73-74-72-69-6E-67-73-00-2B-00-00-00-0A-30-00-02-31-00-19-00-00-00-4D-61-72-6B-75-73-20-65-67-67-65-72-20-5D-3E-3C-5B-2C-20-28-32-6E-64-29-00-0A-32-00-00-03-64-69-63-74-69-6F-6E-61-72-79-00-37-00-00-00-10-56-61-6C-20-26-20-61-73-64-31-00-01-00-00-00-10-56-61-6C-32-20-26-20-61-73-64-31-00-03-00-00-00-10-56-61-6C-33-20-26-20-61-73-64-31-00-04-00-00-00-00-02-4E-61-6D-65-00-05-00-00-00-52-69-63-6B-00-09-4E-6F-77-00-EF-BD-69-EC-25-01-00-00-01-42-69-67-4E-75-6D-62-65-72-00-E7-7B-CC-26-96-C7-1F-42-03-41-64-64-72-65-73-73-31-00-47-00-00-00-02-53-74-72-65-65-74-00-0B-00-00-00-66-66-66-20-53-74-72-65-65-74-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-6F-FF-31-53-26-01-00-00-00-04-41-64-64-72-65-73-73-65-73-00-A2-00-00-00-03-30-00-4B-00-00-00-02-53-74-72-65-65-74-00-0F-00-00-00-1F-61-72-72-61-79-3C-61-64-64-72-65-73-73-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-6F-73-0C-E7-25-01-00-00-00-03-31-00-4C-00-00-00-02-53-74-72-65-65-74-00-10-00-00-00-61-72-72-61-79-20-32-20-61-64-64-72-65-73-73-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-6F-17-E6-E1-25-01-00-00-00-00-00"; |
|
42 |
||
43 |
private const string BinaryFormatterHex = |
|
44 |
@"00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-0C-02-00-00-00-4C-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2C-20-56-65-72-73-69-6F-6E-3D-33-2E-35-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-05-01-00-00-00-1F-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-54-65-73-74-43-6C-61-73-73-07-00-00-00-05-5F-4E-61-6D-65-04-5F-4E-6F-77-0A-5F-42-69-67-4E-75-6D-62-65-72-09-5F-41-64-64-72-65-73-73-31-0A-5F-41-64-64-72-65-73-73-65-73-07-73-74-72-69-6E-67-73-0A-64-69-63-74-69-6F-6E-61-72-79-01-00-00-04-03-03-03-0D-05-1D-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-02-00-00-00-90-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-2C-20-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2C-20-56-65-72-73-69-6F-6E-3D-33-2E-35-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-5D-5D-7F-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-E1-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-44-69-63-74-69-6F-6E-61-72-79-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-02-00-00-00-06-03-00-00-00-04-52-69-63-6B-B6-25-3A-D1-C5-59-CC-88-0F-33-34-31-32-33-31-32-33-31-32-33-2E-31-32-31-09-04-00-00-00-09-05-00-00-00-09-06-00-00-00-09-07-00-00-00-05-04-00-00-00-1D-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-03-00-00-00-07-5F-73-74-72-65-65-74-06-5F-50-68-6F-6E-65-08-5F-45-6E-74-65-72-65-64-01-01-00-0D-02-00-00-00-06-08-00-00-00-0A-66-66-66-20-53-74-72-65-65-74-06-09-00-00-00-0E-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-B6-BD-B8-BF-74-69-CC-88-04-05-00-00-00-90-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-2C-20-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2C-20-56-65-72-73-69-6F-6E-3D-33-2E-35-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-5D-5D-03-00-00-00-06-5F-69-74-65-6D-73-05-5F-73-69-7A-65-08-5F-76-65-72-73-69-6F-6E-04-00-00-1F-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-5B-5D-02-00-00-00-08-08-09-0A-00-00-00-02-00-00-00-02-00-00-00-04-06-00-00-00-7F-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4C-69-73-74-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-03-00-00-00-06-5F-69-74-65-6D-73-05-5F-73-69-7A-65-08-5F-76-65-72-73-69-6F-6E-06-00-00-08-08-09-0B-00-00-00-03-00-00-00-03-00-00-00-04-07-00-00-00-E1-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-44-69-63-74-69-6F-6E-61-72-79-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-04-00-00-00-07-56-65-72-73-69-6F-6E-08-43-6F-6D-70-61-72-65-72-08-48-61-73-68-53-69-7A-65-0D-4B-65-79-56-61-6C-75-65-50-61-69-72-73-00-03-00-03-08-92-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-47-65-6E-65-72-69-63-45-71-75-61-6C-69-74-79-43-6F-6D-70-61-72-65-72-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-08-E5-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4B-65-79-56-61-6C-75-65-50-61-69-72-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-5B-5D-03-00-00-00-09-0C-00-00-00-03-00-00-00-09-0D-00-00-00-07-0A-00-00-00-00-01-00-00-00-04-00-00-00-04-1D-4E-65-77-74-6F-6E-73-6F-66-74-2E-4A-73-6F-6E-2E-54-65-73-74-73-2E-41-64-64-72-65-73-73-02-00-00-00-09-0E-00-00-00-09-0F-00-00-00-0D-02-11-0B-00-00-00-04-00-00-00-0A-06-10-00-00-00-18-4D-61-72-6B-75-73-20-65-67-67-65-72-20-5D-3E-3C-5B-2C-20-28-32-6E-64-29-0D-02-04-0C-00-00-00-92-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-47-65-6E-65-72-69-63-45-71-75-61-6C-69-74-79-43-6F-6D-70-61-72-65-72-60-31-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-00-00-00-00-07-0D-00-00-00-00-01-00-00-00-03-00-00-00-03-E3-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4B-65-79-56-61-6C-75-65-50-61-69-72-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-04-EF-FF-FF-FF-E3-01-53-79-73-74-65-6D-2E-43-6F-6C-6C-65-63-74-69-6F-6E-73-2E-47-65-6E-65-72-69-63-2E-4B-65-79-56-61-6C-75-65-50-61-69-72-60-32-5B-5B-53-79-73-74-65-6D-2E-53-74-72-69-6E-67-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-2C-5B-53-79-73-74-65-6D-2E-49-6E-74-33-32-2C-20-6D-73-63-6F-72-6C-69-62-2C-20-56-65-72-73-69-6F-6E-3D-32-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-62-37-37-61-35-63-35-36-31-39-33-34-65-30-38-39-5D-5D-02-00-00-00-03-6B-65-79-05-76-61-6C-75-65-01-00-08-06-12-00-00-00-0A-56-61-6C-20-26-20-61-73-64-31-01-00-00-00-01-ED-FF-FF-FF-EF-FF-FF-FF-06-14-00-00-00-0B-56-61-6C-32-20-26-20-61-73-64-31-03-00-00-00-01-EB-FF-FF-FF-EF-FF-FF-FF-06-16-00-00-00-0B-56-61-6C-33-20-26-20-61-73-64-31-04-00-00-00-01-0E-00-00-00-04-00-00-00-06-17-00-00-00-0E-1F-61-72-72-61-79-3C-61-64-64-72-65-73-73-09-09-00-00-00-B6-FD-0B-45-F4-58-CC-88-01-0F-00-00-00-04-00-00-00-06-19-00-00-00-0F-61-72-72-61-79-20-32-20-61-64-64-72-65-73-73-09-09-00-00-00-B6-3D-A2-1A-2B-58-CC-88-0B"; |
|
45 |
||
46 |
private const string XmlText = |
|
47 |
@"<TestClass xmlns=""http://schemas.datacontract.org/2004/07/Newtonsoft.Json.Tests"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Address1><Entered>2010-01-21T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>fff Street</Street></Address1><Addresses><Address><Entered>2009-12-31T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>array<address</Street></Address><Address><Entered>2009-12-30T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>array 2 address</Street></Address></Addresses><BigNumber>34123123123.121</BigNumber><Name>Rick</Name><Now>2010-01-01T12:12:16.0809174+13:00</Now><dictionary xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>Val & asd1</a:Key><a:Value>1</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>Val2 & asd1</a:Key><a:Value>3</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>Val3 & asd1</a:Key><a:Value>4</a:Value></a:KeyValueOfstringint></dictionary><strings xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string i:nil=""true""/><a:string>Markus egger ]><[, (2nd)</a:string><a:string i:nil=""true""/></strings></TestClass>"; |
|
48 |
||
49 |
private const string JsonText = |
|
50 |
@"{""strings"":[null,""Markus egger ]><[, (2nd)"",null],""dictionary"":{""Val & asd1"":1,""Val2 & asd1"":3,""Val3 & asd1"":4},""Name"":""Rick"",""Now"":""\/Date(1262301136080+1300)\/"",""BigNumber"":34123123123.121,""Address1"":{""Street"":""fff Street"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1264025536080+1300)\/""},""Addresses"":[{""Street"":""\u001farray<address"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1262211136080+1300)\/""},{""Street"":""array 2 address"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1262124736080+1300)\/""}]}"; |
|
51 |
||
52 |
public enum SerializeMethod |
|
53 |
{ |
|
54 |
JsonNet, |
|
55 |
JsonNetBinary, |
|
56 |
BinaryFormatter, |
|
57 |
JavaScriptSerializer, |
|
58 |
DataContractSerializer, |
|
59 |
DataContractJsonSerializer |
|
60 |
} |
|
61 |
#endregion |
|
62 |
||
63 |
[Test] |
|
64 |
public void Serialize() |
|
65 |
{ |
|
66 |
1 |
TestClass test = CreateSerializationObject();
|
67 |
||
68 |
1 |
BenchmarkSerializeMethod(SerializeMethod.DataContractSerializer, test);
|
69 |
1 |
BenchmarkSerializeMethod(SerializeMethod.BinaryFormatter, test);
|
70 |
1 |
BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, test);
|
71 |
1 |
BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, test);
|
72 |
1 |
BenchmarkSerializeMethod(SerializeMethod.JsonNet, test);
|
73 |
1 |
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, test);
|
74 |
1 |
}
|
75 |
||
76 |
[Test] |
|
77 |
public void Deserialize() |
|
78 |
{ |
|
79 |
1 |
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractSerializer, XmlText);
|
80 |
1 |
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.BinaryFormatter, MiscellaneousUtils.HexToBytes(BinaryFormatterHex));
|
81 |
1 |
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JavaScriptSerializer, JsonText);
|
82 |
1 |
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractJsonSerializer, JsonText);
|
83 |
1 |
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNet, JsonText);
|
84 |
1 |
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, MiscellaneousUtils.HexToBytes(BsonHex));
|
85 |
1 |
}
|
86 |
||
87 |
[Test] |
|
88 |
public void SerializeSizeNormal() |
|
89 |
{ |
|
90 |
1 |
SerializeSize(CreateSerializationObject());
|
91 |
1 |
}
|
92 |
||
93 |
[Test] |
|
94 |
public void SerializeSizeData() |
|
95 |
{ |
|
96 |
1 |
Image image = new Image();
|
97 |
1 |
image.Data = System.IO.File.ReadAllBytes(@"bunny_pancake.jpg");
|
98 |
1 |
image.FileName = "bunny_pancake.jpg";
|
99 |
1 |
image.Author = "Hironori Akutagawa";
|
100 |
1 |
image.Caption = "I have no idea what you are talking about so here's a bunny with a pancake on its head";
|
101 |
||
102 |
1 |
SerializeSize(image);
|
103 |
1 |
}
|
104 |
||
105 |
private T TimeOperation<T>(Func<T> operation, string name) |
|
106 |
{ |
|
107 |
// warm up |
|
108 |
10 |
operation();
|
109 |
||
110 |
10 |
Stopwatch timed = new Stopwatch();
|
111 |
10 |
timed.Start();
|
112 |
||
113 |
10 |
T result = operation();
|
114 |
||
115 |
10 |
Console.WriteLine(name);
|
116 |
10 |
Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
|
117 |
||
118 |
10 |
timed.Stop();
|
119 |
||
120 |
10 |
return result;
|
121 |
10 |
}
|
122 |
||
123 |
private void SerializeSize(object value) |
|
124 |
{ |
|
125 |
// this is extremely slow with 5000 interations |
|
126 |
2 |
int interations = 100;
|
127 |
||
128 |
2 |
byte[] jsonBytes = TimeOperation(() =>
|
129 |
2 |
{
|
130 |
2 |
string json = null;
|
131 |
2 |
for (int i = 0; i < interations; i++) |
132 |
2 |
{
|
133 |
2 |
json = JsonConvert.SerializeObject(value, Formatting.None);
|
134 |
2 |
}
|
135 |
2 |
|
136 |
2 |
return Encoding.UTF8.GetBytes(json);
|
137 |
2 |
}, "Json.NET");
|
138 |
||
139 |
2 |
byte[] bsonBytes = TimeOperation(() =>
|
140 |
2 |
{
|
141 |
2 |
MemoryStream ms = null;
|
142 |
2 |
for (int i = 0; i < interations; i++) |
143 |
2 |
{
|
144 |
2 |
ms = new MemoryStream();
|
145 |
2 |
JsonSerializer serializer = new JsonSerializer();
|
146 |
2 |
BsonWriter writer = new BsonWriter(ms);
|
147 |
2 |
|
148 |
2 |
serializer.Serialize(writer, value);
|
149 |
2 |
writer.Flush();
|
150 |
2 |
}
|
151 |
2 |
|
152 |
2 |
return ms.ToArray();
|
153 |
2 |
}, "Json.NET BSON");
|
154 |
||
155 |
2 |
byte[] xmlBytes = TimeOperation(() =>
|
156 |
2 |
{
|
157 |
2 |
MemoryStream ms = null;
|
158 |
2 |
for (int i = 0; i < interations; i++) |
159 |
2 |
{
|
160 |
2 |
ms = new MemoryStream();
|
161 |
2 |
DataContractSerializer dataContractSerializer = new DataContractSerializer(value.GetType());
|
162 |
2 |
dataContractSerializer.WriteObject(ms, value);
|
163 |
2 |
}
|
164 |
2 |
|
165 |
2 |
return ms.ToArray();
|
166 |
2 |
}, "DataContractSerializer");
|
167 |
||
168 |
2 |
byte[] wcfJsonBytes = TimeOperation(() =>
|
169 |
2 |
{
|
170 |
2 |
MemoryStream ms = null;
|
171 |
2 |
for (int i = 0; i < interations; i++) |
172 |
2 |
{
|
173 |
2 |
ms = new MemoryStream();
|
174 |
2 |
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(value.GetType());
|
175 |
2 |
dataContractJsonSerializer.WriteObject(ms, value);
|
176 |
2 |
}
|
177 |
2 |
|
178 |
2 |
return ms.ToArray();
|
179 |
2 |
}, "DataContractJsonSerializer");
|
180 |
||
181 |
2 |
byte[] binaryFormatterBytes = TimeOperation(() =>
|
182 |
2 |
{
|
183 |
2 |
MemoryStream ms = null;
|
184 |
2 |
for (int i = 0; i < interations; i++) |
185 |
2 |
{
|
186 |
2 |
ms = new MemoryStream();
|
187 |
2 |
BinaryFormatter formatter = new BinaryFormatter();
|
188 |
2 |
formatter.Serialize(ms, value);
|
189 |
2 |
}
|
190 |
2 |
|
191 |
2 |
return ms.ToArray();
|
192 |
2 |
}, "BinaryFormatter");
|
193 |
||
194 |
2 |
Console.WriteLine("Json.NET size: {0} bytes", jsonBytes.Length);
|
195 |
2 |
Console.WriteLine("BSON size: {0} bytes", bsonBytes.Length);
|
196 |
2 |
Console.WriteLine("WCF JSON size: {0} bytes", wcfJsonBytes.Length);
|
197 |
2 |
Console.WriteLine("WCF XML size: {0} bytes", xmlBytes.Length);
|
198 |
2 |
Console.WriteLine("BinaryFormatter size: {0} bytes", binaryFormatterBytes.Length);
|
199 |
2 |
}
|
200 |
||
201 |
#region Serialize |
|
202 |
1 |
private static readonly byte[] Buffer = new byte[4096];
|
203 |
||
204 |
public void BenchmarkSerializeMethod(SerializeMethod method, object value) |
|
205 |
{ |
|
206 |
7 |
Serialize(method, value);
|
207 |
||
208 |
7 |
Stopwatch timed = new Stopwatch();
|
209 |
7 |
timed.Start();
|
210 |
||
211 |
7 |
string json = null;
|
212 |
7 |
for (int x = 0; x < Iterations; x++) |
213 |
{ |
|
214 |
35000 |
json = Serialize(method, value);
|
215 |
} |
|
216 |
||
217 |
7 |
timed.Stop();
|
218 |
||
219 |
7 |
Console.WriteLine("Serialize method: {0}", method);
|
220 |
7 |
Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
|
221 |
7 |
Console.WriteLine(json);
|
222 |
7 |
Console.WriteLine();
|
223 |
7 |
}
|
224 |
||
225 |
private TestClass CreateSerializationObject() |
|
226 |
{ |
|
227 |
2 |
TestClass test = new TestClass();
|
228 |
||
229 |
2 |
test.dictionary = new Dictionary<string, int> { { "Val & asd1", 1 }, { "Val2 & asd1", 3 }, { "Val3 & asd1", 4 } };
|
230 |
||
231 |
||
232 |
2 |
test.Address1.Street = "fff Street";
|
233 |
2 |
test.Address1.Entered = DateTime.Now.AddDays(20);
|
234 |
||
235 |
2 |
test.BigNumber = 34123123123.121M;
|
236 |
2 |
test.Now = DateTime.Now.AddHours(1);
|
237 |
2 |
test.strings = new List<string>() { null, "Markus egger ]><[, (2nd)", null };
|
238 |
||
239 |
2 |
Address address = new Address();
|
240 |
2 |
address.Entered = DateTime.Now.AddDays(-1);
|
241 |
2 |
address.Street = "\u001farray\u003caddress";
|
242 |
||
243 |
2 |
test.Addresses.Add(address);
|
244 |
||
245 |
2 |
address = new Address();
|
246 |
2 |
address.Entered = DateTime.Now.AddDays(-2);
|
247 |
2 |
address.Street = "array 2 address";
|
248 |
2 |
test.Addresses.Add(address);
|
249 |
2 |
return test;
|
250 |
2 |
}
|
251 |
||
252 |
public string SerializeJsonNet(object value) |
|
253 |
{ |
|
254 |
0 |
Type type = value.GetType();
|
255 |
||
256 |
0 |
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
|
257 |
||
258 |
0 |
json.NullValueHandling = NullValueHandling.Ignore;
|
259 |
||
260 |
0 |
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
|
261 |
0 |
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
|
262 |
0 |
json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
263 |
||
264 |
||
265 |
0 |
StringWriter sw = new StringWriter();
|
266 |
0 |
Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
|
267 |
||
268 |
0 |
writer.Formatting = Formatting.None;
|
269 |
||
270 |
0 |
writer.QuoteChar = '"';
|
271 |
0 |
json.Serialize(writer, value);
|
272 |
||
273 |
0 |
string output = sw.ToString();
|
274 |
0 |
writer.Close();
|
275 |
||
276 |
0 |
return output;
|
277 |
0 |
}
|
278 |
||
279 |
public string SerializeWebExtensions(object value) |
|
280 |
{ |
|
281 |
5001 |
JavaScriptSerializer ser = new JavaScriptSerializer();
|
282 |
||
283 |
5001 |
return ser.Serialize(value);
|
284 |
5001 |
}
|
285 |
||
286 |
public string SerializeDataContractJson(object value) |
|
287 |
{ |
|
288 |
5001 |
DataContractJsonSerializer dataContractSerializer
|
289 |
5001 |
= new DataContractJsonSerializer(value.GetType());
|
290 |
||
291 |
5001 |
MemoryStream ms = new MemoryStream();
|
292 |
5001 |
dataContractSerializer.WriteObject(ms, value);
|
293 |
||
294 |
5001 |
ms.Seek(0, SeekOrigin.Begin);
|
295 |
||
296 |
5001 |
using (StreamReader sr = new StreamReader(ms))
|
297 |
{ |
|
298 |
5001 |
return sr.ReadToEnd();
|
299 |
} |
|
300 |
5001 |
}
|
301 |
||
302 |
public string SerializeDataContract(object value) |
|
303 |
{ |
|
304 |
5001 |
DataContractSerializer dataContractSerializer
|
305 |
5001 |
= new DataContractSerializer(value.GetType());
|
306 |
||
307 |
5001 |
MemoryStream ms = new MemoryStream();
|
308 |
5001 |
dataContractSerializer.WriteObject(ms, value);
|
309 |
||
310 |
5001 |
ms.Seek(0, SeekOrigin.Begin);
|
311 |
||
312 |
5001 |
using (StreamReader sr = new StreamReader(ms))
|
313 |
{ |
|
314 |
5001 |
return sr.ReadToEnd();
|
315 |
} |
|
316 |
5001 |
}
|
317 |
||
318 |
private string Serialize(SerializeMethod method, object value) |
|
319 |
{ |
|
320 |
string json; |
|
321 |
||
322 |
35007 |
switch (method)
|
323 |
{ |
|
324 |
case SerializeMethod.JsonNet: |
|
325 |
5001 |
json = JsonConvert.SerializeObject(value);
|
326 |
5001 |
break;
|
327 |
case SerializeMethod.JsonNetBinary: |
|
328 |
{ |
|
329 |
10002 |
MemoryStream ms = new MemoryStream(Buffer);
|
330 |
10002 |
JsonSerializer serializer = new JsonSerializer();
|
331 |
10002 |
BsonWriter writer = new BsonWriter(ms);
|
332 |
10002 |
serializer.Serialize(writer, value);
|
333 |
||
334 |
//json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position); |
|
335 |
10002 |
json = "Bytes = " + ms.Position;
|
336 |
10002 |
break;
|
337 |
} |
|
338 |
case SerializeMethod.JavaScriptSerializer: |
|
339 |
5001 |
json = SerializeWebExtensions(value);
|
340 |
5001 |
break;
|
341 |
case SerializeMethod.DataContractJsonSerializer: |
|
342 |
5001 |
json = SerializeDataContractJson(value);
|
343 |
5001 |
break;
|
344 |
case SerializeMethod.DataContractSerializer: |
|
345 |
5001 |
json = SerializeDataContract(value);
|
346 |
5001 |
break;
|
347 |
case SerializeMethod.BinaryFormatter: |
|
348 |
5001 |
json = SerializeBinaryFormatter(value);
|
349 |
5001 |
break;
|
350 |
default: |
|
351 |
0 |
throw new ArgumentOutOfRangeException("method");
|
352 |
} |
|
353 |
||
354 |
35007 |
return json;
|
355 |
35007 |
}
|
356 |
||
357 |
private string SerializeBinaryFormatter(object value) |
|
358 |
{ |
|
359 |
string json; |
|
360 |
5001 |
MemoryStream ms = new MemoryStream(Buffer);
|
361 |
5001 |
BinaryFormatter formatter = new BinaryFormatter();
|
362 |
5001 |
formatter.Serialize(ms, value);
|
363 |
||
364 |
5001 |
json = "Bytes = " + ms.Position;
|
365 |
//json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position); |
|
366 |
5001 |
return json;
|
367 |
5001 |
}
|
368 |
#endregion |
|
369 |
||
370 |
#region Deserialize |
|
371 |
public void BenchmarkDeserializeMethod<T>(SerializeMethod method, object json) |
|
372 |
{ |
|
373 |
6 |
Deserialize<T>(method, json);
|
374 |
||
375 |
6 |
Stopwatch timed = new Stopwatch();
|
376 |
6 |
timed.Start();
|
377 |
||
378 |
6 |
T value = default(T);
|
379 |
6 |
for (int x = 0; x < Iterations; x++) |
380 |
{ |
|
381 |
30000 |
value = Deserialize<T>(method, json);
|
382 |
} |
|
383 |
||
384 |
6 |
timed.Stop();
|
385 |
||
386 |
6 |
Console.WriteLine("Deserialize method: {0}", method);
|
387 |
6 |
Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
|
388 |
6 |
Console.WriteLine(value);
|
389 |
6 |
Console.WriteLine();
|
390 |
6 |
}
|
391 |
||
392 |
public T DeserializeJsonNet<T>(string json) |
|
393 |
{ |
|
394 |
5001 |
Type type = typeof(T);
|
395 |
||
396 |
5001 |
JsonSerializer serializer = new JsonSerializer();
|
397 |
5001 |
serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
|
398 |
5001 |
serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
|
399 |
5001 |
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
400 |
||
401 |
5001 |
return (T)serializer.Deserialize(new StringReader(json), type);
|
402 |
||
403 |
//JsonTextReader reader = new JsonTextReader(new StringReader(JsonText)); |
|
404 |
//while (reader.Read()) |
|
405 |
//{ |
|
406 |
||
407 |
//} |
|
408 |
//return default(T); |
|
409 |
5001 |
}
|
410 |
||
411 |
public T DeserializeJsonNetBinary<T>(byte[] bson) |
|
412 |
{ |
|
413 |
5001 |
Type type = typeof(T);
|
414 |
||
415 |
5001 |
JsonSerializer serializer = new JsonSerializer();
|
416 |
5001 |
serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
|
417 |
5001 |
serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
|
418 |
5001 |
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
419 |
||
420 |
5001 |
return (T)serializer.Deserialize(new BsonReader(new MemoryStream(bson)), type);
|
421 |
5001 |
}
|
422 |
||
423 |
public T DeserializeWebExtensions<T>(string json) |
|
424 |
{ |
|
425 |
5001 |
JavaScriptSerializer ser = new JavaScriptSerializer();
|
426 |
||
427 |
5001 |
return ser.Deserialize<T>(json);
|
428 |
5001 |
}
|
429 |
||
430 |
public T DeserializeDataContractJson<T>(string json) |
|
431 |
{ |
|
432 |
5001 |
DataContractJsonSerializer dataContractSerializer
|
433 |
5001 |
= new DataContractJsonSerializer(typeof(T));
|
434 |
||
435 |
5001 |
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
|
436 |
||
437 |
5001 |
return (T)dataContractSerializer.ReadObject(ms);
|
438 |
5001 |
}
|
439 |
||
440 |
private T Deserialize<T>(SerializeMethod method, object json) |
|
441 |
{ |
|
442 |
30006 |
switch (method)
|
443 |
{ |
|
444 |
case SerializeMethod.JsonNet: |
|
445 |
5001 |
return DeserializeJsonNet<T>((string) json);
|
446 |
case SerializeMethod.JsonNetBinary: |
|
447 |
5001 |
return DeserializeJsonNetBinary<T>((byte[]) json);
|
448 |
case SerializeMethod.BinaryFormatter: |
|
449 |
5001 |
return DeserializeBinaryFormatter<T>((byte[]) json);
|
450 |
case SerializeMethod.JavaScriptSerializer: |
|
451 |
5001 |
return DeserializeWebExtensions<T>((string) json);
|
452 |
case SerializeMethod.DataContractSerializer: |
|
453 |
5001 |
return DeserializeDataContract<T>((string) json);
|
454 |
case SerializeMethod.DataContractJsonSerializer: |
|
455 |
5001 |
return DeserializeDataContractJson<T>((string) json);
|
456 |
default: |
|
457 |
0 |
throw new ArgumentOutOfRangeException("method");
|
458 |
} |
|
459 |
30006 |
}
|
460 |
||
461 |
private T DeserializeDataContract<T>(string xml) |
|
462 |
{ |
|
463 |
5001 |
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));
|
464 |
||
465 |
5001 |
DataContractSerializer serializer = new DataContractSerializer(typeof (T));
|
466 |
5001 |
return (T) serializer.ReadObject(ms);
|
467 |
5001 |
}
|
468 |
||
469 |
private T DeserializeBinaryFormatter<T>(byte[] bytes) |
|
470 |
{ |
|
471 |
5001 |
BinaryFormatter formatter = new BinaryFormatter();
|
472 |
5001 |
return (T)formatter.Deserialize(new MemoryStream(bytes));
|
473 |
5001 |
}
|
474 |
#endregion |
|
475 |
||
476 |
||
477 |
||
478 |
[Test] |
|
479 |
public void SerializeLargeObject() |
|
480 |
{ |
|
481 |
1 |
LargeRecursiveTestClass rootValue = null;
|
482 |
1 |
LargeRecursiveTestClass parentValue = null;
|
483 |
1 |
for (int i = 0; i < 20; i++) |
484 |
{ |
|
485 |
20 |
LargeRecursiveTestClass currentValue = new LargeRecursiveTestClass()
|
486 |
20 |
{
|
487 |
20 |
Integer = int.MaxValue,
|
488 |
20 |
Text = "The quick red fox jumped over the lazy dog."
|
489 |
20 |
};
|
490 |
|
|
491 |
20 |
if (rootValue == null)
|
492 |
1 |
rootValue = currentValue;
|
493 |
20 |
if (parentValue != null)
|
494 |
19 |
parentValue.Child = currentValue;
|
495 |
||
496 |
20 |
parentValue = currentValue;
|
497 |
} |
|
498 |
||
499 |
1 |
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, rootValue);
|
500 |
1 |
}
|
501 |
} |
|
502 |
||
503 |
public class LargeRecursiveTestClass |
|
504 |
{ |
|
505 |
public LargeRecursiveTestClass Child { get; set; } |
|
506 |
public string Text { get; set; } |
|
507 |
public int Integer { get; set; } |
|
508 |
} |
|
509 |
||
510 |
#region Classes |
|
511 |
[Serializable] |
|
512 |
[DataContract] |
|
513 |
public class TestClass |
|
514 |
{ |
|
515 |
[DataMember] |
|
516 |
public string Name |
|
517 |
{ |
|
518 |
25805 |
get { return _Name; }
|
519 |
25005 |
set { _Name = value; }
|
520 |
} |
|
521 |
15005 |
private string _Name = "Rick";
|
522 |
||
523 |
[DataMember] |
|
524 |
public DateTime Now |
|
525 |
{ |
|
526 |
25805 |
get { return _Now; }
|
527 |
25007 |
set { _Now = value; }
|
528 |
} |
|
529 |
15005 |
private DateTime _Now = DateTime.Now;
|
530 |
||
531 |
[DataMember] |
|
532 |
public decimal BigNumber |
|
533 |
{ |
|
534 |
25805 |
get { return _BigNumber; }
|
535 |
25007 |
set { _BigNumber = value; }
|
536 |
} |
|
537 |
15005 |
private decimal _BigNumber = 1212121.22M;
|
538 |
||
539 |
[DataMember] |
|
540 |
public Address Address1 |
|
541 |
{ |
|
542 |
25809 |
get { return _Address1; }
|
543 |
25005 |
set { _Address1 = value; }
|
544 |
} |
|
545 |
15005 |
private Address _Address1 = new Address();
|
546 |
||
547 |
||
548 |
||
549 |
[DataMember] |
|
550 |
public List<Address> Addresses |
|
551 |
{ |
|
552 |
25809 |
get { return _Addresses; }
|
553 |
25005 |
set { _Addresses = value; }
|
554 |
} |
|
555 |
15005 |
private List<Address> _Addresses = new List<Address>();
|
556 |
||
557 |
15005 |
[DataMember]
|
558 |
15005 |
public List<string> strings = new List<string>();
|
559 |
||
560 |
15005 |
[DataMember]
|
561 |
15005 |
public Dictionary<string, int> dictionary = new Dictionary<string, int>();
|
562 |
} |
|
563 |
||
564 |
[Serializable] |
|
565 |
[DataContract] |
|
566 |
public class Address |
|
567 |
{ |
|
568 |
[DataMember] |
|
569 |
public string Street |
|
570 |
{ |
|
571 |
77415 |
get { return _street; }
|
572 |
75021 |
set { _street = value; }
|
573 |
} |
|
574 |
60018 |
private string _street = "32 Kaiea";
|
575 |
||
576 |
[DataMember] |
|
577 |
public string Phone |
|
578 |
{ |
|
579 |
77415 |
get { return _Phone; }
|
580 |
75015 |
set { _Phone = value; }
|
581 |
} |
|
582 |
60018 |
private string _Phone = "(503) 814-6335";
|
583 |
||
584 |
[DataMember] |
|
585 |
public DateTime Entered |
|
586 |
{ |
|
587 |
77415 |
get { return _Entered; }
|
588 |
75021 |
set { _Entered = value; }
|
589 |
} |
|
590 |
60018 |
private DateTime _Entered = DateTime.Parse("01/01/2007", CultureInfo.CurrentCulture.DateTimeFormat);
|
591 |
} |
|
592 |
#endregion |
|
593 |
} |
|
594 |
#endif |