From ff181fbd5bb9955a5cf4687dbaabd030ccb9d2a7 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 3 Dec 2024 18:51:36 -0800 Subject: [PATCH] Client creation performance (#442) * performance testing for client creation * bump the pod * bump bindings --- Package.swift | 2 +- Sources/XMTPiOS/Client.swift | 14 +++-- Tests/XMTPTests/ClientTests.swift | 54 +++++++++++++++++++ XMTP.podspec | 4 +- .../xcshareddata/swiftpm/Package.resolved | 4 +- 5 files changed, 68 insertions(+), 10 deletions(-) diff --git a/Package.swift b/Package.swift index d659aa18..7000c8e0 100644 --- a/Package.swift +++ b/Package.swift @@ -21,7 +21,7 @@ let package = Package( .package(url: "https://github.com/bufbuild/connect-swift", exact: "1.0.0"), .package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.4.3"), .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", exact: "1.8.3"), - .package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "3.0.8") + .package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "3.0.9") ], targets: [ .target( diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index 738b768f..89b841a0 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -149,18 +149,22 @@ public final class Client { ) } - public static func build(address: String, options: ClientOptions) + public static func build(address: String, options: ClientOptions, inboxId: String? = nil) async throws -> Client { let accountAddress = address.lowercased() - let inboxId = try await getOrCreateInboxId( - api: options.api, address: accountAddress) - + let resolvedInboxId: String + if let existingInboxId = inboxId { + resolvedInboxId = existingInboxId + } else { + resolvedInboxId = try await getOrCreateInboxId(api: options.api, address: accountAddress) + } + return try await initializeClient( accountAddress: accountAddress, options: options, signingKey: nil, - inboxId: inboxId + inboxId: resolvedInboxId ) } diff --git a/Tests/XMTPTests/ClientTests.swift b/Tests/XMTPTests/ClientTests.swift index 3d3442df..dfbf1ca8 100644 --- a/Tests/XMTPTests/ClientTests.swift +++ b/Tests/XMTPTests/ClientTests.swift @@ -460,5 +460,59 @@ class ClientTests: XCTestCase { installationId: alixInstallationId )) } + + func testCreatesADevClientPerformance() async throws { + let key = try Crypto.secureRandomBytes(count: 32) + let fakeWallet = try PrivateKey.generate() + + // Measure time to create the client + let start = Date() + let client = try await Client.create( + account: fakeWallet, + options: ClientOptions( + api: ClientOptions.Api(env: .dev, isSecure: true), + dbEncryptionKey: key + ) + ) + let end = Date() + let time1 = end.timeIntervalSince(start) + print("PERF: Created a client in \(time1)s") + + // Measure time to build a client + let start2 = Date() + let buildClient1 = try await Client.build( + address: fakeWallet.address, + options: ClientOptions( + api: ClientOptions.Api(env: .dev, isSecure: true), + dbEncryptionKey: key + ) + ) + let end2 = Date() + let time2 = end2.timeIntervalSince(start2) + print("PERF: Built a client in \(time2)s") + + // Measure time to build a client with an inboxId + let start3 = Date() + let buildClient2 = try await Client.build( + address: fakeWallet.address, + options: ClientOptions( + api: ClientOptions.Api(env: .dev, isSecure: true), + dbEncryptionKey: key + ), + inboxId: client.inboxID + ) + let end3 = Date() + let time3 = end3.timeIntervalSince(start3) + print("PERF: Built a client with inboxId in \(time3)s") + + // Assert performance comparisons + XCTAssertTrue(time2 < time1, "Building a client should be faster than creating one.") + XCTAssertTrue(time3 < time1, "Building a client with inboxId should be faster than creating one.") + XCTAssertTrue(time3 < time2, "Building a client with inboxId should be faster than building one without.") + + // Assert that inbox IDs match + XCTAssertEqual(client.inboxID, buildClient1.inboxID, "Inbox ID of the created client and first built client should match.") + XCTAssertEqual(client.inboxID, buildClient2.inboxID, "Inbox ID of the created client and second built client should match.") + } } diff --git a/XMTP.podspec b/XMTP.podspec index a573938a..665bda3e 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "XMTP" - spec.version = "3.0.12" + spec.version = "3.0.13" spec.summary = "XMTP SDK Cocoapod" spec.description = <<-DESC @@ -22,7 +22,7 @@ Pod::Spec.new do |spec| spec.dependency 'CSecp256k1', '~> 0.2' spec.dependency "Connect-Swift", "= 1.0.0" - spec.dependency 'LibXMTP', '= 3.0.8' + spec.dependency 'LibXMTP', '= 3.0.9' spec.dependency 'CryptoSwift', '= 1.8.3' spec.dependency 'SQLCipher', '= 4.5.7' diff --git a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 0e6dafca..b42daae7 100644 --- a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/xmtp/libxmtp-swift.git", "state" : { - "revision" : "a98ff96416c63dd8a0166a4db94df461668922ca", - "version" : "3.0.8" + "revision" : "082dcb8a270c549c7aa55c2cef59698eae389a49", + "version" : "3.0.9" } }, {