Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fd5ff4a
Update Stopwatch.cs
SimonCropp Jan 6, 2023
f433722
.
SimonCropp Jan 6, 2023
179283f
Merge branch 'master' into struct-stopwatch
SimonCropp Jan 6, 2023
55fe0be
Update Stopwatch.cs
SimonCropp Jan 6, 2023
08ac433
.
SimonCropp Jan 6, 2023
358a0fc
Merge branch 'master' into struct-stopwatch
SimonCropp Jan 6, 2023
042a401
Merge branch 'master' into struct-stopwatch
SimonCropp Jan 6, 2023
27a6aef
.
SimonCropp Jan 6, 2023
217b8a4
Merge branch 'master' into struct-stopwatch
SimonCropp Jan 6, 2023
bdd1f7c
.
SimonCropp Jan 6, 2023
82e5f17
Merge branch 'master' into struct-stopwatch
SimonCropp Jan 6, 2023
f224867
Update StopWatchTemplateUsage.cs
SimonCropp Jan 6, 2023
e122bbb
.
SimonCropp Jan 6, 2023
ecfcd2b
Merge branch 'master' into struct-stopwatch
SimonCropp Jan 6, 2023
fe4a97e
Merge branch 'master' into struct-stopwatch
SimonCropp Jan 9, 2023
f984195
Merge branch 'master' into feature/struct-stopwatch-take-2
GeertvanHorrik Feb 19, 2025
7f174dd
Create example assembly for IL comparison
GeertvanHorrik Feb 20, 2025
382681d
Add partial nullable annotations
GeertvanHorrik Feb 20, 2025
fe592c8
Migrate sync methods to new allocation free measurement
GeertvanHorrik Feb 21, 2025
ed6ba26
Add benchmark to ensure the new strategy is an improvement
GeertvanHorrik Feb 22, 2025
7783300
First work on the async implementation
GeertvanHorrik Feb 22, 2025
723aef5
First working async implementation
GeertvanHorrik Feb 22, 2025
1c0852b
Fix more tests
GeertvanHorrik Feb 22, 2025
adbe614
Remove unused file
GeertvanHorrik Feb 22, 2025
c56146b
Tweak to fix more unit tests
GeertvanHorrik Feb 22, 2025
cd6271d
Fix final unit tests
GeertvanHorrik Feb 22, 2025
fb5360e
Cleanup unused code
GeertvanHorrik Feb 22, 2025
8057e96
Cleanup
ltrzesniewski Feb 23, 2025
c8eb36c
I don't think creating field references is necessary
ltrzesniewski Feb 23, 2025
6cde18d
Update readme
GeertvanHorrik Feb 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions AssemblyWithInterceptorAndFormatting/ClassWithMethod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Threading;
using MethodTimer;

Expand All @@ -18,16 +19,18 @@ public void Method(string fileName, int id)

public void Method_Expected(string fileName, int id)
{
var stopwatch = Stopwatch.StartNew();
var start = Stopwatch.GetTimestamp();
try
{
Thread.Sleep(10);
}
finally
{
stopwatch.Stop();
var end = Stopwatch.GetTimestamp();
var duration = end - start;
var elapsedTimeSpan = new TimeSpan((long)(MethodTimerHelper.TimestampToTicks * duration));
var methodTimerMessage = $"File name '{fileName}' with id '{id}'";
MethodTimeLogger.Log(null, stopwatch.ElapsedMilliseconds, methodTimerMessage);
MethodTimeLogger.Log(null, (long)elapsedTimeSpan.TotalMilliseconds, methodTimerMessage);
}
}

Expand Down
6 changes: 6 additions & 0 deletions AssemblyWithInterceptorAndFormatting/MethodTimeLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

public static class MethodTimeLogger
Expand All @@ -18,4 +19,9 @@ public static void Log(MethodBase methodBase, long milliseconds, string message)
Messages.Add(message);
}
}
}

internal static class MethodTimerHelper
{
internal static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;
}
27 changes: 27 additions & 0 deletions ExampleAssembly/Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace ExampleAssembly
{
using BenchmarkDotNet.Attributes;

[MemoryDiagnoser]
public class StopwatchVsTimestamp
{
private TestClassWithStopwatch _testClassWithStopwatch = new TestClassWithStopwatch();
private TestClassWithTimestamp _testClassWithTimestamp = new TestClassWithTimestamp();

public StopwatchVsTimestamp()
{
}

[Benchmark]
public void Timestamp_Sync() => _testClassWithTimestamp.SyncMethod();

[Benchmark]
public void Timestamp_Async() => _testClassWithTimestamp.SyncMethod();

[Benchmark]
public void Stopwatch_Sync() => _testClassWithStopwatch.SyncMethod();

[Benchmark]
public void Stopwatch_Async() => _testClassWithStopwatch.SyncMethod();
}
}
10 changes: 10 additions & 0 deletions ExampleAssembly/ExampleAssembly.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>
</Project>
21 changes: 21 additions & 0 deletions ExampleAssembly/MethodTimeLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Reflection;

public static class MethodTimeLogger
{
public static List<MethodBase> MethodBase = new();
public static List<string> Messages = new();

public static void Log(MethodBase methodBase, long milliseconds, string message)
{
Console.WriteLine($"{methodBase.Name} {milliseconds}: {message}");

MethodBase.Add(methodBase);

if (message != null)
{
Messages.Add(message);
}
}
}
7 changes: 7 additions & 0 deletions ExampleAssembly/MethodTimerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;
using System.Diagnostics;

internal static class MethodTimerHelper
{
internal static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;
}
39 changes: 39 additions & 0 deletions ExampleAssembly/MethodsWithStopwatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;

public class TestClassWithStopwatch
{
public void SyncMethod()
{
var stopwatch = Stopwatch.StartNew();

try
{
// Do something
//Console.WriteLine("Hello, World!");
}
finally
{
stopwatch.Stop();

var elapsedTimeSpan = stopwatch.Elapsed;
}
}

public async Task AsyncMethod()
{
var stopwatch = Stopwatch.StartNew();

try
{
await Task.Delay(5);
}
finally
{
stopwatch.Stop();

var elapsedTimeSpan = stopwatch.Elapsed;
}
}
}
58 changes: 58 additions & 0 deletions ExampleAssembly/MethodsWithTimestamp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;

public class TestClassWithTimestamp
{
public void SyncMethod()
{
long startTimestamp;
long endTimestamp;
long elapsed;
TimeSpan elapsedTimeSpan;

startTimestamp = Stopwatch.GetTimestamp();

try
{
// Do something
//Console.WriteLine("Hello, World!");
}
finally
{
endTimestamp = Stopwatch.GetTimestamp();

elapsed = endTimestamp - startTimestamp;
elapsedTimeSpan = new TimeSpan((long)(MethodTimerHelper.TimestampToTicks * elapsed));
}
}

public async Task AsyncMethod()
{
long startTimestamp = default;
long endTimestamp;
long elapsed;
TimeSpan elapsedTimeSpan;

if (startTimestamp == 0)
{
startTimestamp = Stopwatch.GetTimestamp();
}

try
{
await Task.Delay(5);
}
finally
{
if (startTimestamp != 0)
{
endTimestamp = Stopwatch.GetTimestamp();

elapsed = endTimestamp - startTimestamp;
elapsedTimeSpan = new TimeSpan((long)(MethodTimerHelper.TimestampToTicks * elapsed));
}
}
}
}
46 changes: 46 additions & 0 deletions ExampleAssembly/MethodsWithTimestampPreweaved.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace ExampleAssembly
{
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;

public class MethodsWithTimestampPreweaved
{
private long _startTimestamp = default;
private long _endTimestamp;
private long _elapsed;
private TimeSpan _elapsedTimeSpan;
private int _state = 0;
private string methodTimerMessage;

public async Task AsyncMethod()
{
if (_startTimestamp == 0)
{
_startTimestamp = Stopwatch.GetTimestamp();
}

try
{
await Task.Delay(5);
}
finally
{
StopMethodTimerStopwatch();
}
}

private void StopMethodTimerStopwatch()
{
if (_state == -2 && _startTimestamp != 0)
{
_endTimestamp = Stopwatch.GetTimestamp();
_elapsed = _endTimestamp - _startTimestamp;
_elapsedTimeSpan = new TimeSpan((long)(MethodTimerHelper.TimestampToTicks * (double)_elapsed));
methodTimerMessage = null;
MethodTimeLogger.Log(MethodBase.GetCurrentMethod(), (long)_elapsedTimeSpan.TotalMilliseconds, methodTimerMessage);
}
}
}
}
12 changes: 12 additions & 0 deletions ExampleAssembly/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ExampleAssembly
{
using BenchmarkDotNet.Running;

public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<StopwatchVsTimestamp>();
}
}
}
Loading