Skip to content

Commit

Permalink
performance testing for client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Dec 3, 2024
1 parent 9acece3 commit b0292af
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
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.")
}

}

0 comments on commit b0292af

Please sign in to comment.