From f48de2541e2aa066ededd7ba3bfe1be6c4ddbce0 Mon Sep 17 00:00:00 2001
From: Asger Iversen <20689829+AsgerIversen@users.noreply.github.com>
Date: Tue, 21 Jan 2025 15:00:58 +0100
Subject: [PATCH 1/3] WIP on metrics example
---
Demos.sln | 7 +++-
Metrics/Metrics.csproj | 47 +++++++++++++++++++++++++++
Metrics/MyTestStep.cs | 36 +++++++++++++++++++++
Metrics/ProcessMetrics.cs | 67 +++++++++++++++++++++++++++++++++++++++
4 files changed, 156 insertions(+), 1 deletion(-)
create mode 100644 Metrics/Metrics.csproj
create mode 100644 Metrics/MyTestStep.cs
create mode 100644 Metrics/ProcessMetrics.cs
diff --git a/Demos.sln b/Demos.sln
index d883716..0bba570 100644
--- a/Demos.sln
+++ b/Demos.sln
@@ -6,7 +6,8 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTap.Plugins.Demo.Battery", "Battery\OpenTap.Plugins.Demo.Battery.csproj", "{6ED5480D-D6A0-48E3-9B94-599568294626}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTap.Plugins.Demo.ResultsAndTiming", "ResultsAndTiming\OpenTap.Plugins.Demo.ResultsAndTiming.csproj", "{400E13F0-E63E-496B-9E3F-955BECC22444}"
-
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Metrics", "Metrics\Metrics.csproj", "{AD785327-C02C-41CE-894C-AED4C8625172}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -22,6 +23,10 @@ Global
{400E13F0-E63E-496B-9E3F-955BECC22444}.Debug|Any CPU.Build.0 = Debug|Any CPU
{400E13F0-E63E-496B-9E3F-955BECC22444}.Release|Any CPU.ActiveCfg = Release|Any CPU
{400E13F0-E63E-496B-9E3F-955BECC22444}.Release|Any CPU.Build.0 = Release|Any CPU
+ {AD785327-C02C-41CE-894C-AED4C8625172}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {AD785327-C02C-41CE-894C-AED4C8625172}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {AD785327-C02C-41CE-894C-AED4C8625172}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {AD785327-C02C-41CE-894C-AED4C8625172}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/Metrics/Metrics.csproj b/Metrics/Metrics.csproj
new file mode 100644
index 0000000..dda1640
--- /dev/null
+++ b/Metrics/Metrics.csproj
@@ -0,0 +1,47 @@
+
+
+ false
+ false
+ false
+ true
+
+
+ netstandard2.0
+ TUI
+
+
+
+ package.xml
+ false
+
+
+
+ true
+
+
+
+
+ net472
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Metrics/MyTestStep.cs b/Metrics/MyTestStep.cs
new file mode 100644
index 0000000..9a7910f
--- /dev/null
+++ b/Metrics/MyTestStep.cs
@@ -0,0 +1,36 @@
+using OpenTap;
+
+namespace Metrics
+{
+ [Display("MyTestStep", Description: "Insert a description here", Group: "Metrics")]
+
+ public class MyTestStep : TestStep
+ {
+ #region Settings
+ // ToDo: Add property here for each parameter the end user should be able to change
+ #endregion
+ public MyTestStep()
+ {
+ // ToDo: Set default values for properties / settings.
+ }
+
+ public override void PrePlanRun()
+ {
+ base.PrePlanRun();
+ // ToDo: Optionally add any setup code this step needs to run before the testplan starts
+ }
+
+ public override void Run()
+ {
+ // ToDo: Add test case code here
+ RunChildSteps(); //If step has child steps.
+ UpgradeVerdict(Verdict.Pass);
+ }
+
+ public override void PostPlanRun()
+ {
+ // ToDo: Optionally add any cleanup code this step needs to run after the entire testplan has finished
+ base.PostPlanRun();
+ }
+ }
+}
diff --git a/Metrics/ProcessMetrics.cs b/Metrics/ProcessMetrics.cs
new file mode 100644
index 0000000..4c84966
--- /dev/null
+++ b/Metrics/ProcessMetrics.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using OpenTap.Metrics;
+
+namespace OpenTap.Demonstration
+{
+
+ public class ProcessMetrics : IOnPollMetricsCallback
+ {
+ public Stopwatch runnerTimer = Stopwatch.StartNew();
+ public Dictionary prevCpuByPid = new Dictionary();
+ public Dictionary prevCpuTotalByPid = new Dictionary();
+
+ [Metric("Process Memory", null, MetricKind.Poll, DefaultPollRate = 5, DefaultEnabled = true)]
+ public double Memory { get; set; }
+ [Metric("Process CPU", null, MetricKind.Poll, DefaultPollRate = 5, DefaultEnabled = true)]
+ public double CPU { get; set; }
+
+ public ProcessMetrics()
+ {
+ // Initialize the process metrics
+ }
+
+ public void OnPollMetrics(IEnumerable metrics)
+ {
+ Memory = GetMemoryUsageForProcess(Process.GetCurrentProcess().Id);
+ CPU = GetCPUUsageForProcess(Process.GetCurrentProcess().Id);
+ }
+
+ private float GetCPUUsageForProcess(int processId)
+ {
+ if (processId == 0)
+ return 0;
+ var proc = Process.GetProcessById(processId);
+ long runnerCpu = proc.TotalProcessorTime.Ticks;
+ long runnerCpuTotal = runnerTimer.Elapsed.Ticks;
+ if (!prevCpuByPid.ContainsKey(processId))
+ {
+ prevCpuByPid[processId] = 0;
+ prevCpuTotalByPid[processId] = 0;
+ }
+
+ var result = (float)(100.0 * (runnerCpu - prevCpuByPid[processId]) / (runnerCpuTotal - prevCpuTotalByPid[processId])) / Environment.ProcessorCount;
+ prevCpuByPid[processId] = runnerCpu;
+ prevCpuTotalByPid[processId] = runnerCpuTotal;
+ return result;
+ }
+
+ private long GetMemoryUsageForProcess(int processId)
+ {
+ if (processId == 0)
+ return 0;
+ try
+ {
+ var proc = Process.GetProcessById(processId);
+ return proc.WorkingSet64;
+ }
+ catch (Exception ex)
+ {
+ throw new InvalidOperationException("'Memory usage' for a process metric failed", ex);
+ }
+ }
+
+ }
+
+}
From 2531d15cbe9dea0a8a6ae9d52385eb582d3d54e1 Mon Sep 17 00:00:00 2001
From: Asger Iversen <20689829+AsgerIversen@users.noreply.github.com>
Date: Tue, 21 Jan 2025 16:15:50 +0100
Subject: [PATCH 2/3] Renamed to MetricDemo
---
Metrics/MyTestStep.cs | 36 ----------------------
{Metrics => MetricsDemo}/Metrics.csproj | 0
{Metrics => MetricsDemo}/ProcessMetrics.cs | 2 +-
MetricsDemo/package.xml | 16 ++++++++++
4 files changed, 17 insertions(+), 37 deletions(-)
delete mode 100644 Metrics/MyTestStep.cs
rename {Metrics => MetricsDemo}/Metrics.csproj (100%)
rename {Metrics => MetricsDemo}/ProcessMetrics.cs (96%)
create mode 100644 MetricsDemo/package.xml
diff --git a/Metrics/MyTestStep.cs b/Metrics/MyTestStep.cs
deleted file mode 100644
index 9a7910f..0000000
--- a/Metrics/MyTestStep.cs
+++ /dev/null
@@ -1,36 +0,0 @@
-using OpenTap;
-
-namespace Metrics
-{
- [Display("MyTestStep", Description: "Insert a description here", Group: "Metrics")]
-
- public class MyTestStep : TestStep
- {
- #region Settings
- // ToDo: Add property here for each parameter the end user should be able to change
- #endregion
- public MyTestStep()
- {
- // ToDo: Set default values for properties / settings.
- }
-
- public override void PrePlanRun()
- {
- base.PrePlanRun();
- // ToDo: Optionally add any setup code this step needs to run before the testplan starts
- }
-
- public override void Run()
- {
- // ToDo: Add test case code here
- RunChildSteps(); //If step has child steps.
- UpgradeVerdict(Verdict.Pass);
- }
-
- public override void PostPlanRun()
- {
- // ToDo: Optionally add any cleanup code this step needs to run after the entire testplan has finished
- base.PostPlanRun();
- }
- }
-}
diff --git a/Metrics/Metrics.csproj b/MetricsDemo/Metrics.csproj
similarity index 100%
rename from Metrics/Metrics.csproj
rename to MetricsDemo/Metrics.csproj
diff --git a/Metrics/ProcessMetrics.cs b/MetricsDemo/ProcessMetrics.cs
similarity index 96%
rename from Metrics/ProcessMetrics.cs
rename to MetricsDemo/ProcessMetrics.cs
index 4c84966..679dc10 100644
--- a/Metrics/ProcessMetrics.cs
+++ b/MetricsDemo/ProcessMetrics.cs
@@ -6,7 +6,7 @@
namespace OpenTap.Demonstration
{
- public class ProcessMetrics : IOnPollMetricsCallback
+ public class ProcessMetrics : IMetricSource, IOnPollMetricsCallback
{
public Stopwatch runnerTimer = Stopwatch.StartNew();
public Dictionary prevCpuByPid = new Dictionary();
diff --git a/MetricsDemo/package.xml b/MetricsDemo/package.xml
new file mode 100644
index 0000000..5e3126e
--- /dev/null
+++ b/MetricsDemo/package.xml
@@ -0,0 +1,16 @@
+
+
+
+ This is my OpenTAP plugin package.
+
+
+
+
+
+
From c4b118c36acf2d58ce124cf9dc76c892776dcd84 Mon Sep 17 00:00:00 2001
From: Alexander Larsen
Date: Tue, 21 Jan 2025 16:23:43 +0100
Subject: [PATCH 3/3] fix MetricsDemo sln path
---
Demos.sln | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Demos.sln b/Demos.sln
index 0bba570..dabb7b6 100644
--- a/Demos.sln
+++ b/Demos.sln
@@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTap.Plugins.Demo.Batter
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTap.Plugins.Demo.ResultsAndTiming", "ResultsAndTiming\OpenTap.Plugins.Demo.ResultsAndTiming.csproj", "{400E13F0-E63E-496B-9E3F-955BECC22444}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Metrics", "Metrics\Metrics.csproj", "{AD785327-C02C-41CE-894C-AED4C8625172}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Metrics", "MetricsDemo\Metrics.csproj", "{AD785327-C02C-41CE-894C-AED4C8625172}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution