【发布时间】:2020-01-31 10:17:05
【问题描述】:
这里是案例: 我在大约 80 个客户端上有 wpf 应用程序,它们为数据处理通信单个 .net 框架 api。我有秒表跟踪器,用于跟踪 Wpf 和 api 应用程序的持续时间。代码示例:
我的 api 属性:
public class DurationControlLoggerAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var action = actionContext.ActionDescriptor.ActionName;
actionContext.ActionArguments.Add("_stopwatch_", Stopwatch.StartNew());
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (ConfigurationManager.AppSettings["ApiLogger"].ToLower().Trim() != "true")
return;
var controller = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var action = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
var stopWatch = (Stopwatch)actionExecutedContext.ActionContext.ActionArguments["_stopwatch_"];
stopWatch.Stop();
var scope = actionExecutedContext.ActionContext.Request.GetDependencyScope();
var commonService = (ICommonService)scope.GetService(typeof(ICommonService));
commonService.InsertLog(new Model.Common.LogModel
{
InsertDate = DateTime.Now.ToString(),
LogLevel = "API",
MachineName = "API",
Message = $"Controller : {controller} - Action : {action} - TotalSeconds : {stopWatch.Elapsed.TotalSeconds}",
StackTrace = string.Empty
});
}
}
动作示例:
[HttpPost]
[DurationControlLogger]
public bool InsertProduct(ProductModel model)
{
return _mainService.TracingService.InsertProduct(model);
}
此操作过程持续时间约为 0.03 秒。另一方面,wpf api 调用持续时间约为 10 秒。 Wpf 代码块如下:
var Stopwatch = Stopwatch.StartNew();
var isSuccess = DataHelper.InsertProduct(Product);
Stopwatch.Stop();
if (Stopwatch.Elapsed.TotalSeconds > 2)
DataHelper.InsertTraceLog($"ProducrtBusiness - InsertProduct TotalSecond : {Stopwatch.Elapsed.TotalSeconds}");
DataHelper.InsertProduct 方法执行基本的 http post 请求。代码在这里:
public static class HttpClientHelper
{
public static T Post<T>(object model, string url)
{
var resultStatus = false;
T resultData = default(T);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<T>(data);
resultStatus = response.StatusCode == System.Net.HttpStatusCode.OK;
resultData = result;
}
}
return resultStatus ? resultData : default(T);
}
.....
有人知道这种情况吗?
编辑
我添加了最后一个日志代码。这是代码: ` var stopwatch = Stopwatch.StartNew();
HttpResponseMessage response = client.PostAsync(url, content).Result;
stopwatch.Stop();
if (stopwatch.Elapsed.TotalSeconds > 2)
{
AppendToFile($"{DateTime.Now.ToString()} - {model.ToString()} - {stopwatch.Elapsed.TotalSeconds} - Content: {jsonData}");
}
`
此日志持续时间有时仍为 8-10 秒。
【问题讨论】:
-
我会使用像 wireshark 或 fiddler 这样的嗅探器并查看以下两种方法 1) 数据是否压缩/编码 2) 你看到的是 http 1.0(流模式)还是 1.1 块模式。版本将在标头中 3)检查请求和响应之间的时间以确定延迟是在客户端还是在服务器上。
-
感谢@jdwend 的建议。我在客户端机器上安装了 wireshark 来跟踪网络流量(ps:fiddler 由于政策原因没有工作)。我检查了所有数据流量,每个请求持续时间都在 1 秒以下。但我仍然有 8-10 秒的日志.. 客户端机器中的问题。
-
日志文件有多大。日志文件的附加似乎需要很长时间。驱动器上剩余多少空间?您使用的是什么类型的驱动器(固态驱动器或硬盘驱动器)?在嗅探器中,您是否在响应中看到 200 OK 的状态?延迟是因为 AppendToFile 还是 jason 反序列化?
-
@jdweng,我在嗅探器中看到每个请求的状态 200 OK。我找到了解决方案。我将发布代码。
标签: c# wpf api request duration