Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert event batching #197

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions DevCycle/Networking/DevCycleService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class DevCycleService: DevCycleServiceProtocol {
return completion((nil, nil, ClientError.InvalidUser))
}

self.batchEventsPayload(events: eventPayload, user: userBody, completion: completion)
self.sendEventsPayload(events: eventPayload, user: userBody, completion: completion)
}

func saveEntity(user: DevCycleUser, completion: @escaping SaveEntityCompletionHandler) {
Expand Down Expand Up @@ -293,41 +293,28 @@ class DevCycleService: DevCycleServiceProtocol {
return eventsJSON
}

private func batchEventsPayload(events: [[String:Any]], user: Any, completion: @escaping PublishEventsCompletionHandler) {
private func sendEventsPayload(events: [[String:Any]], user: Any, completion: @escaping PublishEventsCompletionHandler) {
var eventsRequest = createEventsRequest()
eventsRequest.httpMethod = "POST"
eventsRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
eventsRequest.addValue("application/json", forHTTPHeaderField: "Accept")
eventsRequest.addValue(self.config.sdkKey, forHTTPHeaderField: "Authorization")

let totalEventsCount = events.count
var startIndex = 0
var endIndex = min(self.maxBatchSize, totalEventsCount)

while startIndex < totalEventsCount {
let batchEvents = Array(events[startIndex..<endIndex])

let requestBody: [String: Any] = [
"events": batchEvents,
"user": user
Comment on lines -303 to -312
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (performance): Removal of batching logic could impact performance or functionality.

Consider the implications of sending all events in a single request, such as potential issues with large payloads or server limitations.

]

let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted)
Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")")
eventsRequest.httpBody = jsonBody

self.makeRequest(request: eventsRequest) { data, response, error in
if error != nil || data == nil {
return completion((data, response, error))
}
// Continue with next batch
startIndex = endIndex
endIndex = min(endIndex + self.maxBatchSize, totalEventsCount)
let requestBody: [String: Any] = [
"events": events,
"user": user
]

if startIndex >= totalEventsCount {
return completion((data, response, nil))
}
let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted)
Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")")
eventsRequest.httpBody = jsonBody

self.makeRequest(request: eventsRequest) { data, response, error in
if error != nil || data == nil {
return completion((data, response, error))
}

return completion((data, response, nil))
}
}
}
61 changes: 24 additions & 37 deletions DevCycleTests/Networking/DevCycleServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DevCycleServiceTests: XCTestCase {
let service = MockDevCycleService()
let eventQueue = EventQueue()
let user = try! DevCycleUser.builder().userId("user1").build()
let expectation = XCTestExpectation(description: "Events are flushed in a single batch")
let expectation = XCTestExpectation(description: "10 Events are flushed in a single batch")

// Generate 205 custom events and add them to the queue
for i in 0..<10 {
Expand All @@ -98,11 +98,11 @@ class DevCycleServiceTests: XCTestCase {
XCTAssertEqual(service.makeRequestCallCount, 1, "makeRequest should have been called 1 time")
}

func testFlushingEventsInBatches() {
func testFlushingLargeNumberOfEvents() {
let service = MockDevCycleService()
let eventQueue = EventQueue()
let user = try! DevCycleUser.builder().userId("user1").build()
let expectation = XCTestExpectation(description: "Events are serially queued and flushed in multiple batches")
let expectation = XCTestExpectation(description: "205 Events are flushed in a single batch")

// Generate 205 custom events and add them to the queue
for i in 0..<205 {
Expand All @@ -116,7 +116,7 @@ class DevCycleServiceTests: XCTestCase {
}
wait(for: [expectation], timeout: 3.0)
XCTAssertTrue(service.publishEventsCalled)
XCTAssertEqual(service.makeRequestCallCount, 3, "makeRequest should have been called 3 times")
XCTAssertEqual(service.makeRequestCallCount, 1, "makeRequest should have been called 1 times")
}
}

Expand Down Expand Up @@ -223,43 +223,30 @@ extension DevCycleServiceTests {
}

private func batchEventsPayload(events: [[String:Any]], user: Any, completion: @escaping PublishEventsCompletionHandler) {
let url = URL(string: "http://test.com/v1/events")!
var eventsRequest = URLRequest(url: url)
eventsRequest.httpMethod = "POST"
eventsRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
eventsRequest.addValue("application/json", forHTTPHeaderField: "Accept")
eventsRequest.addValue(self.sdkKey, forHTTPHeaderField: "Authorization")

let totalEventsCount = events.count
var startIndex = 0
var endIndex = min(self.testMaxBatchSize, totalEventsCount)

while startIndex < totalEventsCount {
let batchEvents = Array(events[startIndex..<endIndex])

let requestBody: [String: Any] = [
"events": batchEvents,
"user": user
]
let url = URL(string: "http://test.com/v1/events")!
var eventsRequest = URLRequest(url: url)
eventsRequest.httpMethod = "POST"
eventsRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
eventsRequest.addValue("application/json", forHTTPHeaderField: "Accept")
eventsRequest.addValue(self.sdkKey, forHTTPHeaderField: "Authorization")

let requestBody: [String: Any] = [
"events": events,
"user": user
]

let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted)
Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")")
eventsRequest.httpBody = jsonBody

self.makeRequest(request: eventsRequest) { data, response, error in
if error != nil || data == nil {
return completion((data, response, error))
}
// Continue with next batch
startIndex = endIndex
endIndex = min(endIndex + self.testMaxBatchSize, totalEventsCount)
let jsonBody = try? JSONSerialization.data(withJSONObject: requestBody, options: .prettyPrinted)
Log.debug("Post Events Payload: \(String(data: jsonBody!, encoding: .utf8) ?? "")")
eventsRequest.httpBody = jsonBody

if startIndex >= totalEventsCount {
return completion((data, response, nil))
}
}
self.makeRequest(request: eventsRequest) { data, response, error in
if error != nil || data == nil {
return completion((data, response, error))
}

return completion((data, response, nil))
}
}
}


Expand Down
Loading