GUI 啟動完成
This commit is contained in:
@@ -6,4 +6,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coreprofiler" Version="1.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
39
CoreProfilerExample.Common/Extensions/MethodBaseExtension.cs
Normal file
39
CoreProfilerExample.Common/Extensions/MethodBaseExtension.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using CoreProfiler;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace CoreProfilerExample.Common.Extensions
|
||||
{
|
||||
public static class MethodBaseExtension
|
||||
{
|
||||
public static IDisposable ProfilingStep(this MethodBase method, string? stepName = null)
|
||||
{
|
||||
if (stepName == null)
|
||||
{
|
||||
// 取得方法的類型物件
|
||||
var methodType = method.ReflectedType;
|
||||
|
||||
// 取得類別的類型物件
|
||||
var classType = methodType?.ReflectedType ?? methodType;
|
||||
|
||||
// 取出類別底下所有非同步類型的方法,比對出當前的方法類型
|
||||
var classMethodType = classType?
|
||||
.GetMethods()
|
||||
.Select(x => new
|
||||
{
|
||||
Method = x,
|
||||
Attribute = x.GetCustomAttribute<AsyncStateMachineAttribute>(),
|
||||
})
|
||||
.FirstOrDefault(x => x.Attribute?.StateMachineType == methodType);
|
||||
|
||||
var className = classType?.Name ?? "無法取得類別名稱";
|
||||
|
||||
var methodName = classMethodType?.Method.Name ?? method.Name ?? "無法取得方法名稱";
|
||||
|
||||
stepName = $"{classType?.Name}.{methodName}";
|
||||
}
|
||||
|
||||
return ProfilingSession.Current.Step(stepName);
|
||||
}
|
||||
}
|
||||
}
|
||||
61
CoreProfilerExample.Common/Options/CoreProfilerOption.cs
Normal file
61
CoreProfilerExample.Common/Options/CoreProfilerOption.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CoreProfilerExample.Common.Options
|
||||
{
|
||||
public class CoreProfilerOption
|
||||
{
|
||||
/// <summary>
|
||||
/// 資料保存上限(Session)。
|
||||
/// </summary>
|
||||
[JsonProperty("circularBufferSize")]
|
||||
public int CircularBufferSize { get; set; } = 200;
|
||||
/// <summary>
|
||||
/// 要過濾掉的項目。
|
||||
/// </summary>
|
||||
[JsonProperty("filters")]
|
||||
public IEnumerable<CoreProfilerOptionFilter> Filters { get; set; } = [
|
||||
new CoreProfilerOptionFilter
|
||||
{
|
||||
Key = "/coreprofiler",
|
||||
Value = "/coreprofiler",
|
||||
Type = "CoreProfiler.ProfilingFilters.NameContainsProfilingFilter, CoreProfiler",
|
||||
},
|
||||
new CoreProfilerOptionFilter
|
||||
{
|
||||
Key = "static files",
|
||||
Value = "ico,jpg,js,css,svg,json,ttf,woff,woff2,eot",
|
||||
Type = "CoreProfiler.ProfilingFilters.FileExtensionProfilingFilter, CoreProfiler",
|
||||
},
|
||||
];
|
||||
|
||||
public CoreProfilerOption Save(string filename)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||
|
||||
File.WriteAllText(filename, json);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public static CoreProfilerOption UseCoreProfilerSetting()
|
||||
{
|
||||
var filename = Path.Combine(AppContext.BaseDirectory, "coreprofiler.json");
|
||||
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filename);
|
||||
|
||||
return JsonConvert.DeserializeObject<CoreProfilerOption>(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return new CoreProfilerOption().Save(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CoreProfilerExample.Common.Options
|
||||
{
|
||||
public class CoreProfilerOptionFilter
|
||||
{
|
||||
[JsonProperty("key")]
|
||||
public string Key { get; set; } = null!;
|
||||
[JsonProperty("value")]
|
||||
public string Value { get; set; } = null!;
|
||||
[JsonProperty("type")]
|
||||
public string Type { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Common.Extensions;
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Repository.Models.DataModels;
|
||||
using System.Reflection;
|
||||
using static CoreProfilerExample.Common.Constants.WeatherConstant;
|
||||
|
||||
namespace CoreProfilerExample.Repository.Implements
|
||||
@@ -7,6 +9,8 @@ namespace CoreProfilerExample.Repository.Implements
|
||||
public class WeatherForecastRepository : IWeatherForecastRepository
|
||||
{
|
||||
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
|
||||
{
|
||||
using (MethodBase.GetCurrentMethod()?.ProfilingStep())
|
||||
{
|
||||
return Task.Run(() => days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
|
||||
{
|
||||
@@ -16,4 +20,5 @@ namespace CoreProfilerExample.Repository.Implements
|
||||
}) : []);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
using CoreProfilerExample.Repository.Models.DataModels;
|
||||
using CoreProfilerExample.Service.Models.Dtos;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Extensions
|
||||
{
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Common.Extensions;
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Service.Extensions;
|
||||
using CoreProfilerExample.Service.Interfaces;
|
||||
using CoreProfilerExample.Service.Models.Dtos;
|
||||
using CoreProfilerExample.Service.Models.ParameterDtos;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
|
||||
namespace CoreProfilerExample.Service.Implements
|
||||
{
|
||||
public class WeatherService(IWeatherForecastRepository weatherForecast) : IWeatherService
|
||||
public class WeatherService : IWeatherService
|
||||
{
|
||||
public WeatherService(IWeatherForecastRepository weatherForecast)
|
||||
{
|
||||
this.weatherForecast = weatherForecast;
|
||||
}
|
||||
|
||||
private readonly IWeatherForecastRepository weatherForecast = null!;
|
||||
|
||||
public Task<GetWeatherForecastDto> GetWeatherForecastAsync(GetWeatherForecastParameterDto parameterDto)
|
||||
{
|
||||
using (MethodBase.GetCurrentMethod()?.ProfilingStep())
|
||||
{
|
||||
return weatherForecast.GetAsync(parameterDto.ForecastDays).ToDtoAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Models.Dtos
|
||||
namespace CoreProfilerExample.Service.Models.Dtos
|
||||
{
|
||||
public class GetWeatherForecastDto
|
||||
{
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Models.Dtos
|
||||
namespace CoreProfilerExample.Service.Models.Dtos
|
||||
{
|
||||
public class GetWeatherForecastItemDto
|
||||
{
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreProfilerExample.Service.Models.ParameterDtos
|
||||
namespace CoreProfilerExample.Service.Models.ParameterDtos
|
||||
{
|
||||
public class GetWeatherForecastParameterDto
|
||||
{
|
||||
|
||||
@@ -11,6 +11,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Service
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Common", "CoreProfilerExample.Common\CoreProfilerExample.Common.csproj", "{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "方案項目", "方案項目", "{5C1FF8FA-0F30-4581-8173-320356AFD2CC}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
README.md = README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using CoreProfilerExample.Common.Extensions;
|
||||
using CoreProfilerExample.Infrastructure.Extensions;
|
||||
using CoreProfilerExample.Models.Parameters;
|
||||
using CoreProfilerExample.Models.ViewModels;
|
||||
using CoreProfilerExample.Service.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
|
||||
namespace CoreProfilerExample.Controllers
|
||||
{
|
||||
@@ -16,6 +18,8 @@ namespace CoreProfilerExample.Controllers
|
||||
[ProducesResponseType(typeof(GetWeatherForecastItemViewModel), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType(typeof(ExceptionViewModel), (int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> GetWeatherForecastAsync(GetWeatherForecastParameter parameter)
|
||||
{
|
||||
using (MethodBase.GetCurrentMethod()?.ProfilingStep())
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -27,4 +31,5 @@ namespace CoreProfilerExample.Controllers
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CoreProfiler.Web" Version="1.1.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using CoreProfilerExample.Repository.Implements;
|
||||
using CoreProfiler.Web;
|
||||
using CoreProfilerExample.Common.Options;
|
||||
using CoreProfilerExample.Repository.Implements;
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Service.Implements;
|
||||
using CoreProfilerExample.Service.Interfaces;
|
||||
@@ -18,5 +20,14 @@ namespace CoreProfilerExample.Infrastructure.Extensions
|
||||
services.AddScoped<IWeatherService, WeatherService>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseCoreProfiler(this IApplicationBuilder app)
|
||||
{
|
||||
CoreProfilerOption.UseCoreProfilerSetting();
|
||||
|
||||
app.UseCoreProfiler(true);
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,8 @@ app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
#region CoreProfilerExample
|
||||
app.UseCoreProfiler();
|
||||
#endregion
|
||||
|
||||
app.Run();
|
||||
|
||||
BIN
CoreProfilerExampleResource/LastestProfilingResult.JPG
Normal file
BIN
CoreProfilerExampleResource/LastestProfilingResult.JPG
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
BIN
CoreProfilerExampleResource/LastestProfilingResultDetail.JPG
Normal file
BIN
CoreProfilerExampleResource/LastestProfilingResultDetail.JPG
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
120
README.md
120
README.md
@@ -1 +1,119 @@
|
||||
# CoreProfilerExample
|
||||
## CoreProfiler
|
||||
|
||||
```Shell
|
||||
NUGET INSTALL coreprofiler -Version 1.1.4
|
||||
```
|
||||
|
||||
用於紀錄特定區塊內的執行耗時。
|
||||
|
||||
基本手法如下:
|
||||
|
||||
```C#
|
||||
var stepName = $"{class_name}.{method_name}";
|
||||
|
||||
using (ProfilingSession.Current.Step(stepName))
|
||||
{
|
||||
// 需要計時的程式碼片段
|
||||
}
|
||||
```
|
||||
|
||||
由於大部分都是使用相同的紀錄模型,因次本範例內提供一個擴充方法,透過反射取得類別與方法名稱。
|
||||
|
||||
```C#
|
||||
// ./CoreProfilerExample.Common/Extensions/MethodBaseExtension.cs
|
||||
|
||||
public static IDisposable ProfilingStep(this MethodBase method, string? stepName = null)
|
||||
{
|
||||
if (stepName == null)
|
||||
{
|
||||
// 取得方法的類型物件
|
||||
var methodType = method.ReflectedType;
|
||||
|
||||
// 取得類別的類型物件
|
||||
var classType = methodType?.ReflectedType ?? methodType;
|
||||
|
||||
// 取出類別底下所有非同步類型的方法,比對出當前的方法類型
|
||||
var classMethodType = classType?
|
||||
.GetMethods()
|
||||
.Select(x => new
|
||||
{
|
||||
Method = x,
|
||||
Attribute = x.GetCustomAttribute<AsyncStateMachineAttribute>(),
|
||||
})
|
||||
.FirstOrDefault(x => x.Attribute?.StateMachineType == methodType);
|
||||
|
||||
var className = classType?.Name ?? "無法取得類別名稱";
|
||||
|
||||
var methodName = classMethodType?.Method.Name ?? method.Name ?? "無法取得方法名稱";
|
||||
|
||||
stepName = $"{classType?.Name}.{methodName}";
|
||||
}
|
||||
|
||||
return ProfilingSession.Current.Step(stepName);
|
||||
}
|
||||
```
|
||||
|
||||
擴充 MethodBase 類別,開發者可以自行決定是否要輸入<code>stepName</code>參數,若沒有輸入則會透過反射的技術取得類別與方法的名稱,作為紀錄耗時的區塊階段名稱。
|
||||
|
||||
反射的說明: 透過<code>MethodBase</code>來取得方法的<code>Type</code>物件,再取得類別的<code>Type</code>物件,原本這樣就可以使用<code>Name</code>來取得類別與方法的名稱,但方法若為非同步,則會取出不好閱讀的名稱,因次需要再執行以下操作來取得易於閱讀的名稱:
|
||||
|
||||
```C#
|
||||
var classMethodType = classType?
|
||||
.GetMethods()
|
||||
.Select(x => new
|
||||
{
|
||||
Method = x,
|
||||
Attribute = x.GetCustomAttribute<AsyncStateMachineAttribute>(),
|
||||
})
|
||||
.Where(x => x.Attribute?.StateMachineType == methodType)
|
||||
.FirstOrDefault();
|
||||
```
|
||||
|
||||
利用類別的<code>Type</code>取得其底下的所有方法,再透過比對方法<code>Type</code>的方式取得方法的<code>MethodInfo</code>,就可以使用<code>Name</code>來取得易讀的方法名稱了。
|
||||
|
||||
擴充方法的基本使用方法如下:
|
||||
|
||||
```C#
|
||||
using (MethodBase.GetCurrentMethod()?.ProfilingStep())
|
||||
{
|
||||
// 需要計時的程式碼片段
|
||||
}
|
||||
|
||||
using (MethodBase.GetCurrentMethod()?.ProfilingStep("階段名稱"))
|
||||
{
|
||||
// 需要計時的程式碼片段
|
||||
}
|
||||
```
|
||||
|
||||
## CoreProfiler.Web
|
||||
|
||||
```Shell
|
||||
NUGET INSTALL CoreProfiler.Web -Version 1.1.4
|
||||
```
|
||||
|
||||
將每個階段要追蹤的程式碼包裝好後,再來安裝方便查找紀錄的介面,如下:
|
||||
|
||||
```C#
|
||||
// ./CoreProfilerExample/Infrastructure/Extensions/DependencyInjectionExtension.cs
|
||||
|
||||
public static IApplicationBuilder UseCoreProfiler(this IApplicationBuilder app)
|
||||
{
|
||||
CoreProfilerOption.UseCoreProfilerSetting();
|
||||
|
||||
app.UseCoreProfiler(true);
|
||||
|
||||
return app;
|
||||
}
|
||||
```
|
||||
|
||||
使用<code>CoreProfilerOption.UseCoreProfilerSetting()</code>方法來產生GUI所需的相關設定參數後,使用<code>app.UseCoreProfiler(true)</code>注入應用程式,其參數<code>true</code>表示每一個紀錄片段是否要往下查找是否有其他的紀錄片段。
|
||||
|
||||
啟動後連結 <a>/nanoprofiler/view</a> 確認是否有啟用成功。
|
||||
|
||||
執行紀錄如下:
|
||||
|
||||

|
||||
|
||||
紀錄的詳細如下:
|
||||
|
||||

|
||||
Reference in New Issue
Block a user