2016-07-25 17 views
0

我創建了一個PostSharp方面,它應該記錄我使用它的任何方法的執行時間。在PostSharp中使用StopWatch進行異步方法的不準確性能日誌記錄MethodInterceptionAspect

但是,它似乎沒有按照我的預期工作,sw.ElapsedMilliseconds總是在0到3毫秒之間。

[Serializable] 
[AttributeUsage(AttributeTargets.Method)] 
public sealed class PerfLogAttribute : MethodInterceptionAspect 
{ 
    public override void OnInvoke(MethodInterceptionArgs args) 
    { 
     var sw = new Stopwatch(); 
     sw.Start(); 

     args.Proceed(); 

     sw.Stop(); 

     log.Debug(sw.ElapsedMilliseconds); 
    } 
} 

使用這樣的:

[PerfLog] 
public async Task<bool> DoSomethingAsync() { 
    // Adding a delay to test (or call database async) 
    await Task.Delay(5000); 
    return true; 
} 
+0

你確定Postsharp以async/await的方式工作嗎?它可能只是測量到「等待Task.Delay(5000)」,不包括它。使用Google搜索「PostSharp異步MethodInterceptor」可以找到一些可能相關的點擊。 –

回答

1

正如@ Christian.K說,你只是截取其中實例化異步任務,而不是異步任務本身的方法。您也正在使用Method Interception來完成這項工作,但它並不完全是您需要的模式,因爲您並不需要截取方法執行。你只需要包裝該方法。

您的情況實際上是在http://doc.postsharp.net/async-methods#apply-to-state-machine的文檔中編寫的。

的剖析方面:

[Serializable] 
public class ProfilingAttribute : OnMethodBoundaryAspect 
{ 
    public override void OnEntry(MethodExecutionArgs args) 
    { 
     Stopwatch sw = Stopwatch.StartNew(); 
     args.MethodExecutionTag = sw; 
    } 

    public override void OnExit(MethodExecutionArgs args) 
    { 
     Stopwatch sw = (Stopwatch) args.MethodExecutionTag; 
     sw.Stop(); 
     Console.WriteLine("Method {0} executed for {1}ms.", 
          args.Method.Name, sw.ElapsedMilliseconds); 
    } 
} 

應用:

[Profiling(ApplyToStateMachine = true)] 
public async Task TestProfiling() 
{ 
    await Task.Delay(1000); 
    Thread.Sleep(1000); 
} 

如果您正在使用的明確許可使用它這將無法正常工作在PostSharp 4.2,但它會在PostSharp 4.3,這是可用在https://www.postsharp.net/downloads/postsharp-4.3下載。

關於性能分析的更多信息可以在PostSharp.Samples.Profiling示例http://samples.postsharp.net/中找到。

相關問題