Files
bitchat/bitchatTests/Performance/PerformanceBaselineTests+HotPaths.swift
jackandjack 46c1b6caa0 Architecture, @Observable migration, hot-path performance, and live-device transport hardening (2.0.0 build 4) (#1969)
* Bump targets to iOS 17/macOS 14 and optimize PeerList/Nostr hot paths

* Migrate all 11 AppEnvironmentModels feature models to SwiftUI @Observable

* Inject AppRuntimeDependencies and decompose ChatViewModel into domain extensions

* Decompose Nostr protocol/relay policies and chat coordinators

* Harden strict concurrency and dead-code hygiene across core packages and app

* Clear remaining iOS 17 deprecation warnings in BitchatApp and VoiceRecorder

* Fix BLE redundant-link thrash, Tor -1011 bans, EOSE promotion, and duplicate startup subs

* Cancel stale foreground Tor reconnect task across background cycles

* Bump 2.0.0 build number to 4

* Address SwiftLint, Periphery, and Codex review findings

* Preserve @Observable dependencies for group details, geohash counts, and live voice availability

* Refresh inbox rows when geohash participant display names update

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2026-11-28 20:28:35 -10:00

152 lines
5.8 KiB
Swift

//
// PerformanceBaselineTests+HotPaths.swift
// bitchatTests
//
// Performance baselines added for 2.0.1 hot paths:
// - PeerListModel inbox rebuild at scale (50 favorites x 50 direct chats):
// previously O(F * C) scanning every conversation per favorite + hashing
// SHA-256 fingerprints + hitting UserDefaults on every refresh.
// - InboundFrameRouter off-main dedup & multi-relay fan-in:
// drops duplicate fan-in completely off MainActor with O(|subKeys|) cleanup.
//
import XCTest
import BitFoundation
@testable import bitchat
extension PerformanceBaselineTests {
// MARK: - PeerListModel inbox rebuild at scale
func testPeerListInboxRefreshAtScale() {
let favoriteCount = 50
let directChatCount = 50
let keychain = MockKeychain()
let suiteName = "PerformanceBaselineTests.inbox.\(UUID().uuidString)"
let storage = UserDefaults(suiteName: suiteName) ?? .standard
storage.removePersistentDomain(forName: suiteName)
defer { storage.removePersistentDomain(forName: suiteName) }
let conversations = ConversationStore()
let locationManager = LocationChannelManager(storage: storage)
let viewModel = ChatViewModel(
keychain: keychain,
idBridge: NostrIdentityBridge(keychain: MockKeychainHelper()),
identityManager: MockIdentityManager(keychain),
transport: MockTransport(),
conversations: conversations,
locationManager: locationManager
)
let baseDate = Date(timeIntervalSince1970: 1_700_000_000)
var favorites: [Data: FavoritesPersistenceService.FavoriteRelationship] = [:]
favorites.reserveCapacity(favoriteCount)
for index in 0..<favoriteCount {
var keyBytes = [UInt8](repeating: 0, count: 32)
keyBytes[0] = UInt8((index >> 8) & 0xFF)
keyBytes[1] = UInt8(index & 0xFF)
keyBytes[31] = 0xAA
let noiseKey = Data(keyBytes)
favorites[noiseKey] = FavoritesPersistenceService.FavoriteRelationship(
peerNoisePublicKey: noiseKey,
peerNostrPublicKey: nil,
peerNickname: "friend-\(index)",
isFavorite: true,
theyFavoritedUs: true,
favoritedAt: baseDate,
lastUpdated: baseDate.addingTimeInterval(Double(index))
)
if index < directChatCount {
let peerID = PeerID(hexData: noiseKey)
for msgIndex in 0..<5 {
conversations.append(
BitchatMessage(
id: "inbox-perf-\(index)-\(msgIndex)",
sender: "friend-\(index)",
content: "hello from conversation \(index) message \(msgIndex)",
timestamp: baseDate.addingTimeInterval(Double(index * 10 + msgIndex)),
isRelay: false,
isPrivate: true,
senderPeerID: peerID
),
to: .directPeer(peerID)
)
}
}
}
let model = PeerListModel(
chatViewModel: viewModel,
conversations: conversations,
locationChannelsModel: LocationChannelsModel(manager: locationManager),
favoritesProvider: { favorites }
)
XCTAssertEqual(model.inboxRows.count, favoriteCount)
let refreshesPerPass = 20
var samples: [TimeInterval] = []
measure {
let start = Date()
for _ in 0..<refreshesPerPass {
model.refreshNow()
}
samples.append(Date().timeIntervalSince(start))
}
XCTAssertEqual(model.inboxRows.count, favoriteCount)
reportThroughput("peerList.inboxRefresh", samples: samples, operations: refreshesPerPass, unit: "refreshes")
}
// MARK: - Nostr off-main multi-relay inbound dedup fan-in
func testNostrOffMainInboundDedupFanIn() {
let router = InboundFrameRouter()
let relayURLs = (0..<5).map { "wss://relay-\($0).example" }
let eventIDs = (0..<500).map { String(format: "%064x", 0xD000_0000 + $0) }
let totalFrames = relayURLs.count * eventIDs.count
var samples: [TimeInterval] = []
measure {
router.resetInboundTracking(clearReceivedCounts: true)
let start = Date()
var recorded = 0
var dropped = 0
for eventID in eventIDs {
for relayURL in relayURLs {
switch router.precheckInboundEvent(
subscriptionID: "geo-perf",
eventID: eventID,
relayUrl: relayURL
) {
case .proceed(let generation):
switch router.recordVerifiedInboundEvent(
subscriptionID: "geo-perf",
eventID: eventID,
expectedGeneration: generation
) {
case .recorded:
recorded += 1
case .duplicate:
dropped += 1
case .staleSubscription:
break
}
case .duplicate:
dropped += 1
}
}
}
router.removeRecentInboundEvents(forSubscriptionID: "geo-perf")
samples.append(Date().timeIntervalSince(start))
XCTAssertEqual(recorded, eventIDs.count)
XCTAssertEqual(dropped, totalFrames - eventIDs.count)
}
reportThroughput("nostrRelay.offMainDedup", samples: samples, operations: totalFrames, unit: "frames")
}
}