diff options
author | Sung Yoon Whang <suwhang@microsoft.com> | 2018-02-27 03:56:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-27 03:56:54 -0800 |
commit | f761b8ee2adaf0bee0badcde5a3f22df518a31e9 (patch) | |
tree | e31b30780571e0354e348ddb56902cae9a509a8c /tests | |
parent | 2825e34ed4b63482d1a2a1b1aeb6c341a4d05a44 (diff) | |
download | coreclr-f761b8ee2adaf0bee0badcde5a3f22df518a31e9.tar.gz coreclr-f761b8ee2adaf0bee0badcde5a3f22df518a31e9.tar.bz2 coreclr-f761b8ee2adaf0bee0badcde5a3f22df518a31e9.zip |
Add test for GC.GetAllocatedBytesForCurrentThread (#16428)
* Add test for GC.GetAllocatedBytesForCurrentThread
* modified test to fit coreclr test runner
* add testing with/without gc.collect()
* Address object size difference on x86 machines
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.cs | 127 | ||||
-rw-r--r-- | tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.csproj | 36 |
2 files changed, 163 insertions, 0 deletions
diff --git a/tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.cs b/tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.cs new file mode 100755 index 0000000000..89a1904685 --- /dev/null +++ b/tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.cs @@ -0,0 +1,127 @@ +// 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. +// Tests GC.Collect() + +using System; +using System.Diagnostics; +using System.Collections.Generic; +using System.Reflection; + +public class Test +{ + static Random Rand = new Random(); + + public static bool GetAllocatedBytesForCurrentThread(int size) + { + int startCount = GC.CollectionCount(0); + long start = GC.GetAllocatedBytesForCurrentThread(); + + GC.KeepAlive(new String('a', size)); + + long end = GC.GetAllocatedBytesForCurrentThread(); + int endCount = GC.CollectionCount(0); + + if (start == end) + { + Console.WriteLine("GetAllocatedBytesForCurrentThread: start and end same!"); + return false; + } + return true; + } + + static int Alloc(List<object> list, int size) + { + int toAlloc = Rand.Next(size / 2 , (int)((float)size * 1.5)); + Console.WriteLine("allocating {0} bytes", toAlloc); + int allocated = 0; + + while (allocated < toAlloc) + { + int s = Rand.Next(100, 1000); + allocated += s + 24; + byte[] b = new byte[s]; + list.Add((object)b); + } + return allocated; + } + + static bool TestWithAlloc() + { + int allocatedBytes = 0; + for (int i = 0; i < 100; i++) + { + List<object> list = new List<object>(); + allocatedBytes = Alloc(list, 80*1024*1024); + + if (!GetAllocatedBytesForCurrentThread (100000)) + { + return false; + } + Console.WriteLine("iter {0} allocated {1} bytes", i, allocatedBytes); + } + return true; + } + + // In core 1.0 we didn't have the API exposed so needed to use reflection to get it. + // This should be split into 2 tests, with and without GC.Collect. + static bool TestCore1(bool testWithCollection) + { + const string name = "GetAllocatedBytesForCurrentThread"; + var typeInfo = typeof(GC).GetTypeInfo(); + var method = typeInfo.GetMethod(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + + long nBytesBefore = 0; + long nBytesAfter = 0; + + int countBefore = GC.CollectionCount(0); + int bytesDiff = 0; + + + for (int i = 0; i < 10000; ++i) + { + nBytesBefore = (long)method.Invoke(null, null); + // Test with collection. + if (testWithCollection) + { + GC.Collect(); + } + + nBytesAfter = (long)method.Invoke(null, null); + + if (((nBytesBefore + 16) != nBytesAfter) && ((nBytesBefore + 24) != nBytesAfter)) + { + int countAfter = GC.CollectionCount(0); + Console.WriteLine("b: {0}, a: {1}, iter {2}, {3}->{4}", nBytesBefore, nBytesAfter, i, countBefore, countAfter); + return false; + } + } + return true; + } + + public static int Main() + { + // First test with collection + if (!TestCore1(true)) + { + Console.WriteLine("Test for GetAllocatedBytesForCurrentThread() failed!"); + return 1; + } + + // Test without collection + if (!TestCore1(false)) + { + Console.WriteLine("Test for GetAllocatedBytesForCurrentThread() failed!"); + return 1; + } + if (!TestWithAlloc()) + { + Console.WriteLine("Test for GetAllocatedBytesForCurrentThread() failed!"); + return 1; + } + + + Console.WriteLine("Test for GetAllocatedBytesForCurrentThread() passed!"); + return 100; + } +} diff --git a/tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.csproj b/tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.csproj new file mode 100644 index 0000000000..745e3695d8 --- /dev/null +++ b/tests/src/GC/API/GC/GetAllocatedBytesForCurrentThread.csproj @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <AssemblyName>$(MSBuildProjectName)</AssemblyName> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid> + <OutputType>Exe</OutputType> + <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> + </PropertyGroup> + <!-- Default configurations to help VS understand the configurations --> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup> + <ItemGroup> + <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> + <Visible>False</Visible> + </CodeAnalysisDependentAssemblyPaths> + </ItemGroup> + <PropertyGroup> + <!-- Set to 'Full' if the Debug? column is marked in the spreadsheet. Leave blank otherwise. --> + <DebugType>PdbOnly</DebugType> + <NoLogo>True</NoLogo> + <DefineConstants>$(DefineConstants);DESKTOP</DefineConstants> + </PropertyGroup> + <ItemGroup> + <Compile Include="GetAllocatedBytesForCurrentThread.cs" /> + </ItemGroup> + <ItemGroup> + <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> + </ItemGroup> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> + <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup> +</Project> |