2012-09-28 3 views
0

API 서버와 상호 작용하는 여러 가지 방법으로 각 테스트 소요 시간을 테스트하려고합니다. 예를 들어성능을 찾기 위해 일반적으로 래핑하는 방법은 무엇입니까?

나는 이러한 방법을 포장 할 :

여기
public DCResultOfValidateSiteForUser ValidateSiteForUser(int UserId, int UserType, int SiteId) 
    { 
     return Service.ValidateSiteForUser(UserId, UserType, SiteId); 
    } 
    public DCResultOfIsSystemInStandby IsSystemInStandby() 
    { 
     return Service.IsSystemInStandby(); 
    } 

내가 위의 방법 감싸는 할 것입니다 :

public static T TestPerf<T>(Action action) 
    { 
     Stopwatch sw = new Stopwatch(); 
     sw.Start(); 

     action(); 

     sw.Stop(); 

     StackTrace stackTrace = new StackTrace(); 
     StackFrame stackFrame = stackTrace.GetFrame(1); 
     MethodBase methodBase = stackFrame.GetMethod(); 

     TextWriter tw = new StreamWriter("perfLog.txt", true); 
     string s = String.Format("{0} {1}{2,15}", String.Format("{0:M/d/yyyy HH:mm:ss}", DateTime.Now), methodBase.Name, sw.Elapsed); 
     tw.WriteLine(s); 
     tw.Close(); 

     return default(T); 
    } 

그러나, 동작이 무효 반환하기 때문이다. 그때 그때 가지고는 할 : 내가 원하는 것은

public DCResultOfValidateSiteForUser ValidateSiteForUser(int UserId, int UserType, int SiteId) 
    { 
     TestPerf<DCResultOfValidateSiteForUser>(() => Service.ValidateSiteForUser(UserId, UserType, SiteId)); 
     return Service.ValidateSiteForUser(UserId, UserType, SiteId); 
    } 
    public DCResultOfIsSystemInStandby IsSystemInStandby() 
    { 
     TestPerf<DCResultOfIsSystemInStandby>(() => Service.IsSystemInStandby()); 
     return Service.IsSystemInStandby(); 
    } 

:

public DCResultOfIsSystemInStandby IsSystemInStandby() 
    { 
     return TestPerf<DCResultOfIsSystemInStandby>(() => Service.IsSystemInStandby()); 
    } 

나는 100의가있는 한 모든 방법으로 스톱워치 코드를 삽입하고 싶지 않아요.

제공되는 도움에 감사드립니다.

덕분에이 컴파일해야

답변

1

두 번 방법을 실행하지 :

public DCResultOfValidateSiteForUser ValidateSiteForUser(int UserId, int UserType, int SiteId) 
{ 
    DCResultOfValidateSiteForUser result = null; 
    TestPerf<DCResultOfValidateSiteForUser>(() => result = Service.ValidateSiteForUser(UserId, UserType, SiteId)); 
    return result; 
} 
+0

감사합니다! 나는 지금 좀 바보 같아. – jpiccolo

관련 문제