1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import Darwin
import XCTest
import os
final class DeviceScrollProfile: XCTestCase {
func testScrollFeed() throws {
let environment = ProcessInfo.processInfo.environment
try XCTSkipUnless(environment["OLDFELLA_DEVICE_SCROLL_PROFILE"] == "1", "Run with just profile.")
let seconds = Double(environment["OLDFELLA_PROFILE_SECONDS"] ?? "40") ?? 40
let fixture = environment["OLDFELLA_PROFILE_FEED"] ?? "live"
let direction = environment["OLDFELLA_PROFILE_DIRECTION"] ?? "alternating"
continueAfterFailure = false
let app = XCUIApplication()
app.launchArguments = ["-scroll-profiling"]
if fixture == "rich" {
app.launchArguments += ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-rich-scroll"]
}
defer { app.terminate() }
app.launch()
XCTAssertTrue(
app.buttons["feedPickerButton"].waitForExistence(timeout: 45), "Sign in before profiling the live feed."
)
let list = preparedFeed(in: app)
let notification = try XCTUnwrap(environment["OLDFELLA_PROFILE_START_NOTIFICATION"])
waitForRecorder(notification: notification)
recordScroll(app: app, list: list, seconds: seconds, fixture: fixture, direction: direction)
waitForRecorder(notification: notification + ".finished", marker: "OLDFELLA_PROFILE_FINISHED", timeout: 240)
}
private func recordScroll(
app: XCUIApplication, list: XCUIElement, seconds: Double, fixture: String, direction: String
) {
let log = OSLog(subsystem: "com.oldfella.profiling", category: .pointsOfInterest)
let signpostID = OSSignpostID(log: log)
var gestures = 0
var pageRequests = 0
let start = ProcessInfo.processInfo.systemUptime
print("OLDFELLA_SCROLL_BEGIN feed=\(fixture) seconds=\(seconds) direction=\(direction)")
os_signpost(.begin, log: log, name: "OldFellaScrollProfile", signpostID: signpostID)
repeat {
if direction == "manual" {
Thread.sleep(forTimeInterval: 0.1)
continue
}
if direction == "forward" || gestures % 8 < 4 {
list.swipeUp(velocity: .fast)
} else {
list.swipeDown(velocity: .fast)
}
gestures += 1
if direction == "forward" {
let more = app.buttons["loadMorePostsButton"].firstMatch
if more.exists && more.isHittable && more.isEnabled {
os_signpost(.event, log: log, name: "OldFellaLoadMorePosts")
more.tap()
pageRequests += 1
print("OLDFELLA_PROFILE_LOAD_MORE count=\(pageRequests)")
}
}
} while ProcessInfo.processInfo.systemUptime - start < seconds
os_signpost(.end, log: log, name: "OldFellaScrollProfile", signpostID: signpostID)
let elapsed = ProcessInfo.processInfo.systemUptime - start
print("OLDFELLA_SCROLL_END gestures=\(gestures) pageRequests=\(pageRequests) elapsed=\(elapsed)")
}
private func preparedFeed(in app: XCUIApplication) -> XCUIElement {
let list = app.collectionViews.firstMatch
XCTAssertTrue(list.waitForExistence(timeout: 45), "The feed must load before scrolling starts.")
let firstPost = list.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@", "postText-")).firstMatch
XCTAssertTrue(firstPost.waitForExistence(timeout: 10), "The feed needs posts for a scrolling measurement.")
let initialPost = app.buttons[firstPost.identifier]
let initialY = initialPost.frame.minY
list.swipeUp(velocity: .fast)
XCTAssertTrue(
!initialPost.exists || initialPost.frame.minY < initialY - 10,
"The first swipe must move the feed."
)
for _ in 0..<2 { list.swipeUp(velocity: .fast) }
return list
}
private func waitForRecorder(
notification: String, marker: String = "OLDFELLA_PROFILE_READY", timeout: TimeInterval = 90
) {
let ready = expectation(description: "The recorder is ready")
ready.assertForOverFulfill = false
let center = CFNotificationCenterGetDarwinNotifyCenter()
let observer = Unmanaged.passUnretained(ready).toOpaque()
CFNotificationCenterAddObserver(
center, observer,
{ _, observer, _, _, _ in
guard let observer else { return }
Unmanaged<XCTestExpectation>.fromOpaque(observer).takeUnretainedValue().fulfill()
}, notification as CFString, nil, .deliverImmediately)
defer {
CFNotificationCenterRemoveObserver(center, observer, CFNotificationName(notification as CFString), nil)
}
print(marker)
fflush(stdout)
wait(for: [ready], timeout: timeout)
}
}