Skip to content

Commit

Permalink
Client creation performance (#442)
Browse files Browse the repository at this point in the history
* performance testing for client creation

* bump the pod

* bump bindings
  • Loading branch information
nplasterer authored Dec 4, 2024
1 parent 6b6a461 commit ff181fb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 9 additions & 5 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
54 changes: 54 additions & 0 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

}
4 changes: 2 additions & 2 deletions XMTP.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down

0 comments on commit ff181fb

Please sign in to comment.