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 Check() { var checks=new Dictionary(); 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;ypanel.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; } }