Skip to content

Add performance tests #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private async Task ReadMessageFromWebSocketAndWriteToPipelineAsync(CancellationT
{
while (!cancelToken.IsCancellationRequested && !_ws.CloseStatus.HasValue)
{
var memory = _pipe.Writer.GetMemory();
var memory = _pipe.Writer.GetMemory(8192);
var result = await _ws.ReceiveAsync(memory, cancelToken).ConfigureAwait(false);
if (
_ws.State == WebSocketState.Open &&
Expand Down
40 changes: 40 additions & 0 deletions tests/Performance/PerfClient/PerfClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../../src/AppModel\NetDaemon.AppModel\NetDaemon.AppModel.csproj" />
<ProjectReference Include="../../../src/AppModel\NetDaemon.AppModel.SourceDeployedApps\NetDaemon.AppModel.SourceDeployedApps.csproj" />
<ProjectReference Include="../../../src/Extensions\NetDaemon.Extensions.Logging\NetDaemon.Extensions.Logging.csproj" />
<ProjectReference Include="../../../src/Extensions\NetDaemon.Extensions.MqttEntityManager\NetDaemon.Extensions.MqttEntityManager.csproj" />
<ProjectReference Include="../../../src/Extensions\NetDaemon.Extensions.Tts\NetDaemon.Extensions.Tts.csproj" />
<ProjectReference Include="../../../src/Runtime\NetDaemon.Runtime\NetDaemon.Runtime.csproj" />
<ProjectReference Include="../../../src/HassModel\NetDeamon.HassModel\NetDaemon.HassModel.csproj" />
<ProjectReference Include="../../../src/HassModel\NetDaemon.HassModel.Integration\NetDaemon.HassModel.Integration.csproj" />
</ItemGroup>
<PropertyGroup>
<CodeAnalysisRuleSet>..\..\..\.linting\roslynator.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Content Include="apps/**/*.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Remove="apps\TtsTest.cs" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions tests/Performance/PerfClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.Hosting;
using NetDaemon.Runtime;
using NetDaemon.AppModel;
using System.Reflection;
using Serilog;
#pragma warning disable CA1812

try
{
await Host.CreateDefaultBuilder(args)
.UseNetDaemonAppSettings()
.UseSerilog((context, provider, logConfig) =>
{
logConfig.ReadFrom.Configuration(context.Configuration);
})
.UseNetDaemonRuntime()
.ConfigureServices((_, services) =>
services
.AddAppsFromAssembly(Assembly.GetEntryAssembly()!)
// Remove this is you are not running the integration!
//.AddNetDaemonStateManager()
)
.Build()
.RunAsync()
.ConfigureAwait(false);
}
catch (Exception e)
{
Console.WriteLine($"Failed to start host... {e}");
throw;
}
2 changes: 2 additions & 0 deletions tests/Performance/PerfClient/apps/Config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Future configuration file for the project.

38 changes: 38 additions & 0 deletions tests/Performance/PerfClient/apps/PerformanceTestApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Diagnostics;
using System.Reactive.Linq;
using Microsoft.Extensions.Logging;
using NetDaemon.AppModel;
using NetDaemon.HassModel;

#pragma warning disable CA1050

/// <summary>
/// First starts the performance test and process all state changes and logs the performance
/// </summary>
[NetDaemonApp]
public class PerformanceTestApp
{
public PerformanceTestApp(IHaContext ha, ILogger<PerformanceTestApp> logger)
{
var counter = 0;
var timer = new Stopwatch();
ha.StateAllChanges()
.Subscribe(x =>
{
if (counter == 0)
{
timer.Start();
logger.LogInformation("Performance test started");
}
if (x.New?.State == "stop")
{
timer.Stop();
logger.LogInformation("Performance test completed in {Time}ms, with {Counter} call with performance {MsgPerSec} msg/sec", timer.ElapsedMilliseconds, counter, Math.Round( counter / (timer.ElapsedMilliseconds / 1000.0)));
timer.Reset();
}
counter++;
});
logger.LogInformation("Starting performance test by sending service call with service start_performance_test");
ha.CallService("netdaemon", "start_performance_test");
}
}
36 changes: 36 additions & 0 deletions tests/Performance/PerfClient/apps/PerformanceTestSecondApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Diagnostics;
using System.Reactive.Linq;
using Microsoft.Extensions.Logging;
using NetDaemon.AppModel;
using NetDaemon.HassModel;

#pragma warning disable CA1050

/// <summary>
/// Second app process all state changes and logs the performance
/// </summary>
[NetDaemonApp]
public class PerformanceTestSecondApp
{
public PerformanceTestSecondApp(IHaContext ha, ILogger<PerformanceTestSecondApp> logger)
{
var counter = 0;
var timer = new Stopwatch();
ha.StateChanges()
.Subscribe(x =>
{
if (counter == 0)
{
timer.Start();
}
if (x.New?.State == "stop")
{
timer.Stop();
logger.LogInformation("Performance test completed in {Time}ms, with {Counter} call with performance {MsgPerSec} msg/sec", timer.ElapsedMilliseconds, counter, Math.Round( counter / (timer.ElapsedMilliseconds / 1000.0)));
timer.Reset();
}
counter++;
});
logger.LogInformation("Waiting for starting performance test");
}
}
36 changes: 36 additions & 0 deletions tests/Performance/PerfClient/apps/PerformanceTestThirdApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Diagnostics;
using System.Reactive.Linq;
using Microsoft.Extensions.Logging;
using NetDaemon.AppModel;
using NetDaemon.HassModel;

#pragma warning disable CA1050

/// <summary>
/// Third app process all state changes and logs the performance
/// </summary>
[NetDaemonApp]
public class PerformanceTestThirdApp
{
public PerformanceTestThirdApp(IHaContext ha, ILogger<PerformanceTestThirdApp> logger)
{
var counter = 0;
var timer = new Stopwatch();
ha.StateChanges()
.Subscribe(x =>
{
if (counter == 0)
{
timer.Start();
}
if (x.New?.State == "stop")
{
timer.Stop();
logger.LogInformation("Performance test completed in {Time}ms, with {Counter} call with performance {MsgPerSec} msg/sec", timer.ElapsedMilliseconds, counter, Math.Round( counter / (timer.ElapsedMilliseconds / 1000.0)));
timer.Reset();
}
counter++;
});
logger.LogInformation("Waiting for starting performance test");
}
}
33 changes: 33 additions & 0 deletions tests/Performance/PerfClient/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"System": "Information",
"Microsoft": "Information",
"System.Net.Http.HttpClient": "Warning",
"daemonapp.app": "Verbose"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext:l}: {Message:lj}{NewLine}{Exception}"
}
}
]
},
"HomeAssistant": {
"Host": "localhost",
"Port": 8002,
"Ssl": false,
"Token": "somefakeone",
"InsecureBypassCertificateErrors": false
},
"NetDaemon": {
"Admin": true,
"ApplicationConfigurationFolder": "./apps"
}
}
21 changes: 21 additions & 0 deletions tests/Performance/PerfServer/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
global using System;
global using System.Net.WebSockets;
global using System.Reactive.Linq;
global using System.Reactive.Threading.Tasks;
global using System.Text;
global using System.Text.Json;
global using System.Text.Json.Serialization;
global using System.Threading.Channels;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using Microsoft.AspNetCore.Hosting.Server;
global using Microsoft.AspNetCore.Hosting.Server.Features;
global using NetDaemon.Client;
global using NetDaemon.Client.HomeAssistant.Model;
global using NetDaemon.Client.Internal;
global using NetDaemon.Client.Internal.Helpers;
global using NetDaemon.Client.Internal.Net;
global using NetDaemon.Client.Internal.Extensions;
global using NetDaemon.Client.Internal.HomeAssistant.Commands;
global using NetDaemon.Client.Settings;
global using NetDaemon.Client.HomeAssistant.Extensions;
37 changes: 37 additions & 0 deletions tests/Performance/PerfServer/Internal/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Contains commands that being sent from ND that needs to be de-serialized by the fake sever

namespace NetDaemon.Tests.Performance;

#pragma warning disable CA1852

public record CommandMessage
{
[JsonPropertyName("type")] public string Type { get; init; } = string.Empty;
[JsonPropertyName("id")] public int Id { get; set; }
}

internal record CreateInputBooleanCommand : CommandMessage
{
public CreateInputBooleanCommand()
{
Type = "input_boolean/create";
}

[JsonPropertyName("name")] public required string Name { get; init; }
}

internal record CallServiceCommand : CommandMessage
{
public CallServiceCommand()
{
Type = "call_service";
}

[JsonPropertyName("domain")] public string Domain { get; init; } = string.Empty;

[JsonPropertyName("service")] public string Service { get; init; } = string.Empty;

[JsonPropertyName("service_data")] public object? ServiceData { get; init; }

[JsonPropertyName("target")] public HassTarget? Target { get; init; }
}
Loading