apex-enterprise-patterns / fflib-apex-common

Common Apex Library supporting Apex Enterprise Patterns and much more!
BSD 3-Clause "New" or "Revised" License
913 stars 518 forks source link

Unit of Work is using more DmlRows than expected #353

Closed rsoesemann closed 3 years ago

rsoesemann commented 3 years ago

I'm trying to convince a team to use fflib UoW for test record creation and made some points regarding redability of tests but they show me some benchmarks and it shows CPU time and especially DmlRows are much higher with fflib and UOW.

I created two tests which show there is more Dml Rows than without UOW.

` @IsTest private static void twoDmlRowsForSingleRecord() {

    // Setup
    fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS);

    Opportunity o = new Opportunity(Name='foo', StageName='Open', CloseDate=System.today());
    uow.registerNew(o);

    // Exercise
    uow.commitWork();

    // Verify
    System.assertEquals(1, [SELECT Count() FROM Opportunity]);
    System.assertEquals(1, Limits.getDmlRows());
}    

@IsTest
private static void fourDmlRowsParentChild() {

    // Setup
    fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS);

    Account a = new Account(Name='foo');
    uow.registerNew(a);

    Opportunity o = new Opportunity(Name='foo', StageName='Open', CloseDate=System.today());
    uow.registerNew(o);
    uow.registerRelationship(o, Opportunity.AccountId, a);

    // Exercise
    uow.commitWork();

    // Verify
    System.assertEquals(1, [SELECT Count() FROM Opportunity]);
    System.assertEquals(1, [SELECT Count() FROM Account]);
    System.assertEquals(2, Limits.getDmlRows());
}`

See full UOW Test class here: https://github.com/rsoesemann/apex-domainbuilder/commit/0746d579793ffc56528cd7c179d7b9f037645e63

ImJohnMDaniel commented 3 years ago

G'day @rsoesemann Do you have specific numbers of increased DML statements when using UOW verses when not using UOW??

rsoesemann commented 3 years ago

My bad. fflib uow is using ONE more Dml for setSavepoint() that's all. I had a bug that doubled it. Closing this.

ImJohnMDaniel commented 3 years ago

sounds good. Thanks for the update.