From 7f93b5280eade62b2c51a44b03d8e8b430c793ee Mon Sep 17 00:00:00 2001 From: BJ Homer Date: Tue, 16 Jul 2024 15:01:28 -0600 Subject: [PATCH 1/4] Send tracks events in batches of no more than 1000 --- CHANGELOG.md | 1 + .../TracksEventPersistenceService.h | 1 + .../TracksEventPersistenceService.m | 29 ++++++++++++++++++- Sources/Event Logging/TracksEventService.h | 1 + Sources/Event Logging/TracksEventService.m | 5 ++++ Sources/Event Logging/TracksService.m | 2 +- 6 files changed, 37 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e697d57c..8269f2e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ _None._ ### New Features - Exposed Sentry's `onCrashedLastRun` to `CrashLoggingDataProvider`. +- Tracks events are now reported in batches of up to 1000 events, for better performance under large loads. ### Bug Fixes diff --git a/Sources/Event Logging/TracksEventPersistenceService.h b/Sources/Event Logging/TracksEventPersistenceService.h index 64de4e3c..d47a8abc 100644 --- a/Sources/Event Logging/TracksEventPersistenceService.h +++ b/Sources/Event Logging/TracksEventPersistenceService.h @@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)persistTracksEvent:(TracksEvent *)tracksEvent; - (NSArray *)fetchAllTracksEvents; +- (NSArray *)fetchTracksEventsWithLimit:(NSInteger)limit; - (NSUInteger)countAllTracksEvents; diff --git a/Sources/Event Logging/TracksEventPersistenceService.m b/Sources/Event Logging/TracksEventPersistenceService.m index 5ef8d8c7..e17d23f7 100644 --- a/Sources/Event Logging/TracksEventPersistenceService.m +++ b/Sources/Event Logging/TracksEventPersistenceService.m @@ -9,6 +9,7 @@ #endif @import Foundation; +@import CoreData; @interface TracksEventPersistenceService () @@ -40,7 +41,7 @@ - (void)persistTracksEvent:(TracksEvent *)tracksEvent } -- (NSArray *)fetchAllTracksEvents +- (NSArray *)fetchAllTracksEvents { __block NSMutableArray *transformedResults; @@ -65,6 +66,32 @@ - (NSArray *)fetchAllTracksEvents return transformedResults; } +- (NSArray *)fetchTracksEventsWithLimit:(NSInteger)limit { + __block NSMutableArray *transformedResults; + + [self.managedObjectContext performBlockAndWait:^{ + NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"TracksEvent"]; + fetchRequest.fetchLimit = limit; + + NSError *error; + NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; + + if (error) { + TracksLogError(@"Error while fetching all TracksEvent: %@", error); + return; + } + + transformedResults = [[NSMutableArray alloc] initWithCapacity:results.count]; + for (TracksEventCoreData *eventCoreData in results) { + TracksEvent *tracksEvent = [self mapToTracksEventWithTracksEventCoreData:eventCoreData]; + [transformedResults addObject:tracksEvent]; + } + }]; + + return transformedResults; +} + + - (NSUInteger)countAllTracksEvents { diff --git a/Sources/Event Logging/TracksEventService.h b/Sources/Event Logging/TracksEventService.h index 58ab4207..743089c4 100644 --- a/Sources/Event Logging/TracksEventService.h +++ b/Sources/Event Logging/TracksEventService.h @@ -25,6 +25,7 @@ withAnonymousUserID:(NSString *)anonymousUserID; - (NSArray *)allTracksEvents; +- (NSArray *)tracksEventsWithLimit:(NSInteger)limit; - (NSUInteger)numberOfTracksEvents; diff --git a/Sources/Event Logging/TracksEventService.m b/Sources/Event Logging/TracksEventService.m index e075c915..a7e44deb 100644 --- a/Sources/Event Logging/TracksEventService.m +++ b/Sources/Event Logging/TracksEventService.m @@ -97,6 +97,11 @@ - (NSArray *)allTracksEvents return [self.persistenceService fetchAllTracksEvents]; } +- (NSArray *)tracksEventsWithLimit:(NSInteger)limit +{ + return [self.persistenceService fetchTracksEventsWithLimit:limit]; +} + - (void)removeTracksEvents:(NSArray *)tracksEvents { diff --git a/Sources/Event Logging/TracksService.m b/Sources/Event Logging/TracksService.m index 01775454..1ad9c9be 100644 --- a/Sources/Event Logging/TracksService.m +++ b/Sources/Event Logging/TracksService.m @@ -161,7 +161,7 @@ - (void)sendQueuedEvents [self.timer invalidate]; [[NSNotificationCenter defaultCenter] postNotificationName:TrackServiceWillSendQueuedEventsNotification object:nil]; - NSArray *events = [self.tracksEventService allTracksEvents]; + NSArray *events = [self.tracksEventService tracksEventsWithLimit:1000]; if (events.count == 0) { [self resetTimer]; From d2be5f46abfdfd3ca00be61ff2eeeb0b9bbbb41a Mon Sep 17 00:00:00 2001 From: BJ Homer Date: Wed, 17 Jul 2024 07:28:59 -0600 Subject: [PATCH 2/4] Fix test, consolidate code --- .../TracksEventPersistenceService.m | 22 +------------------ Tests/Tests/ObjC/TracksServiceTests.m | 2 +- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/Sources/Event Logging/TracksEventPersistenceService.m b/Sources/Event Logging/TracksEventPersistenceService.m index e17d23f7..02b0e50c 100644 --- a/Sources/Event Logging/TracksEventPersistenceService.m +++ b/Sources/Event Logging/TracksEventPersistenceService.m @@ -43,27 +43,7 @@ - (void)persistTracksEvent:(TracksEvent *)tracksEvent - (NSArray *)fetchAllTracksEvents { - __block NSMutableArray *transformedResults; - - [self.managedObjectContext performBlockAndWait:^{ - NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"TracksEvent"]; - - NSError *error; - NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; - - if (error) { - TracksLogError(@"Error while fetching all TracksEvent: %@", error); - return; - } - - transformedResults = [[NSMutableArray alloc] initWithCapacity:results.count]; - for (TracksEventCoreData *eventCoreData in results) { - TracksEvent *tracksEvent = [self mapToTracksEventWithTracksEventCoreData:eventCoreData]; - [transformedResults addObject:tracksEvent]; - } - }]; - - return transformedResults; + return [self fetchTracksEventsWithLimit:0]; } - (NSArray *)fetchTracksEventsWithLimit:(NSInteger)limit { diff --git a/Tests/Tests/ObjC/TracksServiceTests.m b/Tests/Tests/ObjC/TracksServiceTests.m index 28d8b979..b9782e24 100644 --- a/Tests/Tests/ObjC/TracksServiceTests.m +++ b/Tests/Tests/ObjC/TracksServiceTests.m @@ -101,7 +101,7 @@ - (void)testSendQueuedEventsOneEvent tracksEvent.eventName = @"Test"; tracksEvent.userID = @"anonymous123"; NSArray *events = @[tracksEvent]; - OCMExpect([self.tracksEventService allTracksEvents]).andReturn(events); + OCMExpect([self.tracksEventService tracksEventsWithLimit:1000]).andReturn(events); OCMExpect([self.subject.remote sendBatchOfEvents:[OCMArg checkWithBlock:^BOOL(id obj) { XCTAssertTrue([obj isKindOfClass:[NSArray class]]); From 93111571c876bd76ddea144743c4db0bd014542c Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 22 Jul 2024 16:36:16 +1000 Subject: [PATCH 3/4] Add PR number for events batching to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8269f2e7..c82751eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,7 @@ _None._ ### New Features - Exposed Sentry's `onCrashedLastRun` to `CrashLoggingDataProvider`. -- Tracks events are now reported in batches of up to 1000 events, for better performance under large loads. +- Tracks events are now reported in batches of up to 1000 events, for better performance under large loads. [#293] ### Bug Fixes From c96912b113215b5434fc8de56c190d91e0df5d7b Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 22 Jul 2024 16:36:48 +1000 Subject: [PATCH 4/4] Remove some whitespace --- Sources/Event Logging/TracksEventPersistenceService.m | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/Event Logging/TracksEventPersistenceService.m b/Sources/Event Logging/TracksEventPersistenceService.m index 02b0e50c..70140ee5 100644 --- a/Sources/Event Logging/TracksEventPersistenceService.m +++ b/Sources/Event Logging/TracksEventPersistenceService.m @@ -41,7 +41,7 @@ - (void)persistTracksEvent:(TracksEvent *)tracksEvent } -- (NSArray *)fetchAllTracksEvents +- (NSArray *)fetchAllTracksEvents { return [self fetchTracksEventsWithLimit:0]; } @@ -71,8 +71,6 @@ - (NSArray *)fetchTracksEventsWithLimit:(NSInteger)limit { return transformedResults; } - - - (NSUInteger)countAllTracksEvents { __block NSUInteger count = 0;