Skip to content

Commit 0307668

Browse files
Add Convert.FromHex benchmark (#3032)
* Added Convert.FromHex benchmark * Update src/benchmarks/micro/libraries/System.Runtime.Extensions/Perf.Convert.cs * Robustify downloading dotnet --------- Co-authored-by: Dan Moseley <[email protected]>
1 parent 613b0e5 commit 0307668

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

scripts/dotnet.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,29 +778,38 @@ def install(
778778
# Download appropriate dotnet install script
779779
dotnetInstallScriptExtension = '.ps1' if platform == 'win32' else '.sh'
780780
dotnetInstallScriptName = 'dotnet-install' + dotnetInstallScriptExtension
781-
url = 'https://dot.net/v1/'
781+
url = 'https://dot.net/v1/'
782782
dotnetInstallScriptUrl = url + dotnetInstallScriptName
783783

784784
dotnetInstallScriptPath = path.join(install_dir, dotnetInstallScriptName)
785785

786786
getLogger().info('Downloading %s', dotnetInstallScriptUrl)
787787
count = 0
788-
while count < 3:
788+
max_count = 10
789+
while count < max_count:
789790
try:
790791
with urlopen(dotnetInstallScriptUrl, context=ssl._create_unverified_context()) as response:
791792
if "html" in response.info()['Content-Type']:
792793
count = count + 1
793-
sleep(1) # sleep one second
794+
sleep(count ** 2)
794795
continue
795796
with open(dotnetInstallScriptPath, 'wb') as outfile:
796797
outfile.write(response.read())
797798
break
798-
except Exception:
799+
except URLError as error:
800+
getLogger().warning(f"Could not download dotnet-install script from {dotnetInstallScriptUrl}; {Reason: {error.reason}}; Attempt {count}")
801+
count = count + 1
802+
sleep(count ** 2)
803+
continue
804+
except Exception as error:
805+
getLogger().warning(f"Could not download dotnet-install script from {dotnetInstallScriptUrl}; {type(error).__name__}; Attempt {count}")
799806
count = count + 1
800-
sleep(1)
807+
sleep(count ** 2)
801808
continue
802809

803-
if count == 3:
810+
getLogger().info(f"Downloaded {dotnetInstallScriptUrl} OK")
811+
812+
if count == max_count:
804813
getLogger().error("Fatal error: could not download dotnet-install script")
805814
raise Exception("Fatal error: could not download dotnet-install script")
806815

src/benchmarks/micro/libraries/System.Runtime.Extensions/Perf.Convert.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -18,6 +18,9 @@ public class Perf_Convert
1818
private byte[] _binaryData;
1919
private char[] _base64CharArray;
2020
private string _base64String;
21+
#if NET5_0_OR_GREATER
22+
private string _hexString;
23+
#endif
2124
private char[] _base64Chars;
2225

2326
[GlobalSetup(Target = nameof(GetTypeCode))]
@@ -85,8 +88,16 @@ public void SetupToHexString()
8588
new Random(42).NextBytes(_binaryData);
8689
}
8790

91+
[GlobalSetup(Target = nameof(FromHexString))]
92+
public void SetupFromHexString()
93+
{
94+
_hexString = Convert.ToHexString(Encoding.ASCII.GetBytes("This is a test of Convert."));
95+
}
96+
8897
[Benchmark]
8998
public string ToHexString() => Convert.ToHexString(_binaryData);
99+
[Benchmark]
100+
public byte[] FromHexString() => Convert.FromHexString(_hexString);
90101
#endif
91102

92103
private static byte[] InitializeBinaryDataCollection(int size)

0 commit comments

Comments
 (0)