Created
May 13, 2022 09:01
-
-
Save greymistcube/a1e63e5e1f0a160ed9ecf0697115be33 to your computer and use it in GitHub Desktop.
`DataModel` benchmark
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
using System.Collections.Generic; | |
using System.Collections.Immutable; | |
using System.Linq; | |
using BenchmarkDotNet.Attributes; | |
using Libplanet.Crypto; | |
using Libplanet.Store; | |
using BTypes = Bencodex.Types; | |
namespace Libplanet.Benchmarks | |
{ | |
public class BenchDataModel | |
{ | |
private RootModel _rootModel; | |
private LeafModel _leafModel; | |
private PlainValueLeaf _plainValueLeaf; | |
private BTypes.Dictionary _encodedRootModel; | |
private BTypes.Dictionary _encodedLeafModel; | |
public BenchDataModel() | |
{ | |
_rootModel = new RootModel(); | |
_leafModel = new LeafModel(); | |
_encodedRootModel = _rootModel.Encode(); | |
_encodedLeafModel = _leafModel.Encode(); | |
_plainValueLeaf = new PlainValueLeaf(_encodedLeafModel); | |
} | |
[Benchmark] | |
public void EncodeRoot() | |
{ | |
_rootModel.Encode(); | |
} | |
[Benchmark] | |
public void EncodeLeaf() | |
{ | |
_leafModel.Encode(); | |
} | |
[Benchmark] | |
public void EncodePlainValueLeaf() | |
{ | |
_plainValueLeaf.Encode(); | |
} | |
[Benchmark] | |
public void DecodeRoot() | |
{ | |
_ = new RootModel(_encodedRootModel); | |
} | |
[Benchmark] | |
public void DecodeLeaf() | |
{ | |
_ = new LeafModel(_encodedLeafModel); | |
} | |
[Benchmark] | |
public void DecodePlainValueLeaf() | |
{ | |
_ = new PlainValueLeaf(_encodedLeafModel); | |
} | |
private class RootModel : DataModel | |
{ | |
public RootModel() | |
: base() | |
{ | |
System.Random random = new System.Random(); | |
MidModel = new MidModel(); | |
BigList = Enumerable | |
.Range(0, 1000) | |
.Select(_ => random.Next()) | |
.ToImmutableList(); | |
BigDict = Enumerable | |
.Range(0, 1000) | |
.Select(_ => new KeyValuePair<Address, string>( | |
new PrivateKey().ToAddress(), | |
new PrivateKey().ToAddress().ToString())) | |
.ToImmutableDictionary(); | |
} | |
public RootModel(Bencodex.Types.Dictionary encoded) | |
: base(encoded) | |
{ | |
} | |
public MidModel MidModel { get; private set; } | |
public ImmutableList<int> BigList { get; private set; } | |
public ImmutableDictionary<Address, string> BigDict { get; private set; } | |
} | |
private class MidModel : DataModel | |
{ | |
public MidModel() | |
: base() | |
{ | |
System.Random random = new System.Random(); | |
LeafModel = new LeafModel(); | |
BigList = Enumerable | |
.Range(0, 1000) | |
.Select(_ => random.Next()) | |
.ToImmutableList(); | |
BigDict = Enumerable | |
.Range(0, 1000) | |
.Select(_ => new KeyValuePair<Address, string>( | |
new PrivateKey().ToAddress(), | |
new PrivateKey().ToAddress().ToString())) | |
.ToImmutableDictionary(); | |
} | |
public MidModel(Bencodex.Types.Dictionary encoded) | |
: base(encoded) | |
{ | |
} | |
public LeafModel LeafModel { get; private set; } | |
public ImmutableList<int> BigList { get; private set; } | |
public ImmutableDictionary<Address, string> BigDict { get; private set; } | |
} | |
private class LeafModel : DataModel | |
{ | |
public LeafModel() | |
: base() | |
{ | |
System.Random random = new System.Random(); | |
BigList = Enumerable | |
.Range(0, 1000) | |
.Select(_ => random.Next()) | |
.ToImmutableList(); | |
BigDict = Enumerable | |
.Range(0, 1000) | |
.Select(_ => new KeyValuePair<Address, string>( | |
new PrivateKey().ToAddress(), | |
new PrivateKey().ToAddress().ToString())) | |
.ToImmutableDictionary(); | |
} | |
public LeafModel(Bencodex.Types.Dictionary encoded) | |
: base(encoded) | |
{ | |
} | |
public ImmutableList<int> BigList { get; private set; } | |
public ImmutableDictionary<Address, string> BigDict { get; private set; } | |
} | |
private class PlainValueLeaf | |
{ | |
public PlainValueLeaf() | |
{ | |
System.Random random = new System.Random(); | |
BigList = Enumerable | |
.Range(0, 1000) | |
.Select(_ => random.Next()) | |
.ToImmutableList(); | |
BigDict = Enumerable | |
.Range(0, 1000) | |
.Select(_ => new KeyValuePair<Address, string>( | |
new PrivateKey().ToAddress(), | |
new PrivateKey().ToAddress().ToString())) | |
.ToImmutableDictionary(); | |
} | |
public PlainValueLeaf(Bencodex.Types.Dictionary encoded) | |
{ | |
BigList = ((BTypes.List)encoded[nameof(BigList)]) | |
.Select(x => (int)((BTypes.Integer)x).Value) | |
.ToImmutableList(); | |
BigDict = ((BTypes.Dictionary)encoded[nameof(BigDict)]) | |
.Select(kv => new KeyValuePair<Address, string>( | |
new Address(((BTypes.Binary)kv.Key).ByteArray), | |
((BTypes.Text)kv.Value).Value)) | |
.ToImmutableDictionary(); | |
} | |
public BTypes.Dictionary Encode() | |
{ | |
return BTypes.Dictionary.Empty | |
.Add( | |
nameof(BigList), | |
(BTypes.IValue)new BTypes.List( | |
BigList.Select(x => (BTypes.IValue)new BTypes.Integer(x)).ToList())) | |
.Add( | |
nameof(BigDict), | |
new BTypes.Dictionary( | |
BigDict | |
.Select(kv => new KeyValuePair<BTypes.IKey, BTypes.IValue>( | |
new BTypes.Binary(kv.Key.ByteArray), | |
new BTypes.Text(kv.Value))))); | |
} | |
public ImmutableList<int> BigList { get; private set; } | |
public ImmutableDictionary<Address, string> BigDict { get; private set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment