Tedd.Profiler
1.0.0
See the version list below for details.
dotnet add package Tedd.Profiler --version 1.0.0
NuGet\Install-Package Tedd.Profiler -Version 1.0.0
<PackageReference Include="Tedd.Profiler" Version="1.0.0" />
paket add Tedd.Profiler --version 1.0.0
#r "nuget: Tedd.Profiler, 1.0.0"
// Install Tedd.Profiler as a Cake Addin #addin nuget:?package=Tedd.Profiler&version=1.0.0 // Install Tedd.Profiler as a Cake Tool #tool nuget:?package=Tedd.Profiler&version=1.0.0
Tedd.Profiler
Simple profiler for measuring parts of application.
Availabe on NuGet: https://www.nuget.org/packages/Tedd.Profiler
Examples
The Tedd.Profiler.Examples project contains examples on how to use profiler.
For example, averaging ping times:
public class PingAverage: IWorker
{
// Set up a Profiler using default ProfilerRoot
private static readonly Profiler _profiler = ProfilerRoot.Default.CreateInstance(new ProfilerOptions(ProfilerType.SampleAverageTimeMs, 1_000, 10_000));
private bool _running = false;
public Task Task { get; private set; }
public void Start()
{
// Set up a task to run
_running = true;
Task = Task.Run(RunLoop);
}
public void Stop()
{
// Signal task to stop at next iteration
_running = false;
}
private void RunLoop()
{
// Set up ping
var ping = new Ping();
var options = new PingOptions()
{
DontFragment = true
};
var data = new String('*', 32);
var buffer = Encoding.ASCII.GetBytes(data);
var timeout = 120;
// Do forever until Stop() is called
while (_running)
{
// Send ping
var reply = ping.Send("www.google.com", timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
// // // // // // // // // // // // // //
// Add a measurement to our profiler. //
// // // // // // // // // // // // // //
_profiler.AddTimeMeasurement(reply.RoundtripTime * 10_000, 1);
}
// Throttle so we don't flood target
Thread.Sleep(500);
}
}
}
Creating a Profiler using a ProfilerRoot allows us to pull the key+values from that.
// Print result
foreach (var kv in ProfilerRoot.Default.GetMeasurements())
{
var key = kv.Key.PadRight(50, ' ') + ": ";
Console.WriteLine(key + kv.Value.PadLeft(15, ' '));
}
Name
When creating a Profiler you can choose any name. Multiple instances of the profiler can have same name.
Different modes
Each instance of the profiler can function in different modes.
ProfilerType | Description |
---|---|
Counter | Thread synchronized (or not synchronized) integer |
Text | Text |
TimeTotal | Thread synchronized cumulative time |
SampleAverageTimeMs | Thread synchronized average of cumulative time based on sample count |
CountAveragePerSecond | Same as TimeAverage, but value is calculated as samples per second. |
Counter
The counter can be increased, decreased or set. You can choose between atomic (thread safe) operation or not. An atomic operation uses Interlocked.Add or Interlocked.Inc to increase counter, whereas non-atomic simply add directly. Called directly the atomic is around 2x slower than non-atomic, but your actual result may vary. You must pick the best suited method for your use case.
TimeTotal
Similar to Counter, but uses the ticks-part of AddSample. It will get results from use of timer (with CreateTimer()).
SampleAverageTimeMs
Calculating sample average time is done by keeping history of each record added. ProfilerOptions has parameters for cleaning up excess or expires items.
During Cleanup() all excess items are removed from history and subtracted from global numbers. Cleanup() is run when you pull numbers (GetValue() or GetText()), you can also run it manually if you don't pull statistics so often but write a lot of samples.
If you are looking for operations per second then keeping a maximum history of 100 records and 2 000 ms may be enough.
CountAveragePerSecond
Same as SampleAverageTimeMs, except value calculated will be samples per second based on time one sample takes.
Performance
Implementations are kept simple. Objects are pooled internally for reuse.
The heaviest operation is averaging types, where a ConcurrentQueue keeps track of history. Adding a sample is relatively fast, but running Cleanup() (automatically run by GetValue()/GetText()) takes a bit more resources.
A modern computer should be able to reach excess of 20 million measurements per second. Even with a history of 20 million entries and cleanup every 50ms it should reach excess of 17 million samples per second. You can test different parameters yourself inside OperationsPerSecond class in the Example project.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Tedd.ObjectPool (>= 1.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.2 | 593 | 5/17/2020 |
1.0.1 | 461 | 5/17/2020 |
1.0.0 | 483 | 5/17/2020 |
0.0.1-beta.2 | 310 | 5/17/2020 |
0.0.1-beta.1 | 267 | 5/16/2020 |
Initial release. Measuring working, but missing central collector for useful output.