-
-
Notifications
You must be signed in to change notification settings - Fork 31
Add Unit Tests for GC #206
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Label="Globals"> | ||
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath> | ||
</PropertyGroup> | ||
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" /> | ||
<ItemGroup> | ||
<ProjectCapability Include="TestContainer" /> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
<ProjectGuid>768be3b6-41c6-4dfb-8a2d-443b2113f5ad</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<FileAlignment>512</FileAlignment> | ||
<RootNamespace>NFUnitTestGC</RootNamespace> | ||
<AssemblyName>NFUnitTest</AssemblyName> | ||
<IsCodedUITest>False</IsCodedUITest> | ||
<IsTestProject>true</IsTestProject> | ||
<TestProjectType>UnitTest</TestProjectType> | ||
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" /> | ||
<ItemGroup> | ||
<Compile Include="TestGCWithByteArrays.cs" /> | ||
<Compile Include="TestGCWithTimeSpanArrays.cs" /> | ||
<Compile Include="TestGCWithDateTimeArrays.cs" /> | ||
<Compile Include="TestGCWithObjectArrays.cs" /> | ||
<Compile Include="TestGC.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="mscorlib"> | ||
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath> | ||
</Reference> | ||
<Reference Include="nanoFramework.TestFramework"> | ||
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.85\lib\nanoFramework.TestFramework.dll</HintPath> | ||
</Reference> | ||
<Reference Include="nanoFramework.UnitTestLauncher"> | ||
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.85\lib\nanoFramework.UnitTestLauncher.exe</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" /> | ||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyCopyright("Copyright (c) 2021 nanoFramework contributors")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Copyright (c) .NET Foundation and Contributors | ||
// Portions Copyright (c) Microsoft Corporation. All rights reserved. | ||
// See LICENSE file in the project root for full license information. | ||
// | ||
|
||
|
||
using nanoFramework.TestFramework; | ||
using System; | ||
|
||
namespace NFUnitTestGC | ||
{ | ||
[TestClass] | ||
public class TestGC | ||
{ | ||
[TestMethod] | ||
public void TestGCStress() | ||
{ | ||
int maxArraySize = 1024 * 32; | ||
object[] arrays = new object[600]; | ||
|
||
OutputHelper.WriteLine("Starting TestGCStress"); | ||
|
||
for (int loop = 0; loop < 100; loop++) | ||
{ | ||
OutputHelper.WriteLine($"Running iteration {loop}"); | ||
|
||
for (int i = 0; i < arrays.Length - 1;) | ||
{ | ||
OutputHelper.WriteLine($"Alloc array of {maxArraySize} bytes @ pos {i}"); | ||
arrays[i++] = new byte[maxArraySize]; ; | ||
|
||
OutputHelper.WriteLine($"Alloc array of 64 bytes @ pos {i}"); | ||
arrays[i++] = new byte[64]; | ||
} | ||
|
||
arrays[0] = new byte[maxArraySize]; | ||
|
||
for (int i = 0; i < arrays.Length; i++) | ||
{ | ||
arrays[i] = null; | ||
} | ||
} | ||
|
||
OutputHelper.WriteLine("Completed TestGCStress"); | ||
josesimoes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// Copyright (c) .NET Foundation and Contributors | ||
// Portions Copyright (c) Microsoft Corporation. All rights reserved. | ||
// See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using nanoFramework.TestFramework; | ||
using System; | ||
|
||
namespace NFUnitTestGC | ||
{ | ||
[TestClass] | ||
public class TestGCWithByteArrays | ||
{ | ||
[TestMethod] | ||
public void TestCompactionForNotFixedArray() | ||
josesimoes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
OutputHelper.WriteLine("Starting TestCompactionForNotFixedArray"); | ||
|
||
for (int loop = 0; loop < 10; loop++) | ||
{ | ||
OutputHelper.WriteLine($"Starting iteration {loop}"); | ||
|
||
// First we create byte and holes that keeps some space that could be used by compaction | ||
|
||
// Small count so compaction does not happen | ||
byte[] arrayOfArrays = new byte[10]; | ||
|
||
RunAllocations(arrayOfArrays); | ||
|
||
// This is the array that we expect to move in during compaction. | ||
byte[] testNativeBuffer = new byte[100]; | ||
|
||
// Fill it, so it is not optimized out | ||
Random random = new(); | ||
var baseValue = random.Next(2); | ||
|
||
for (int i = 0; i < testNativeBuffer.Length; i++) | ||
{ | ||
testNativeBuffer[i] = (byte)(i * baseValue); | ||
} | ||
|
||
// trigger compaction | ||
InitiateCompaction(); | ||
|
||
int index = 0; | ||
|
||
// Check that array content is not corrupted | ||
foreach (var item in testNativeBuffer) | ||
{ | ||
Assert.AreEqual(index * baseValue, item, $"Array content comparison failed at position {index}. Expecting {(index * baseValue)}, found {item}"); | ||
index++; | ||
} | ||
|
||
OutputHelper.WriteLine("No corruption detected in array"); | ||
} | ||
|
||
OutputHelper.WriteLine("Completed TestCompactionForNotFixedArray"); | ||
} | ||
|
||
void RunAllocations(byte[] arrObj) | ||
{ | ||
for (int i = 1; i < arrObj.Length; i++) | ||
{ | ||
// Creates referenced byte, which stays in memory until InitiateCompaction exits | ||
arrObj[i] = new byte(); | ||
|
||
// Tries to create larger object that would be later hole . | ||
// This object could be garbage collected on each "i" cycle. | ||
byte[] arr = new byte[50 * i]; | ||
|
||
// Creates some usage for arr, so it is not optimized out. | ||
arr[0] = 1; | ||
arr[1] = 2; | ||
|
||
OutputHelper.WriteLine($"On Cycle {i:D3} Array of {arr[1]} was allocated"); | ||
} | ||
} | ||
|
||
// This method causes compaction to occur. | ||
// It is not so trivial as it need to fragment heap with referenced byte array. | ||
void InitiateCompaction() | ||
{ | ||
// large count, so compaction happens during call to RunAllocations | ||
byte[] arrayOfArrays = new byte[1500]; | ||
RunAllocations(arrayOfArrays); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.