Created
January 30, 2020 07:18
-
-
Save MihaZupan/558f70ad5e64e7d85a3466e8bc0dca2c to your computer and use it in GitHub Desktop.
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 Xunit; | |
namespace System.PrivateUri.Functional.Tests | |
{ | |
public static class KnownSchemeTests | |
{ | |
[Theory] | |
[InlineData("https")] | |
[InlineData("ws")] | |
[InlineData("wss")] | |
[InlineData("ftp")] | |
[InlineData("file")] | |
[InlineData("gopher")] | |
[InlineData("nntp")] | |
[InlineData("news")] | |
[InlineData("mailto")] | |
[InlineData("uuid")] | |
[InlineData("telnet")] | |
[InlineData("ldap")] | |
[InlineData("net.tcp")] | |
[InlineData("net.pipe")] | |
[InlineData("vsmacros")] | |
public static void KnownSchemes_DoNotAllocateForSchemeLookup(string scheme) | |
{ | |
string uriString = scheme + "://foo.bar"; | |
// Cache this to save on test execution time | |
double allocatedForHttp = s_allocatedForHttp ??= MeasureAllocations(() => new Uri("http://foo.bar")); | |
double allocatedForTestScheme = MeasureAllocations(() => new Uri(uriString)); | |
Assert.InRange(allocatedForTestScheme, allocatedForHttp - 4, allocatedForHttp + 4); | |
} | |
private static double? s_allocatedForHttp = null; | |
private static double MeasureAllocations(Action action) | |
{ | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
const long iterations = 10_000; | |
long before = GC.GetAllocatedBytesForCurrentThread(); | |
for (long i = 0; i < iterations; i++) | |
action(); | |
long after = GC.GetAllocatedBytesForCurrentThread(); | |
return (after - before) / (double)iterations; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment