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);
    }

ただし、Action は void を返すため。次に、次のことを行います。

    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 があるので、すべてのメソッドにストップウォッチ コードを配置する必要はありません。

提供できるヘルプに感謝します。

ありがとう

4

1 に答える 1

1

これにより、メソッドが 2 回コンパイルされ、実行されなくなります。

public DCResultOfValidateSiteForUser ValidateSiteForUser(int UserId, int UserType, int SiteId)
{
    DCResultOfValidateSiteForUser result = null;
    TestPerf<DCResultOfValidateSiteForUser>(() => result = Service.ValidateSiteForUser(UserId, UserType, SiteId));
    return result;
}
于 2012-09-28T19:15:13.123 に答える