Skip to content

Commit

Permalink
add new property flushBatchSize for fine tuning the network request
Browse files Browse the repository at this point in the history
  • Loading branch information
zihejia committed Nov 10, 2023
1 parent 1ce27d9 commit 4f222e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
12 changes: 11 additions & 1 deletion MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MixpanelDemoTests: MixpanelBaseTests {
XCTAssert(waitTime >= 110, "Network backoff time is less than 2 minutes.")
XCTAssert(testMixpanel.flushInstance.flushRequest.networkConsecutiveFailures == 2,
"Network failures did not equal 2")

XCTAssert(eventQueue(token: testMixpanel.apiToken).count == 2,
"Removed an event from the queue that was not sent")
removeDBfile(testMixpanel.apiToken)
Expand All @@ -51,6 +51,16 @@ class MixpanelDemoTests: MixpanelBaseTests {
"events should have been flushed")
removeDBfile(testMixpanel.apiToken)
}

func testFlushProperties() {
let testMixpanel = Mixpanel.initialize(token: randomId(), trackAutomaticEvents: true, flushInterval: 60)
XCTAssertTrue(testMixpanel.flushBatchSize == 50, "the default flush batch size is set to 50")
XCTAssertTrue(testMixpanel.flushInterval == 60, "flush interval is set correctly")
testMixpanel.flushBatchSize = 10
XCTAssertTrue(testMixpanel.flushBatchSize == 10, "flush batch size is set correctly")
testMixpanel.flushInterval = 30
XCTAssertTrue(testMixpanel.flushInterval == 30, "flush interval is set correctly")
}

func testFlushPeople() {
let testMixpanel = Mixpanel.initialize(token: randomId(), trackAutomaticEvents: true, flushInterval: 60)
Expand Down
12 changes: 11 additions & 1 deletion Sources/Flush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Flush: AppLifecycle {
var flushRequest: FlushRequest
var flushOnBackground = true
var _flushInterval = 0.0
var _flushBatchSize = APIConstants.batchSize
private let flushIntervalReadWriteLock: DispatchQueue

var flushInterval: Double {
Expand All @@ -41,6 +42,15 @@ class Flush: AppLifecycle {
startFlushTimer()
}
}

var flushBatchSize: Int {
get {
return _flushBatchSize
}
set {
_flushBatchSize = newValue
}
}

required init(basePathIdentifier: String) {
self.flushRequest = FlushRequest(basePathIdentifier: basePathIdentifier)
Expand Down Expand Up @@ -88,7 +98,7 @@ class Flush: AppLifecycle {
func flushQueueInBatches(_ queue: Queue, type: FlushType) {
var mutableQueue = queue
while !mutableQueue.isEmpty {
let batchSize = min(mutableQueue.count, APIConstants.batchSize)
let batchSize = min(mutableQueue.count, flushBatchSize)
let range = 0..<batchSize
let batch = Array(mutableQueue[range])
let ids: [Int32] = batch.map { entity in
Expand Down
21 changes: 18 additions & 3 deletions Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
}
}

/// The flushBatchSize property controls the number of events sent in a single network request to the Mixpanel server.
/// Batch size affects how data is flushed from the client to Mixpanel. By configuring the size of the batch, you can
/// optimize network usage and control how frequently the client communicates with the server.
open var flushBatchSize: Int {
get {
return flushInstance.flushBatchSize
}
set {
flushInstance.flushBatchSize = newValue
}
}


/// The base URL used for Mixpanel API requests.
/// Useful if you need to proxy Mixpanel requests. Defaults to
/// https://api.mixpanel.com.
Expand Down Expand Up @@ -198,6 +211,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
var optOutStatus: Bool?
var useUniqueDistinctId: Bool
var timedEvents = InternalProperties()

let readWriteLock: ReadWriteLock
#if os(iOS) && !targetEnvironment(macCatalyst)
static let reachability = SCNetworkReachabilityCreateWithName(nil, "api.mixpanel.com")
Expand Down Expand Up @@ -928,16 +942,16 @@ extension MixpanelInstance {
// automatic events will NOT be flushed until one of the flags is non-nil
let eventQueue = self.mixpanelPersistence.loadEntitiesInBatch(
type: self.persistenceTypeFromFlushType(.events),
batchSize: performFullFlush ? Int.max : APIConstants.flushSize,
batchSize: performFullFlush ? Int.max : self.flushBatchSize,
excludeAutomaticEvents: !self.trackAutomaticEventsEnabled
)
let peopleQueue = self.mixpanelPersistence.loadEntitiesInBatch(
type: self.persistenceTypeFromFlushType(.people),
batchSize: performFullFlush ? Int.max : APIConstants.flushSize
batchSize: performFullFlush ? Int.max : self.flushBatchSize
)
let groupsQueue = self.mixpanelPersistence.loadEntitiesInBatch(
type: self.persistenceTypeFromFlushType(.groups),
batchSize: performFullFlush ? Int.max : APIConstants.flushSize
batchSize: performFullFlush ? Int.max : self.flushBatchSize
)

self.networkQueue.async { [weak self, completion] in
Expand Down Expand Up @@ -1514,4 +1528,5 @@ extension MixpanelInstance {
func setOnce(properties: Properties) {
people?.setOnce(properties: properties)
}

}

0 comments on commit 4f222e4

Please sign in to comment.