【问题标题】:wcf webservice timing methods?wcf webservice计时方法?
【发布时间】:2012-09-04 18:58:21
【问题描述】:

我最近发现了这篇文章: http://coderjournal.com/2010/10/timing-the-execution-time-of-your-mvc-actions/

它显示了一个不错的小属性,您可以将其添加到 Controller 类中,它会创建一个秒表并将 X-Runtime 标头粘贴到结果中。

我一直在尝试添加与我的 WCF Rest 服务类似的东西,但到目前为止还无法解决。关于如何在我的网络服务上实现类似的东西有什么建议吗?

【问题讨论】:

  • 查看性能计数器和秒表。

标签: c# asp.net wcf service profile


【解决方案1】:

这是aspects 的理想场所。查看post sharp。

然后您可以创建一个类似的方面:

/// <summary>
/// Adds execution timing and automatic logging to the tagged method
/// </summary>
[Serializable]
public class MethodTimeAttribute : OnMethodBoundaryAspect
{
    private String MethodName { get; set; }
    private DateTime StartTime { get; set; }

    /// <summary>
    /// Method executed at build time. Initializes the aspect instance. After the execution
    /// of <see cref="CompileTimeInitialize"/>, the aspect is serialized as a managed 
    /// resource inside the transformed assembly, and deserialized at runtime.
    /// </summary>
    /// <param name="method">Method to which the current aspect instance 
    /// has been applied.</param>
    /// <param name="aspectInfo">Unused.</param>
    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
    {
        MethodName = method.DeclaringType.FullName  "."  method.Name;
    }


    public override void OnEntry(MethodExecutionArgs args)
    {
        StartTime = DateTime.Now;
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        Log.Debug(this, "Method {0} took {1} to execute", MethodName, DateTime.Now - StartTime);
    }
}

你所要做的就是像这样标记你的方法:

[MethodTime]
private void RunFor(TimeSpan runTime){
    // do stuff 
}

高级后期功能需要许可证,但方法边界方面等简单功能是免费的。

虽然这不会测试您的 wcf 服务的任何网络边界,但它会测试您的服务实施时间。

如果您真的愿意,您可以创建一些自定义行为并将其注入您的 WCF 链以测试时间,但在该阶段出现瓶颈的可能性非常小。更有可能是您发送了大量数据,因此速度很慢,或者您的实现速度很慢,所有这些都可以在服务器端或客户端独立测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    相关资源
    最近更新 更多