Files
ucvl-zonghengjia-open-source/windows/NodeTrafficRepaintSelfTest.cs
T

77 lines
4.4 KiB
C#
Raw Permalink Normal View History

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.Json;
namespace UCVL.VPNPlus;
internal static class NodeTrafficRepaintSelfTest
{
[DllImport("user32.dll")] private static extern int GetGuiResources(IntPtr process,int flag);
internal static Dictionary<string,bool> Check()
{
var checks=new Dictionary<string,bool>();
const int iterations=160;
var clock=DateTimeOffset.UtcNow;
using var surface=new ScenicPanel {Size=new Size(1100,400),LoginArtwork=true};
using var panel=new NodeTrafficPanel(()=>clock) {Location=new Point(10,10)};
surface.Controls.Add(panel);panel.CreateControl();
var dpi=panel.DeviceDpi;
panel.Size=new Size(464*dpi/96,100*dpi/96);
using var actual=new Bitmap(panel.Width,panel.Height);
using var expected=new Bitmap(panel.Width,panel.Height);
NodeTrafficSnapshot Snapshot(int index)=>new()
{
Contract=1,Scope="server_host_interface",Source="linux_host_interface",Direction="interface_perspective",
CounterSince="boot_or_interface_reset",SamplePeriodSeconds=1,GeneratedAt=clock,
Nodes=Enumerable.Range(2,3).Select(node=>new NodeTrafficReading
{
Slug="fixture-"+node,Name="VPN00"+node,SampledAt=clock,IntervalSeconds=1,CounterEpoch="paint-epoch",
UploadRate=index%37==0?null:(index%2==0?1_000_000+index:1024+index),
DownloadRate=index%37==0?null:2048+index,TotalUpload=1_000_000_000L+index,TotalDownload=3_000_000_000L+index,
Stale=index%37==0,
History=[new NodeTrafficHistoryReading
{
SampledAt=clock,IntervalSeconds=1,CounterEpoch="paint-epoch",Interface="eth0",
Upload=index%37==0?null:(index%2==0?1_000_000+index:1024+index),Download=index%37==0?null:2048+index,
Quality=index%37==0?"gap":"ok",GapBefore=index%37==0
}]
}).ToList()
};
panel.SetSnapshot(Snapshot(1));panel.DrawToBitmap(actual,panel.ClientRectangle);
GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();
using var process=Process.GetCurrentProcess();
var beforeGdi=GetGuiResources(process.Handle,0);var beforeUser=GetGuiResources(process.Handle,1);
process.Refresh();var beforeBytes=process.PrivateMemorySize64;
var timer=Stopwatch.StartNew();
for(var i=1;i<=iterations;i++)
{
clock=clock.AddSeconds(1);
panel.SetSnapshot(Snapshot(i));
using(var graphics=Graphics.FromImage(actual))graphics.Clear(Color.Magenta);
panel.DrawToBitmap(actual,panel.ClientRectangle);
}
using(var graphics=Graphics.FromImage(expected))graphics.Clear(Color.Cyan);
panel.DrawToBitmap(expected,panel.ClientRectangle);
var identical=true;
for(var y=0;y<actual.Height&&identical;y++)
for(var x=0;x<actual.Width;x++)if(actual.GetPixel(x,y)!=expected.GetPixel(x,y)) {identical=false;break;}
GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();process.Refresh();
var afterGdi=GetGuiResources(process.Handle,0);var afterUser=GetGuiResources(process.Handle,1);
var afterBytes=process.PrivateMemorySize64;
checks["owned_repaint_sample_replacement_160_iterations_completed"]=panel.ServerCount==3;
checks["dirty_surface_repaint_matches_clean_surface_exactly"]=identical;
checks["repaint_gdi_handles_bounded"]=afterGdi<=beforeGdi+16;
checks["repaint_user_handles_bounded"]=afterUser<=beforeUser+16;
checks["repaint_private_memory_growth_bounded_96MiB"]=afterBytes<=beforeBytes+96*1024*1024;
checks["repaint_cached_samples_bounded"]=Enumerable.Range(2,3).All(node=>panel.HistoryCount("fixture-"+node)<=iterations+1);
File.WriteAllText(Path.Combine(AppContext.BaseDirectory,"node-traffic-repaint-stress.json"),JsonSerializer.Serialize(new
{
passed=checks.Values.All(value=>value),checks,iterations,dpi,seconds=timer.Elapsed.TotalSeconds,
beforeGdi,afterGdi,beforeUser,afterUser,beforeBytes,afterBytes,
scope="owned buffered WinForms surface; synthetic 1-second samples; no production acceptance",
network_modified=false,installed_client_modified=false
},new JsonSerializerOptions {WriteIndented=true}));
return checks;
}
}