Skip to content

Commit

Permalink
Port System.Threading.Performance.Tests and remove duplicated benchma…
Browse files Browse the repository at this point in the history
…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
adamsitnik authored Jul 2, 2018
1 parent b56fc31 commit a2aca5c
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 72 deletions.
72 changes: 0 additions & 72 deletions src/benchmarks/coreclr/perflab/ThreadingPerf.cs

This file was deleted.

35 changes: 35 additions & 0 deletions src/benchmarks/corefx/System.Threading/Perf.EventWaitHandle.cs
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 src/benchmarks/corefx/System.Threading/Perf.Interlocked.cs
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);
}
}
}
}
29 changes: 29 additions & 0 deletions src/benchmarks/corefx/System.Threading/Perf.Lock.cs
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();
}
}
}
}
41 changes: 41 additions & 0 deletions src/benchmarks/corefx/System.Threading/Perf.Monitor.cs
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);
}
}
}
}
45 changes: 45 additions & 0 deletions src/benchmarks/corefx/System.Threading/Perf.SpinLock.cs
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();
}
}
}
}
Loading

0 comments on commit a2aca5c

Please sign in to comment.