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