Skip to content

Add Convert.FromHex benchmark #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions scripts/dotnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,29 +778,38 @@ def install(
# Download appropriate dotnet install script
dotnetInstallScriptExtension = '.ps1' if platform == 'win32' else '.sh'
dotnetInstallScriptName = 'dotnet-install' + dotnetInstallScriptExtension
url = 'https://dot.net/v1/'
url = 'https://dot.net/v1/'
dotnetInstallScriptUrl = url + dotnetInstallScriptName

dotnetInstallScriptPath = path.join(install_dir, dotnetInstallScriptName)

getLogger().info('Downloading %s', dotnetInstallScriptUrl)
count = 0
while count < 3:
max_count = 10
while count < max_count:
try:
with urlopen(dotnetInstallScriptUrl, context=ssl._create_unverified_context()) as response:
if "html" in response.info()['Content-Type']:
count = count + 1
sleep(1) # sleep one second
sleep(count ** 2)
continue
with open(dotnetInstallScriptPath, 'wb') as outfile:
outfile.write(response.read())
break
except Exception:
except URLError as error:
getLogger().warning(f"Could not download dotnet-install script from {dotnetInstallScriptUrl}; {Reason: {error.reason}}; Attempt {count}")
count = count + 1
sleep(count ** 2)
continue
except Exception as error:
getLogger().warning(f"Could not download dotnet-install script from {dotnetInstallScriptUrl}; {type(error).__name__}; Attempt {count}")
count = count + 1
sleep(1)
sleep(count ** 2)
continue

if count == 3:
getLogger().info(f"Downloaded {dotnetInstallScriptUrl} OK")

if count == max_count:
getLogger().error("Fatal error: could not download dotnet-install script")
raise Exception("Fatal error: could not download dotnet-install script")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand All @@ -18,6 +18,9 @@ public class Perf_Convert
private byte[] _binaryData;
private char[] _base64CharArray;
private string _base64String;
#if NET5_0_OR_GREATER
private string _hexString;
#endif
private char[] _base64Chars;

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

[GlobalSetup(Target = nameof(FromHexString))]
public void SetupFromHexString()
{
_hexString = Convert.ToHexString(Encoding.ASCII.GetBytes("This is a test of Convert."));
}

[Benchmark]
public string ToHexString() => Convert.ToHexString(_binaryData);
[Benchmark]
public byte[] FromHexString() => Convert.FromHexString(_hexString);
#endif

private static byte[] InitializeBinaryDataCollection(int size)
Expand Down