Last active
July 12, 2022 21:19
-
-
Save PaulusParssinen/1392d166eb5e5d11720b18925ac4dc0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Numerics; | |
using System.Formats.Asn1; | |
using BenchmarkDotNet.Running; | |
using BenchmarkDotNet.Attributes; | |
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly) | |
.Run(args); | |
[MemoryDiagnoser] | |
[AsciiDocExporter] | |
[MarkdownExporterAttribute.GitHub] | |
public class AsnWriterBenchmark | |
{ | |
private BigInteger _smallBigInteger; | |
private BigInteger _bigBigInteger; | |
private static string OidWorstCase = "1.1.1.1.1"; | |
private static string OidBig = "2.25.329800735698586629295641978511506172918.3"; | |
private static string OidMostArcs = "1.3.6.1.4.1.311.60.2.1.1"; | |
[GlobalSetup] | |
public void GlobalSetup() | |
{ | |
_smallBigInteger = BigInteger.Parse("-1339673755198158349044581307228491520"); | |
_bigBigInteger = BigInteger.Parse("207545550571844404676608632512851454930111394466749205318948660756381" + | |
"523214360115124048083611193260260272384440199925180817531535965931647" + | |
"037093368608713442955529617501657176146245891571745113402870077189045" + | |
"116705181899983704226178882882602868159586789723579670915035003754974" + | |
"985730226756711782751711104985926458681071638525996766798322809764200" + | |
"941677343791419428587801897366593842552727222686457866144928124161967" + | |
"521735393182823375650694786333059783380738262856873316471830589717911" + | |
"537307419734834201104082715701367336140572971505716740825623220507359" + | |
"42929758463490933054115079473593821332264673455059897928082590541"); | |
} | |
[Benchmark] | |
public void WriteInteger_Small() | |
{ | |
var writer = new AsnWriter(AsnEncodingRules.DER); | |
writer.WriteInteger(_smallBigInteger); | |
} | |
[Benchmark] | |
public void WriteInteger_Big() | |
{ | |
var writer = new AsnWriter(AsnEncodingRules.DER); | |
writer.WriteInteger(_bigBigInteger); | |
} | |
[Benchmark] | |
public void WriteOid_WorstCase() | |
{ | |
var writer = new AsnWriter(AsnEncodingRules.DER); | |
writer.WriteObjectIdentifier(OidWorstCase); | |
} | |
[Benchmark] | |
public void WriteOid_Big() | |
{ | |
var writer = new AsnWriter(AsnEncodingRules.DER); | |
writer.WriteObjectIdentifier(OidBig); | |
} | |
[Benchmark] | |
public void WriteOid_OidMostArcs() | |
{ | |
var writer = new AsnWriter(AsnEncodingRules.DER); | |
writer.WriteObjectIdentifier(OidMostArcs); | |
} | |
} | |
[MemoryDiagnoser] | |
public class AsnReaderBenchmark | |
{ | |
private static byte[] _bigInteger = Convert.FromHexString( | |
"0282010100A46861FA9D5DB763633BF5A64EF6E7C2C2367F48D2D46643A22DFC" + | |
"FCCB24E58A14D0F06BDC956437F2A56BA4BEF70BA361BF12964A0D665AFD84B0" + | |
"F7494C8FA4ABC5FCA2E017C06178AEF2CDAD1B5F18E997A14B965C074E8F5649" + | |
"70607276B00583932240FE6E2DD013026F9AE13D7C91CC07C4E1E8E87737DC06" + | |
"EF2B575B89D62EFE46859F8255A123692A706C68122D4DAFE11CB205A7B3DE06" + | |
"E553F7B95F978EF8601A8DF819BF32040BDF92A0DE0DF269B4514282E17AC699" + | |
"34E8440A48AB9D1F5DF89A502CEF6DFDBE790045BD45E0C94E5CA8ADD76A013E" + | |
"9C978440FC8A9E2A9A4940B2460819C3E302AA9C9F355AD754C86D3ED77DDAA3" + | |
"DA13810B4D"); | |
[Benchmark] | |
public BigInteger ReadInteger_BigInteger() | |
{ | |
var reader = new AsnReader(_bigInteger, AsnEncodingRules.DER); | |
return reader.ReadInteger(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment