编写标准化的TDD[测试]方法以显示常见的内存问题是否有用? (Biānxíe biāozhǔnhuà de TDD [cèshì] fāngfǎ yǐ xiǎnshì chángjiàn de nèicún wèntí shìfǒu yǒuyòng?)
这套测试能够轻松、快速地应用于某个方法,能够检测出经典的.NET内存问题并标为红色——不符合标准,同时也能够通过经典解决方案,并标为绿色——符合标准。
For example common memory issues could be : too much relocation by the garbage collector ; allocating too much ; too many garbage collections ( classic example prefer StringBuilder over string reallocs ); holding on to memory for too long (classic example call dispose and do not reling on finalizers ); objects inappropriately reaching g1, g2, LOH ; little leaks that add up to something significant over time, … and others.
也许代码看起来像这样...
[Test]
public void Verify_MyMethodUnderTest_Is_Unlikely_To_Have_Common_Memory_Problem()
{
//-Setup
var ExpectationToleranceA = ...
var ExpectationToleranceB = ...
...
//-Execute
var MeasurementA = MyClassUnderTest.MyMethodUnderTest( dependancyA ) ;
var MeasurementB = MyClassUnderTest.MyMethodUnderTest( dependancyB ) ;
…
//-Verfiy
Assert.That( MeasurementA , Is.WithinTolerance( ExpectationToleranceA ) ) ;
Assert.That( MeasurementB , Is.WithinTolerance( ExpectationToleranceB ) ) ;
}
这里有其他关于内存压力问题的帖子,但这里的想法是可以快速将标准测试指向一个方法,测试会在常见/经典内存压力问题下变为红色失败,但在常见解决方案下变为绿色通过。然后可以指示开发人员审核失败的代码,可能修复泄漏,更改容差甚至删除TDD内存压力测试。
这个想法有潜力吗?
这里有一个相关的问题是针对C++应用程序的,在运行单元测试时检测内存泄漏,这是一个类似的问题但不完全相同。Twk的问题是指在所有测试运行后检查内存...
My idea here is for .NET to 1) unit test each method for common memory issues 2) fail the classic memory issues 3) pass the classic fixes to classic common memory issues 4) be able to quickly throw a quick standard test at a function to see whether it exhibits classic symptoms 5) be able to upgrade the Standard TDD .Net Memory Pressure Test applied in the unit test. This implies a refactor of the above code so that upgrades to the standard test will change upgrade the memory tests applied throughout the Nunit test suite for a project.
(p.s. I know there is no Is.WithinTolerance call but I was just demonstrating an idea. ) cheers ...