-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port System.Threading.Performance.Tests and remove duplicated benchma…
…rks (#80), fixes #74 * copy * port * add category * remove duplicated benchmarks from ThreadingPerf.cs, move the remaining ones to Perf.Interlocked, add CoreCLR category to make sure they are run for both repos
- Loading branch information
1 parent
b56fc31
commit a2aca5c
Showing
7 changed files
with
344 additions
and
72 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/benchmarks/corefx/System.Threading/Perf.EventWaitHandle.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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. | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using Benchmarks; | ||
|
||
namespace System.Threading.Tests | ||
{ | ||
[BenchmarkCategory(Categories.CoreFX)] | ||
public class Perf_EventWaitHandle | ||
{ | ||
private const int IterationCount = 100_000; | ||
|
||
private EventWaitHandle _are; | ||
|
||
[GlobalSetup] | ||
public void Setup() => _are = new EventWaitHandle(false, EventResetMode.AutoReset); | ||
|
||
[GlobalCleanup] | ||
public void Dispose() => _are.Dispose(); | ||
|
||
[Benchmark] | ||
public void Set_Reset() | ||
{ | ||
EventWaitHandle are = _are; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
are.Set(); | ||
are.Reset(); | ||
} | ||
} | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
src/benchmarks/corefx/System.Threading/Perf.Interlocked.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// 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. | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using Benchmarks; | ||
|
||
namespace System.Threading.Tests | ||
{ | ||
[BenchmarkCategory(Categories.CoreFX, Categories.CoreCLR)] | ||
public class Perf_Interlocked | ||
{ | ||
private const int IterationCount = 10_000_000; | ||
|
||
[Benchmark] | ||
public void Increment_int() | ||
{ | ||
int location = 0; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Increment(ref location); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Decrement_int() | ||
{ | ||
int location = 0; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Decrement(ref location); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Increment_long() | ||
{ | ||
long location = 0; | ||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Increment(ref location); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Decrement_long() | ||
{ | ||
long location = 0; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Decrement(ref location); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Add_int() | ||
{ | ||
int location = 0; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Add(ref location, 2); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Add_long() | ||
{ | ||
long location = 0; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Add(ref location, 2); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Exchange_int() | ||
{ | ||
int location = 0; | ||
int newValue = 1; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Exchange(ref location, newValue); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void Exchange_long() | ||
{ | ||
long location = 0; | ||
long newValue = 1; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.Exchange(ref location, newValue); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void CompareExchange_int() | ||
{ | ||
int location = 0; | ||
int newValue = 1; | ||
int comparand = 0; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.CompareExchange(ref location, newValue, comparand); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void CompareExchange_long() | ||
{ | ||
long location = 0; | ||
long newValue = 1; | ||
long comparand = 0; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.CompareExchange(ref location, newValue, comparand); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void CompareExchange_object_Match() | ||
{ | ||
string location = "What?"; | ||
string newValue = "World"; | ||
string comparand = "What?"; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.CompareExchange(ref location, newValue, comparand); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void CompareExchange_object_NoMatch() | ||
{ | ||
string location = "Hello"; | ||
string newValue = "World"; | ||
string comparand = "What?"; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Interlocked.CompareExchange(ref location, newValue, comparand); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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. | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using Benchmarks; | ||
|
||
namespace System.Threading.Tests | ||
{ | ||
[BenchmarkCategory(Categories.CoreFX)] | ||
public class Perf_Lock | ||
{ | ||
private const int IterationCount = 2_000_000; | ||
|
||
ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim(); | ||
|
||
[Benchmark] | ||
public void ReaderWriterLockSlimPerf() | ||
{ | ||
ReaderWriterLockSlim rwLock = _rwLock; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
rwLock.EnterReadLock(); | ||
rwLock.ExitReadLock(); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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. | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using Benchmarks; | ||
|
||
namespace System.Threading.Tests | ||
{ | ||
[BenchmarkCategory(Categories.CoreFX)] | ||
public class Perf_Monitor | ||
{ | ||
private const int IterationCount = 4_000_000; | ||
|
||
object _sync = new object(); | ||
|
||
[Benchmark] | ||
public void EnterExit() | ||
{ | ||
object sync = _sync; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Monitor.Enter(sync); | ||
Monitor.Exit(sync); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void TryEnterExit() | ||
{ | ||
object sync = _sync; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
Monitor.TryEnter(sync, 0); | ||
Monitor.Exit(sync); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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. | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using Benchmarks; | ||
|
||
namespace System.Threading.Tests | ||
{ | ||
[BenchmarkCategory(Categories.CoreFX)] | ||
public class Perf_SpinLock | ||
{ | ||
private const int IterationCount = 1_000_000; | ||
|
||
SpinLock _spinLock = new SpinLock(); | ||
|
||
[Benchmark] | ||
public void EnterExit() | ||
{ | ||
SpinLock spinLock = _spinLock; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
bool lockTaken = false; | ||
|
||
spinLock.Enter(ref lockTaken); | ||
spinLock.Exit(); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void TryEnterExit() | ||
{ | ||
SpinLock spinLock = _spinLock; | ||
|
||
for (int i = 0; i < IterationCount; i++) | ||
{ | ||
bool lockTaken = false; | ||
|
||
spinLock.TryEnter(0, ref lockTaken); | ||
spinLock.Exit(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.