1

我已经使用 moles 有一段时间了,发现它对小型项目非常有用。

但是,在处理需要更广泛的安排任务的单元测试时,单元测试需要永远。

如果我在没有分配 HostType("Moles") 属性的情况下运行单元测试代码,则排列操作需要 < 10 秒(在运行预期的上下文时,它是一个 <2 秒的操作)。添加 Moles 主机属性会将这个时间延长到大约 4-5 分钟。

发生了什么事,我能做些什么呢?

----更新关于 BryanBCook 的回答。

以下是似乎是问题的代码(有点编辑)。如果问题是摩尔 IL 重写,它似乎发生在我不希望它应该发生的地方。你知道它是否会重写一切吗?我想我也很想知道它是否只是痣主机进程中的缩放问题。

[ClassInitialize()]
public static void ClassInit(TestContext ctx)
{

    Common.Logging.Moles.MExceptionEvent.LogExceptionStringStringStringString = delegate(Exception ex, string a, string b, string c, string d)
    {
        Debug.WriteLine(String.Format("Exception occurred in test context '{0}' : {1} ", ctx.TestName, ex.ToString()));
    };

    Common.Logging.Moles.MCriticalEvent.LogStringStringTraceEventTypeStringString = delegate(string a, string b, TraceEventType tet, string c, string d)
    {
        Debug.WriteLine(String.Format("Critical Event occurred in test context '{0}' : {1} ", ctx.TestName, a));
    };

    Common.Logging.Moles.MDebugEvent.LogStringStringTraceEventTypeStringString = delegate(string a, string b, TraceEventType tet, string c, string d)
    {
        //Debug.WriteLine(String.Format("Debug Event occurred in test context '{0}' : {1} ", ctx.TestName, a));
    };

/*there are about 1MM lines of code that generate this dataset.*/
    DataSet _ds = dg.STDDataHelper.GenerateDataSet(); 

/*This is where the delay occurs*/
    m_std = new STD(_ds);

/*Now another object (implements web caching) gets moled to use the newly constructed object*/
    BizObjects.Moles.MSCO.STDs = delegate()
    {
        return m_std;

    };


    m_co1 = new Company(dg.Company.CTSDataHelper.COMPANY_ONE_CODE);
    m_co1.HydrateCoTaxDefinitions(dg.Company.CTSDataHelper.GenerateCompanyOneDataset());
    m_co2 = new Company(dg.Company.CTSDataHelper.COMPANY_ONE_CODE);
    m_co2.HydrateCoTaxDefinitions(dg.Company.CTSDataHelper.GenerateCompanyTwoDataset());

}

---另一个更新

因此,重新排列类 init 以便在 STD 初始化后发生日志记录,并删除整个 STD 对象构造树中的 3 个调试日志记录语句(它们确实无用)已将排列操作减少到大约一分半钟。

虽然这不是最优的(STD 对象树构造中的异常将导致测试失败,而不是被测试的实际事物),但它仍然比支付这个惩罚更好。特别是因为施工投入受到严格控制。

话虽如此,我还注意到 moles 主机进程只使用了大约 138MB 的内存和 13% 的处理器。没有痣(并将所有依赖项添加到单元测试中),标准主机将使用更多(这个comp有一个i7 Quad和8GB ram - 有足够的空间)。似乎痣主机进程正在达到某种限制。

4

1 回答 1

2

Moles 运行缓慢的原因是它使用 ProfilerAPI 拦截 IL 并重写它。如果您在启用代码覆盖率的情况下运行测试或使用 Visual Studio 2010 的 TestImpact 功能时,您会看到类似的减速。平均测试需要 4-5 倍的时间。

4-10 minutes seems extreme, though. My guess is the difference between running tests with code coverage and Moles is that for code coverage, instrumentation and set up for the tests happens once, and this is where the bulk of the delay occurs. For Moles, it's possible that the setup cost to initialize the ProfilerAPI is fixed and happens for each test.

于 2011-08-31T01:52:00.207 回答