Skip to content
Draft
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
7 changes: 6 additions & 1 deletion Demos.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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", "MetricsDemo\Metrics.csproj", "{AD785327-C02C-41CE-894C-AED4C8625172}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions MetricsDemo/Metrics.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<TargetFrameworkIdentifier></TargetFrameworkIdentifier>
<TargetFrameworkVersion></TargetFrameworkVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<DebugWith>TUI</DebugWith>
</PropertyGroup>

<PropertyGroup>
<OpenTapPackageDefinitionPath>package.xml</OpenTapPackageDefinitionPath>
<CreateOpenTapPackage>false</CreateOpenTapPackage>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<CreateOpenTapPackage>true</CreateOpenTapPackage>
</PropertyGroup>

<PropertyGroup Condition="'$(OS)' == 'WINDOWS_NT' AND '$(Configuration)' == 'Debug'">
<!--
We target .NET Framework in debug builds when debugging with the WPF Editor due to a bug in Visual Studio's debugger.
The debugger assumes that netstandard projects should be debugged as .NET Core apps, and thus launches a .NET Core debugger
which fails to attach because tap.exe is a .NET Framework application.

To ensure maximum compatibility, we recommend targetting netstandard2.0 in release builds, unless you need specific
APIs that are not available in netstandard2.0.
-->
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTAP" Version="9.26.1" />
<OpenTapPackageReference Include="Metrics" version="0.2-beta"/>
</ItemGroup>

<ItemGroup Condition="'$(DebugWith)' == 'TUI' AND '$(Configuration)' == 'Debug'">
<OpenTapPackageReference Include="TUI" version="1"/>
</ItemGroup>

<ItemGroup Condition="'$(DebugWith)' == 'Editor' AND '$(Configuration)' == 'Debug'">
<OpenTapPackageReference Include="Editor" version="9"/>
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions MetricsDemo/ProcessMetrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTap.Metrics;

namespace OpenTap.Demonstration
{

public class ProcessMetrics : IMetricSource, IOnPollMetricsCallback
{
public Stopwatch runnerTimer = Stopwatch.StartNew();
public Dictionary<int, long> prevCpuByPid = new Dictionary<int, long>();
public Dictionary<int, long> prevCpuTotalByPid = new Dictionary<int, long>();

[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<MetricInfo> 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);
}
}

}

}
16 changes: 16 additions & 0 deletions MetricsDemo/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
InfoLink: Specifies a location where additional information about the package can be found.
Version: The version of the package. Must be in a semver 2.0 compatible format. This can be automatically updated from GIT.

For Version the following macro is available (Only works if the project directory is under Git source control):
$(GitVersion) - Gets the version from Git in the recommended format Major.Minor.Build-PreRelease+CommitHash.BranchName.
-->
<Package Name="MetricsDemo" xmlns="http://opentap.io/schemas/package" InfoLink="" Version="0.1.0-alpha" OS="Windows,Linux">
<Description>This is my OpenTAP plugin package.</Description>
<Files>
<File Path="Packages/Metrics/Metrics.dll" SourcePath="Metrics.dll">
<SetAssemblyInfo Attributes="Version"/>
</File>
</Files>
</Package>