-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:rpgmaker/NetJSON
- Loading branch information
Showing
7 changed files
with
178 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$" } | ||
}) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |