Skip to content

Commit

Permalink
Merge pull request #21 from thecogworks/feat/benchmar-tests
Browse files Browse the repository at this point in the history
feat: memory cache benchmarks
  • Loading branch information
aochmann authored Jun 11, 2021
2 parents cd83aad + 93a7f81 commit 16243c9
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 17 deletions.
122 changes: 122 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Benchmark
on:
pull_request:

env:
BENCHMARK_PROJECT: 'Source\tests\BenchmarkTests\Cogworks.Essentials.BenchmarkTests\Cogworks.Essentials.BenchmarkTests.csproj'
BENCHMARK_REPORT: 'Cogworks.Essentials.BenchmarkTests.Benchmarks.MemoryCacheBenchmark-report-github.md'
BENCHMARK_REPORT_OUTPUT: '${{github.workspace}}\BenchmarkDotNet.Artifacts\results'

ACTIONS_ALLOW_UNSECURE_COMMANDS: true

# Dotnet Setup
DOTNET_VERSION: 3.1.401

# Stop wasting time caching packages
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true

# Disable sending usage data to Microsoft
DOTNET_CLI_TELEMETRY_OPTOUT: true

# Solution Setup
CONFIG: 'Release'

# Nuget Setup
NUGET_VERSION: 'latest'

jobs:
build-and-benchmark:
runs-on: windows-latest
steps:
- name: Checkout commit
uses: actions/checkout@v2

- name: Setup MSBuild
uses: microsoft/setup-msbuild@v1

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Configure NuGet
uses: nuget/setup-nuget@v1
with:
nuget-version: ${{ env.NUGET_VERSION }}

- name: NuGet Restore
shell: powershell
working-directory: ${{ github.workspace }}
run: |
$solutions = Get-ChildItem -Path . -Recurse -Include *.sln
foreach ($solutionFile in $solutions){
nuget restore "$solutionFile"
}
- name: Install Dependencies
shell: powershell
working-directory: ${{ github.workspace }}
run: |
$solutions = Get-ChildItem -Path . -Recurse -Include *.sln
foreach ($solutionFile in $solutions){
dotnet restore "$solutionFile"
}
- name: Build
shell: powershell
working-directory: ${{ github.workspace }}
run: |
$solutions = Get-ChildItem -Path . -Recurse -Include *.sln
foreach ($solutionFile in $solutions){
msbuild.exe "$solutionFile" `
/p:Configuration=${{ env.CONFIG }} `
/p:DeployOnBuild=false `
/p:SkipInvalidConfigurations=true `
/p:TransformWebConfigEnabled=False `
/p:AutoParameterizationWebConfigConnectionStrings=False `
/p:MarkWebConfigAssistFilesAsExclude=False
}
- name: Run Benchmark
shell: powershell
run: |
dotnet run `
-p "${{ env.BENCHMARK_PROJECT }}" `
-c ${{ env.CONFIG }} `
-f netcoreapp3.1 `
--no-build `
--no-restore `
--filter * `
--join `
--warmupCount 1 `
--minIterationCount 9 `
--maxIterationCount 10 `
--iterationCount 5 `
--strategy ColdStart
- name: Upload benchmark results
uses: actions/upload-artifact@v2
with:
name: benchmark-results
path: ${{ env.BENCHMARK_REPORT_OUTPUT }}\${{ env.BENCHMARK_REPORT }}

commenting:
needs: build-and-benchmark
runs-on: ubuntu-latest
steps:
- name: Checkout commit
uses: actions/checkout@v2

- uses: actions/download-artifact@v1
with:
name: benchmark-results

- name: Comment PR - benchmark report
uses: machine-learning-apps/pr-comment@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
path: benchmark-results/${{ env.BENCHMARK_REPORT }}
5 changes: 3 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: Validate PR
name: PR build and tests validation
on:
pull_request:

env:
ReportOutput: '${{github.workspace}}'
TestReportFileName: 'TestReport.${{github.run_number}}.md'

ACTIONS_ALLOW_UNSECURE_COMMANDS: true

# Dotnet Setup
Expand Down Expand Up @@ -112,7 +113,7 @@ jobs:
with:
name: results

- name: Comment PR
- name: Comment PR - test report
uses: machine-learning-apps/pr-comment@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,5 @@ Source/**/*GeneratedMSBuildEditorConfig.editorconfig

dist/
output/

BenchmarkDotNet.Artifacts/*
20 changes: 19 additions & 1 deletion Source/Cogworks.Essentials.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E93B797F-ECB
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{DD2AD54C-38A9-4E28-A98E-1FB35DFF91F3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cogworks.Essentials.UnitTests", "tests\UnitTests\Cogworks.Essentials.UnitTests\Cogworks.Essentials.UnitTests.csproj", "{4BF3A6C1-4902-403F-97D8-F30FA0DBB689}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cogworks.Essentials.UnitTests", "tests\UnitTests\Cogworks.Essentials.UnitTests\Cogworks.Essentials.UnitTests.csproj", "{4BF3A6C1-4902-403F-97D8-F30FA0DBB689}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnitTests", "UnitTests", "{14D1276A-A96B-44B1-9E42-D508D7993455}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks", "{EC42AD9E-F772-4F32-ABB0-5DF4955B1D2F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cogworks.Essentials.BenchmarkTests", "tests\BenchmarkTests\Cogworks.Essentials.BenchmarkTests\Cogworks.Essentials.BenchmarkTests.csproj", "{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +61,18 @@ Global
{4BF3A6C1-4902-403F-97D8-F30FA0DBB689}.Release|x64.Build.0 = Release|Any CPU
{4BF3A6C1-4902-403F-97D8-F30FA0DBB689}.Release|x86.ActiveCfg = Release|Any CPU
{4BF3A6C1-4902-403F-97D8-F30FA0DBB689}.Release|x86.Build.0 = Release|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Debug|x64.ActiveCfg = Debug|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Debug|x64.Build.0 = Debug|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Debug|x86.ActiveCfg = Debug|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Debug|x86.Build.0 = Debug|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Release|Any CPU.Build.0 = Release|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Release|x64.ActiveCfg = Release|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Release|x64.Build.0 = Release|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Release|x86.ActiveCfg = Release|Any CPU
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -65,6 +81,8 @@ Global
{6D168150-8DE2-40D5-9D44-7806CC586A5D} = {E93B797F-ECBA-47A5-9CD6-767CCF74AF9E}
{4BF3A6C1-4902-403F-97D8-F30FA0DBB689} = {14D1276A-A96B-44B1-9E42-D508D7993455}
{14D1276A-A96B-44B1-9E42-D508D7993455} = {DD2AD54C-38A9-4E28-A98E-1FB35DFF91F3}
{EC42AD9E-F772-4F32-ABB0-5DF4955B1D2F} = {DD2AD54C-38A9-4E28-A98E-1FB35DFF91F3}
{81E7F6A4-B8C1-494A-9E44-1D6E24E218FF} = {EC42AD9E-F772-4F32-ABB0-5DF4955B1D2F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F17AB110-9EB9-4470-81D5-56D4C07306B2}
Expand Down
23 changes: 9 additions & 14 deletions Source/Cogworks.Essentials/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,12 @@ public async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> getValueFunction
{
var hasEntry = _memoryCache.TryGetValue(key, out T cacheEntry);

if (hasEntry)
if (!hasEntry)
{
return cacheEntry;
}

var myLock = Locks.GetOrAdd(key, k => new SemaphoreSlim(1, 1));
await myLock.WaitAsync();
var myLock = Locks.GetOrAdd(key, k => new SemaphoreSlim(1, 1));
await myLock.WaitAsync();

try
{
if (!hasEntry)
try
{
cacheEntry = await getValueFunction();

Expand All @@ -109,14 +104,14 @@ public async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> getValueFunction
.RegisterPostEvictionCallback(CacheCallback);

_memoryCache.Set(key, cacheEntry, entryOptions);
AddCacheKeyToCacheKeysDefinitions(key);
}
finally
{
myLock.Release();
}
}
finally
{
myLock.Release();
}

AddCacheKeyToCacheKeysDefinitions(key);
return cacheEntry;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[*.cs]

# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = none

# IDE0059: Unnecessary assignment of a value
csharp_style_unused_value_assignment_preference = unused_local_variable

# SA1402: File may only contain a single type
dotnet_diagnostic.SA1402.severity = none

# CS8019: Unnecessary using directive
dotnet_diagnostic.CS8019.severity = none
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BenchmarkDotNet.Artifacts/*
Loading

0 comments on commit 16243c9

Please sign in to comment.