Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rpgmaker/NetJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
rpgmaker committed Jul 20, 2022
2 parents ccccdad + c3e5e19 commit 35fef82
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 31 deletions.
28 changes: 28 additions & 0 deletions NetJSON.Benchmark.Net5_0/Benchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using BenchmarkDotNet.Attributes;
using System.Text.Json;
using static System.Console;

namespace NetJSON.Benchmark.Net5_0
{
public abstract class Benchmark<TObj>
{
private readonly TObj obj;

protected Benchmark(TObj instance)
{
obj = instance;
WriteLine("NetJSON: " + NetJSON.Serialize(instance));
WriteLine("MS JSON: " + JsonSerializer.Serialize(instance));
WriteLine("UTFJSON: " + System.Text.Encoding.UTF8.GetString(Utf8Json.JsonSerializer.Serialize(instance)));
}

[Benchmark]
public string NetJson() => NetJSON.Serialize(obj);

[Benchmark]
public string MicrosoftJson() => JsonSerializer.Serialize(obj);

[Benchmark]
public byte[] FastestUtf8Json() => Utf8Json.JsonSerializer.Serialize(obj);
}
}
26 changes: 26 additions & 0 deletions NetJSON.Benchmark.Net5_0/Models/ArraysBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetJSON.Benchmark.Net5_0.Models
{
public class Int32ArrayBenchmark : Benchmark<int[]>
{
public Int32ArrayBenchmark() : base(new int[] { 0, 1, 2, Int32.MinValue, Int32.MaxValue, -1 }) {
}
}

public class SingleArrayBenchmark : Benchmark<float[]>
{
public SingleArrayBenchmark() : base(new float[] { 0, 1, 2, Single.MinValue, Single.MaxValue, (float)Math.PI, (float)Math.E }) {
}
}

public class DoubleArrayBenchmark : Benchmark<double[]>
{
public DoubleArrayBenchmark() : base(new double[] { 0, 1, 2, Double.MinValue, Double.MaxValue, Math.PI, Math.E }) {
}
}
}
33 changes: 33 additions & 0 deletions NetJSON.Benchmark.Net5_0/Models/DictionariesBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetJSON.Benchmark.Net5_0.Models
{
public class StringDictionaryBenchmark : Benchmark<Dictionary<string, string>>
{
public StringDictionaryBenchmark() : base(new Dictionary<string, string> {
{ "name", "item 1" },
{ "address", "Max, 12, PYM, LO" },
{ "receiver", "Mike" },
{ "unit", "US$" }
}) {

}
}
public class StringObjectDictionaryBenchmark : Benchmark<Dictionary<string, object>>
{
public StringObjectDictionaryBenchmark() : base(new Dictionary<string, object> {
{ "name", "item 1" },
{ "time", DateTime.Now },
{ "address", "Max, 12, PYM, LO" },
{ "receiver", "Mike" },
{ "price", 5.8 },
{ "unit", "US$" }
}) {

}
}
}
10 changes: 10 additions & 0 deletions NetJSON.Benchmark.Net5_0/Models/GuidBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NetJSON.Benchmark.Net5_0.Models
{
public class GuidBenchmark : Benchmark<Guid>
{
public GuidBenchmark() : base(Guid.NewGuid()) {
}
}
}
32 changes: 32 additions & 0 deletions NetJSON.Benchmark.Net5_0/Models/SimpleObjectBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace NetJSON.Benchmark.Net5_0
{
public class SimpleObject
{
public string Name { get; set; }
public int ID { get; set; }
}

public class SimpleObjectBenchmark : Benchmark<SimpleObject>
{
public SimpleObjectBenchmark() : base (new SimpleObject { ID = 10, Name = "Performance" }) {
}
}

public class Int32Benchmark : Benchmark<int>
{
public Int32Benchmark() : base(3) {
}
}

public class BoxedInt32Benchmark : Benchmark<object>
{
public BoxedInt32Benchmark() : base(3) {
}
}

public class StringObjectBenchmark : Benchmark<string>
{
public StringObjectBenchmark() : base("Hello world") {
}
}
}
2 changes: 1 addition & 1 deletion NetJSON.Benchmark.Net5_0/NetJSON.Benchmark.Net5_0.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
78 changes: 48 additions & 30 deletions NetJSON.Benchmark.Net5_0/Program.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Running;
using System;
using System.Text.Json;
using NetJSON.Benchmark.Net5_0.Models;
using static System.Console;

namespace NetJSON.Benchmark.Net5_0
{
public class SimpleObject
{
public string Name { get; set; }
public int ID { get; set; }
}

public class SimpleObjectBenchmark
{
private readonly SimpleObject obj;

public SimpleObjectBenchmark()
{
obj = new SimpleObject { ID = 10, Name = "Performance" };
}

[Benchmark]
public string NetJson() => NetJSON.Serialize(obj);

[Benchmark]
public string MicrosoftJson() => JsonSerializer.Serialize(obj);

[Benchmark]
public byte[] FastestUtf8Json() => Utf8Json.JsonSerializer.Serialize(obj);
}

class Program
static class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<SimpleObjectBenchmark>();
WriteLine("1. Simple object");
WriteLine("2. GUID");
WriteLine("3. Int32 values");
WriteLine("4. Single values");
WriteLine("5. Double values");
WriteLine("6. String dictionary");
WriteLine("7. String-object dictionary");
WriteLine("8. Int32-object");
WriteLine("9. String-object");
WriteLine("Type the benchmark number and press Enter key: ");
if (Int32.TryParse(ReadLine(), out int index)) {
switch (index) {
case 1:
BenchmarkRunner.Run<SimpleObjectBenchmark>();
return;
case 2:
BenchmarkRunner.Run<GuidBenchmark>();
return;
case 3:
BenchmarkRunner.Run<Int32ArrayBenchmark>();
return;
case 4:
BenchmarkRunner.Run<SingleArrayBenchmark>();
return;
case 5:
BenchmarkRunner.Run<DoubleArrayBenchmark>();
return;
case 6:
BenchmarkRunner.Run<StringDictionaryBenchmark>();
return;
case 7:
BenchmarkRunner.Run<StringObjectDictionaryBenchmark>();
return;
case 8:
BenchmarkRunner.Run<Int32Benchmark>();
BenchmarkRunner.Run<BoxedInt32Benchmark>();
return;
case 9:
BenchmarkRunner.Run<StringObjectBenchmark>();
return;
default:
break;
}
}
}
}
}

0 comments on commit 35fef82

Please sign in to comment.