Skip to content

Commit 35fef82

Browse files
committed
Merge branch 'master' of github.com:rpgmaker/NetJSON
2 parents ccccdad + c3e5e19 commit 35fef82

File tree

7 files changed

+178
-31
lines changed

7 files changed

+178
-31
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using BenchmarkDotNet.Attributes;
2+
using System.Text.Json;
3+
using static System.Console;
4+
5+
namespace NetJSON.Benchmark.Net5_0
6+
{
7+
public abstract class Benchmark<TObj>
8+
{
9+
private readonly TObj obj;
10+
11+
protected Benchmark(TObj instance)
12+
{
13+
obj = instance;
14+
WriteLine("NetJSON: " + NetJSON.Serialize(instance));
15+
WriteLine("MS JSON: " + JsonSerializer.Serialize(instance));
16+
WriteLine("UTFJSON: " + System.Text.Encoding.UTF8.GetString(Utf8Json.JsonSerializer.Serialize(instance)));
17+
}
18+
19+
[Benchmark]
20+
public string NetJson() => NetJSON.Serialize(obj);
21+
22+
[Benchmark]
23+
public string MicrosoftJson() => JsonSerializer.Serialize(obj);
24+
25+
[Benchmark]
26+
public byte[] FastestUtf8Json() => Utf8Json.JsonSerializer.Serialize(obj);
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace NetJSON.Benchmark.Net5_0.Models
8+
{
9+
public class Int32ArrayBenchmark : Benchmark<int[]>
10+
{
11+
public Int32ArrayBenchmark() : base(new int[] { 0, 1, 2, Int32.MinValue, Int32.MaxValue, -1 }) {
12+
}
13+
}
14+
15+
public class SingleArrayBenchmark : Benchmark<float[]>
16+
{
17+
public SingleArrayBenchmark() : base(new float[] { 0, 1, 2, Single.MinValue, Single.MaxValue, (float)Math.PI, (float)Math.E }) {
18+
}
19+
}
20+
21+
public class DoubleArrayBenchmark : Benchmark<double[]>
22+
{
23+
public DoubleArrayBenchmark() : base(new double[] { 0, 1, 2, Double.MinValue, Double.MaxValue, Math.PI, Math.E }) {
24+
}
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace NetJSON.Benchmark.Net5_0.Models
8+
{
9+
public class StringDictionaryBenchmark : Benchmark<Dictionary<string, string>>
10+
{
11+
public StringDictionaryBenchmark() : base(new Dictionary<string, string> {
12+
{ "name", "item 1" },
13+
{ "address", "Max, 12, PYM, LO" },
14+
{ "receiver", "Mike" },
15+
{ "unit", "US$" }
16+
}) {
17+
18+
}
19+
}
20+
public class StringObjectDictionaryBenchmark : Benchmark<Dictionary<string, object>>
21+
{
22+
public StringObjectDictionaryBenchmark() : base(new Dictionary<string, object> {
23+
{ "name", "item 1" },
24+
{ "time", DateTime.Now },
25+
{ "address", "Max, 12, PYM, LO" },
26+
{ "receiver", "Mike" },
27+
{ "price", 5.8 },
28+
{ "unit", "US$" }
29+
}) {
30+
31+
}
32+
}
33+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NetJSON.Benchmark.Net5_0.Models
4+
{
5+
public class GuidBenchmark : Benchmark<Guid>
6+
{
7+
public GuidBenchmark() : base(Guid.NewGuid()) {
8+
}
9+
}
10+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace NetJSON.Benchmark.Net5_0
2+
{
3+
public class SimpleObject
4+
{
5+
public string Name { get; set; }
6+
public int ID { get; set; }
7+
}
8+
9+
public class SimpleObjectBenchmark : Benchmark<SimpleObject>
10+
{
11+
public SimpleObjectBenchmark() : base (new SimpleObject { ID = 10, Name = "Performance" }) {
12+
}
13+
}
14+
15+
public class Int32Benchmark : Benchmark<int>
16+
{
17+
public Int32Benchmark() : base(3) {
18+
}
19+
}
20+
21+
public class BoxedInt32Benchmark : Benchmark<object>
22+
{
23+
public BoxedInt32Benchmark() : base(3) {
24+
}
25+
}
26+
27+
public class StringObjectBenchmark : Benchmark<string>
28+
{
29+
public StringObjectBenchmark() : base("Hello world") {
30+
}
31+
}
32+
}

NetJSON.Benchmark.Net5_0/NetJSON.Benchmark.Net5_0.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,58 @@
1-
using BenchmarkDotNet.Attributes;
2-
using BenchmarkDotNet.Running;
1+
using BenchmarkDotNet.Running;
32
using System;
4-
using System.Text.Json;
3+
using NetJSON.Benchmark.Net5_0.Models;
4+
using static System.Console;
55

66
namespace NetJSON.Benchmark.Net5_0
77
{
8-
public class SimpleObject
9-
{
10-
public string Name { get; set; }
11-
public int ID { get; set; }
12-
}
13-
14-
public class SimpleObjectBenchmark
15-
{
16-
private readonly SimpleObject obj;
17-
18-
public SimpleObjectBenchmark()
19-
{
20-
obj = new SimpleObject { ID = 10, Name = "Performance" };
21-
}
22-
23-
[Benchmark]
24-
public string NetJson() => NetJSON.Serialize(obj);
25-
26-
[Benchmark]
27-
public string MicrosoftJson() => JsonSerializer.Serialize(obj);
28-
29-
[Benchmark]
30-
public byte[] FastestUtf8Json() => Utf8Json.JsonSerializer.Serialize(obj);
31-
}
32-
33-
class Program
8+
static class Program
349
{
3510
static void Main(string[] args)
3611
{
37-
var summary = BenchmarkRunner.Run<SimpleObjectBenchmark>();
12+
WriteLine("1. Simple object");
13+
WriteLine("2. GUID");
14+
WriteLine("3. Int32 values");
15+
WriteLine("4. Single values");
16+
WriteLine("5. Double values");
17+
WriteLine("6. String dictionary");
18+
WriteLine("7. String-object dictionary");
19+
WriteLine("8. Int32-object");
20+
WriteLine("9. String-object");
21+
WriteLine("Type the benchmark number and press Enter key: ");
22+
if (Int32.TryParse(ReadLine(), out int index)) {
23+
switch (index) {
24+
case 1:
25+
BenchmarkRunner.Run<SimpleObjectBenchmark>();
26+
return;
27+
case 2:
28+
BenchmarkRunner.Run<GuidBenchmark>();
29+
return;
30+
case 3:
31+
BenchmarkRunner.Run<Int32ArrayBenchmark>();
32+
return;
33+
case 4:
34+
BenchmarkRunner.Run<SingleArrayBenchmark>();
35+
return;
36+
case 5:
37+
BenchmarkRunner.Run<DoubleArrayBenchmark>();
38+
return;
39+
case 6:
40+
BenchmarkRunner.Run<StringDictionaryBenchmark>();
41+
return;
42+
case 7:
43+
BenchmarkRunner.Run<StringObjectDictionaryBenchmark>();
44+
return;
45+
case 8:
46+
BenchmarkRunner.Run<Int32Benchmark>();
47+
BenchmarkRunner.Run<BoxedInt32Benchmark>();
48+
return;
49+
case 9:
50+
BenchmarkRunner.Run<StringObjectBenchmark>();
51+
return;
52+
default:
53+
break;
54+
}
55+
}
3856
}
3957
}
4058
}

0 commit comments

Comments
 (0)