Commit e6e58805 authored by shrabbit's avatar shrabbit
Browse files

Add `IPerfSource` and `IPerfConfig` interfaces, implement `TestSource`, and...

Add `IPerfSource` and `IPerfConfig` interfaces, implement `TestSource`, and update UI to display performance data
parent 5438e946
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
using MathNet.Numerics;
using Perf.Shared;

namespace Perf.Core;

public static class ExponentialInterpolation
{
    public static void Do()
    {
        //Interpolate.CubicSpline();
    }
}
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
@@ -6,4 +6,12 @@
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <ProjectReference Include="..\Perf.Shared\Perf.Shared.csproj" />
    </ItemGroup>

    <ItemGroup>
      <PackageReference Include="MathNet.Numerics" Version="5.0.0" />
    </ItemGroup>

</Project>
+22 −0
Original line number Diff line number Diff line
using Perf.Shared;

namespace Perf.Core.Sources;

public class TestSource : PerfSourceBase, IPerfConfig
{
    public override event EventHandler<PerfData>? OnReceivedData;
    public override string Name => "测试";
    public override string Description => "描述文本";
    public override string Unit => "ABC";
    public TimeSpan RefreshRate { get; set; }

    public override async Task Run(CancellationToken cancellationToken)
    {
        while (true)
        {
            OnReceivedData?.Invoke(this, new PerfData(DateTime.Now, Random.Shared.NextDouble()));
            
            await Task.Delay(5, cancellationToken);
        }
    }
}
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
namespace Perf.Shared;

public interface IPerfConfig
{
    TimeSpan RefreshRate { get; set; }
}
 No newline at end of file
+44 −1
Original line number Diff line number Diff line
namespace Perf.Shared;
using System.Diagnostics;

namespace Perf.Shared;

/// <summary>
/// Represents a performance data source that can be started and stopped, providing performance data through an event.
/// </summary>
public interface IPerfSource
{
    event EventHandler<PerfData> OnReceivedData;
    event EventHandler<Exception> OnException;
    
    string Name { get; }
    string Description { get; }
@@ -11,3 +17,40 @@ public interface IPerfSource
    void Start();
    void Stop();
}

public abstract class PerfSourceBase : IPerfSource
{
    public abstract event EventHandler<PerfData>? OnReceivedData;
    public event EventHandler<Exception>? OnException;
    public abstract string Name { get; }
    public abstract string Description { get; }
    public abstract string Unit { get; }

    private CancellationTokenSource _stop = new();
    
    public void Start()
    {
        StartInternal();
    }

    public void Stop()
    {
        _stop.Cancel();
        _stop = new();
    }

    private async void StartInternal()
    {
        try
        {
            await Run(_stop.Token);
        }
        catch (OperationCanceledException) { }
        catch (Exception e)
        {
            OnException?.Invoke(this, e);
        }
    }

    public abstract Task Run(CancellationToken cancellationToken);
}
 No newline at end of file
Loading