Skip to content
Merged
Changes from all commits
Commits
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
40 changes: 35 additions & 5 deletions Source/Testably.Abstractions.Testing/Statistics/CallStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ internal class CallStatistics<TType> : IStatistics<TType>
private ConcurrentQueue<MethodStatistic>? _methods;

/// <inheritdoc cref="_methods" />
private ConcurrentQueue<PropertyStatistic>? _properties;
/// <summary>
/// Properties are accessed far more often than they are inspected, so the accesses are stored as values and the
/// <see cref="PropertyStatistic" />s are only created when <see cref="Properties" /> is read.
/// </summary>
private ConcurrentQueue<PropertyAccessEntry>? _properties;

private ConcurrentQueue<MethodStatistic> MethodQueue
=> LazyInitializer.EnsureInitialized(ref _methods)!;

private ConcurrentQueue<PropertyStatistic> PropertyQueue
private ConcurrentQueue<PropertyAccessEntry> PropertyQueue
=> LazyInitializer.EnsureInitialized(ref _properties)!;

public CallStatistics(IStatisticsGate statisticsGate, string name)
Expand All @@ -36,7 +40,26 @@ public CallStatistics(IStatisticsGate statisticsGate, string name)
public MethodStatistic[] Methods => _methods?.ToArray() ?? [];

/// <inheritdoc cref="IStatistics.Properties" />
public PropertyStatistic[] Properties => _properties?.ToArray() ?? [];
public PropertyStatistic[] Properties
{
get
{
if (_properties is null)
{
return [];
}

PropertyAccessEntry[] entries = _properties.ToArray();
PropertyStatistic[] properties = new PropertyStatistic[entries.Length];
for (int i = 0; i < entries.Length; i++)
{
properties[i] = new PropertyStatistic(entries[i].Counter, entries[i].Name,
entries[i].Access);
}

return properties;
}
}

#endregion

Expand Down Expand Up @@ -365,7 +388,7 @@ internal IDisposable RegisterProperty(string name, PropertyAccess access)
if (_statisticsGate.TryGetLock(out IDisposable release))
{
int counter = _statisticsGate.GetCounter();
PropertyQueue.Enqueue(new PropertyStatistic(counter, name, access));
PropertyQueue.Enqueue(new PropertyAccessEntry(counter, name, access));
}

return release;
Expand All @@ -379,7 +402,14 @@ internal IDisposable RegisterPropertyWithoutLock(IDisposable release, string nam
PropertyAccess access)
{
int counter = _statisticsGate.GetCounter();
PropertyQueue.Enqueue(new PropertyStatistic(counter, name, access));
PropertyQueue.Enqueue(new PropertyAccessEntry(counter, name, access));
return release;
}

private readonly struct PropertyAccessEntry(int counter, string name, PropertyAccess access)
{
public int Counter { get; } = counter;
public string Name { get; } = name;
public PropertyAccess Access { get; } = access;
}
}
Loading