2012-09-04 32 views
1

最近,我發現這篇文章: http://coderjournal.com/2010/10/timing-the-execution-time-of-your-mvc-actions/wcf webservice計時方法?

其中顯示了一個可愛的小屬性,你可以添加到您Controller類,它創建了一個秒錶和棍棒的X-Runtime頭到結果。

我一直在嘗試添加類似於我的WCF Rest服務的東西,但至今無法弄清楚。關於如何在我的web服務上實現類似的任何建議?

+0

查看性能計數器以及秒錶。 – DarthVader

回答

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鏈中來測試計時,但該階段出現瓶頸的可能性不大。更有可能的是,您發送大量數據的速度很慢,或者實施過程中的速度很慢,所有這些都可以通過服務器端或客戶端進行獨立測試。