From a1a06c3f753b85cdf162ee51149c59488e64917f Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sat, 28 May 2011 01:47:25 -0400 Subject: [PATCH] Performance test array controller addObject: when clearing the filter predicate. --- Tests/AppKit/CPArrayControllerPerformance.j | 75 +++++++++++++++++---- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/Tests/AppKit/CPArrayControllerPerformance.j b/Tests/AppKit/CPArrayControllerPerformance.j index 083f9b343..0e73ffffd 100644 --- a/Tests/AppKit/CPArrayControllerPerformance.j +++ b/Tests/AppKit/CPArrayControllerPerformance.j @@ -2,16 +2,17 @@ @import @import +var ELEMENTS = 200, + REPEATS = 25; + @implementation CPArrayControllerPerformance : OJTestCase -- (void)testRearrangeObjects +- (CPArrayController)setupWithElements:(int)aCount { - var ELEMENTS = 200, - REPEATS = 25, - ac = [CPArrayController new], + var ac = [CPArrayController new], array = []; - for (var i = 0; i < ELEMENTS; i++) + for (var i = 0; i < aCount; i++) { var s = [Sortable new]; [s setA:i]; @@ -19,15 +20,17 @@ array.push(s); } - var descriptors = [ - [CPSortDescriptor sortDescriptorWithKey:"a" ascending:NO], - ]; - [ac setContent:array]; - [ac setFilterPredicate:[CPPredicate predicateWithFormat:@"(b != %@)", 0]]; + return ac; +} + +- (void)testRearrangeObjects +{ + var ac = [self setupWithElements:ELEMENTS]; + // Filter alone var start = (new Date).getTime(); for (var i = 0; i < REPEATS; i++) @@ -41,7 +44,7 @@ for (var j = 0, count = [sorted count]; j < count; j++) { if (sorted[j].b == 0) - [self fail:"b == 0 should be filtered out (position: "+j+")"]; + [self fail:"b == 0 should be filtered out (position: " + j + ")"]; last = sorted[j]; } } @@ -49,7 +52,9 @@ CPLog.warn("testRearrangeObjects, filter: "+(end-start)+"ms"); - [ac setSortDescriptors:descriptors]; + [ac setSortDescriptors:[ + [CPSortDescriptor sortDescriptorWithKey:"a" ascending:NO], + ]]; // Filter and sort. start = (new Date).getTime(); @@ -64,9 +69,9 @@ for (var j = 0, count = [sorted count]; j < count; j++) { if (sorted[j].b == 0) - [self fail:"b == 0 should be filtered out (position: "+j+")"]; + [self fail:"b == 0 should be filtered out (position: " + j + ")"]; if (sorted[j].a >= last) - [self fail:"array values should be descending (position: "+j+")"]; + [self fail:"array values should be descending (position: " + j + ")"]; last = sorted[j]; } } @@ -75,6 +80,40 @@ CPLog.warn("testRearrangeObjects, filter and sort: "+(end-start)+"ms"); } +- (void)testAddObject_ +{ + var ac = [self setupWithElements:ELEMENTS], + predicate = [ac filterPredicate], + content = [[ac content] copy]; + + // Add object while clearing the predicate. + [ac setClearsFilterPredicateOnInsertion:YES]; + + [ac setSortDescriptors:[ + [CPSortDescriptor sortDescriptorWithKey:"a" ascending:NO], + ]]; + + var start = (new Date).getTime(); + for (var i = 0; i < REPEATS; i++) + { + [ac setFilterPredicate:predicate]; + [ac addObject:[Sortable sortableWithA:i B:i * 2]]; + + var sorted = [ac arrangedObjects], + last = ELEMENTS; + + // Verify that all is well. + for (var j = 0, count = [sorted count]; j < count; j++) + { + if (sorted[j].a >= last) + [self fail:"array values should be descending (position: " + j + ")"]; + last = sorted[j]; + } + } + var end = (new Date).getTime(); + CPLog.warn("testAddObject_, sorted, clear filter on insert: " + (end - start) + "ms"); +} + @end @implementation Sortable : CPObject @@ -83,4 +122,12 @@ int b @accessors; } ++ (id)sortableWithA:(int)anA B:(int)aB +{ + var r = [Sortable new]; + r.a = anA; + r.b = aB; + return r; +} + @end \ No newline at end of file