Skip to content

Commit

Permalink
Merge pull request #206 from hans-olav/buffer_handling
Browse files Browse the repository at this point in the history
Improving Buffer Handling to Reduce GC Pressure
  • Loading branch information
Liryna authored Oct 1, 2018
2 parents 27eca48 + b243cb2 commit fa556df
Show file tree
Hide file tree
Showing 15 changed files with 636 additions and 137 deletions.
51 changes: 51 additions & 0 deletions DokanNet.Tests/BufferPoolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using DokanNet.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DokanNet.Tests
{
/// <summary>
/// Tests for <see cref="BufferPool"/>.
/// </summary>
[TestClass]
public sealed class BufferPoolTests
{
/// <summary>
/// Rudimentary test for <see cref="BufferPool"/>.
/// </summary>
[TestMethod, TestCategory(TestCategories.Success)]
public void BufferPoolBasicTest()
{
BufferPool pool = new BufferPool();
ILogger logger = new ConsoleLogger();

// Verify buffer is pooled.
const int MB = 1024 * 1024;
byte[] buffer = pool.RentBuffer(MB, logger);
pool.ReturnBuffer(buffer, logger);

byte[] buffer2 = pool.RentBuffer(MB, logger);
Assert.AreSame(buffer, buffer2, "Expected recycling of 1 MB buffer.");

// Verify buffer that buffer not power of 2 is not pooled.
buffer = pool.RentBuffer(MB - 1, logger);
pool.ReturnBuffer(buffer, logger);

buffer2 = pool.RentBuffer(MB - 1, logger);
Assert.AreNotSame(buffer, buffer2, "Did not expect recycling of 1 MB - 1 byte buffer.");

// Run through a bunch of random buffer sizes and make sure we always get a buffer of the right size.
int seed = Environment.TickCount;
Console.WriteLine($"Random seed: {seed}");
Random random = new Random(seed);

for (int i = 0; i < 1000; i++)
{
int size = random.Next(0, 2 * MB);
buffer = pool.RentBuffer((uint)size, logger);
Assert.AreEqual(size, buffer.Length, "Wrong buffer size.");
pool.ReturnBuffer(buffer, logger);
}
}
}
}
108 changes: 30 additions & 78 deletions DokanNet.Tests/DirectoryInfoTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -338,13 +339,12 @@ public void Delete_WhereRecurseIsFalse_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void Delete_WhereRecurseIsTrueAndDirectoryIsNonempty_CallsApiCorrectly()
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);
public static IEnumerable<object[]> ConfigFindFilesData
=> new object[][] { new object[] { true }, new object[] { false } };

[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void Delete_WhereRecurseIsTrueAndDirectoryIsNonempty_CallsApiCorrectly(bool supportsPatternSearch)
{
var fixture = DokanOperationsFixture.Instance;

string path = fixture.DirectoryName.AsRootedPath(),
Expand Down Expand Up @@ -397,13 +397,9 @@ public void Delete_WhereRecurseIsTrueAndDirectoryIsNonempty_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void Delete_WhereRecurseIsTrueAndDirectoryIsEmpty_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void Delete_WhereRecurseIsTrueAndDirectoryIsEmpty_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = fixture.DirectoryName.AsRootedPath();
Expand Down Expand Up @@ -515,13 +511,9 @@ public void GetDirectories_OnRootDirectory_WithoutPatternSearch_CallsApiCorrectl
}

[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SubDirectory")]
[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetDirectories_OnSubDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetDirectories_OnSubDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = fixture.DirectoryName.AsRootedPath();
Expand Down Expand Up @@ -557,13 +549,9 @@ public void GetDirectories_OnSubDirectory_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetDirectoriesWithFilter_OnRootDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetDirectoriesWithFilter_OnRootDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = DokanOperationsFixture.RootName;
Expand Down Expand Up @@ -602,13 +590,9 @@ public void GetDirectoriesWithFilter_OnRootDirectory_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFiles_OnRootDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFiles_OnRootDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = DokanOperationsFixture.RootName;
Expand Down Expand Up @@ -645,13 +629,9 @@ public void GetFiles_OnRootDirectory_CallsApiCorrectly()
}

[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SubDirectory")]
[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFiles_OnSubDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFiles_OnSubDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = fixture.DirectoryName.AsRootedPath();
Expand Down Expand Up @@ -687,13 +667,9 @@ public void GetFiles_OnSubDirectory_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFilesWithFilter_OnRootDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFilesWithFilter_OnRootDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = DokanOperationsFixture.RootName;
Expand Down Expand Up @@ -732,13 +708,9 @@ public void GetFilesWithFilter_OnRootDirectory_CallsApiCorrectly()
}

[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SubDirectory")]
[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFiles_UnknownDates_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFiles_UnknownDates_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = fixture.DirectoryName.AsRootedPath();
Expand Down Expand Up @@ -795,13 +767,9 @@ public void GetFiles_UnknownDates_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFileSystemInfos_OnRootDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFileSystemInfos_OnRootDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = DokanOperationsFixture.RootName;
Expand Down Expand Up @@ -836,13 +804,9 @@ public void GetFileSystemInfos_OnRootDirectory_CallsApiCorrectly()
}

[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SubDirectory")]
[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFileSystemInfos_OnSubDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFileSystemInfos_OnSubDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = fixture.DirectoryName.AsRootedPath();
Expand Down Expand Up @@ -876,13 +840,9 @@ public void GetFileSystemInfos_OnSubDirectory_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFileSystemInfosWithFilter_OnRootDirectory_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFileSystemInfosWithFilter_OnRootDirectory_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = DokanOperationsFixture.RootName;
Expand Down Expand Up @@ -918,13 +878,9 @@ public void GetFileSystemInfosWithFilter_OnRootDirectory_CallsApiCorrectly()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void GetFileSystemInfos_OnRootDirectory_WhereSearchOptionIsAllDirectories_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void GetFileSystemInfos_OnRootDirectory_WhereSearchOptionIsAllDirectories_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var pathsAndItems = new[]
Expand Down Expand Up @@ -1075,13 +1031,9 @@ public void MoveTo_WhereTargetExists_Throws()
#endif
}

[TestMethod, TestCategory(TestCategories.Success)]
[DeploymentItem("DirectoryInfoTests.Configuration.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\DirectoryInfoTests.Configuration.xml", "ConfigFindFiles", DataAccessMethod.Sequential)]
public void SetAccessControl_CallsApiCorrectly()
[DataTestMethod, TestCategory(TestCategories.Success), DynamicData(nameof(ConfigFindFilesData))]
public void SetAccessControl_CallsApiCorrectly(bool supportsPatternSearch)
{
var supportsPatternSearch = bool.Parse((string) TestContext.DataRow["SupportsPatternSearch"]);

var fixture = DokanOperationsFixture.Instance;

var path = fixture.DirectoryName;
Expand Down
9 changes: 0 additions & 9 deletions DokanNet.Tests/DirectoryInfoTests.Configuration.xml

This file was deleted.

13 changes: 6 additions & 7 deletions DokanNet.Tests/DokanNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@
<!--Add the Target Framework to the output file names. -->
<AssemblyName>$(MSBuildProjectName).$(TargetFramework)</AssemblyName>
<CLSCompliant>True</CLSCompliant>
<!-- We need to sign the test assembly to use it in InternalsVisibleTo for DokanNet.dll. -->
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\DokanNet\Dokan.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<Content Include="DirectoryInfoTests.Configuration.xml">
<DependentUpon>DirectoryInfoTest.cs</DependentUpon>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="OverlappedTests.Configuration.xml">
<DependentUpon>OverlappedTests.cs</DependentUpon>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Moq" Version="4.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.12" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.11" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit fa556df

Please sign in to comment.