-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Moves compression before buffer (Optimization) #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d30d4cf
ExportDiffManager
abelonogov-ld 297ede9
visible working compression before buffering
abelonogov-ld 1f4d2c2
test script for iOS
abelonogov-ld ba94817
all tests fixed and 0 warnings
abelonogov-ld 4fcdf40
Enhance RRWebEventGenerator with keyframe handling and new test for c…
abelonogov-ld 7db0115
Enhance test script with improved options and usage documentation
abelonogov-ld 61a0aa0
rename file
abelonogov-ld af82a03
fix test script
abelonogov-ld 4e5497f
remove dead code
abelonogov-ld 038616c
benchmark files
abelonogov-ld 97a37e4
Enhance Benchmark functionality by adding execution time tracking and…
abelonogov-ld e62e2f2
adjust compression parameters
abelonogov-ld c0a4fe9
wip
abelonogov-ld 13798ea
wip
abelonogov-ld 9668c70
Merge branch 'main' into andrey/exportdiffmanager
abelonogov-ld 028123e
Refactor ExportFrame to use ImageSignature instead of TileSignature
abelonogov-ld a0e951a
expand unit tests
abelonogov-ld 8ea1c7b
do account disk reading into benchmark
abelonogov-ld 642bea6
Alert errors
abelonogov-ld ad4ddc3
separated time
abelonogov-ld 0fe4bdf
Merge branch 'main' into andrey/exportdiffmanager
abelonogov-ld 423dd7a
Clear node IDs before generating full snapshot data in RRWebEventGene…
abelonogov-ld 2f811e4
Merge branch 'andrey/exportdiffmanager' of github.com:launchdarkly/sw…
abelonogov-ld c67581b
Remove unused eventNode function from ExportFrame struct to streamlin…
abelonogov-ld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Sources/LaunchDarklySessionReplay/BenchMark/BenchmarkExecutor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #if canImport(UIKit) | ||
|
|
||
| import Foundation | ||
| import OSLog | ||
| import LaunchDarklyObservability | ||
|
|
||
| public final class BenchmarkExecutor { | ||
| public typealias CompressionResult = ( | ||
| compression: SessionReplayOptions.CompressionMethod, | ||
| bytes: Int, | ||
| captureTime: TimeInterval, | ||
| totalTime: TimeInterval | ||
| ) | ||
|
|
||
| private static let compressionMethods: [SessionReplayOptions.CompressionMethod] = [ | ||
| .screenImage, | ||
| .overlayTiles(layers: 15, backtracking: false), | ||
| .overlayTiles(layers: 15, backtracking: true), | ||
| ] | ||
|
|
||
| public init() {} | ||
|
|
||
| public func compression(framesDirectory: URL, runs: Int = 1) async throws -> [CompressionResult] { | ||
| let reader = try RawFrameReader(directory: framesDirectory) | ||
| let frames = Array(reader) | ||
|
|
||
| var results = [CompressionResult]() | ||
| let runCount = max(1, runs) | ||
|
|
||
| for method in Self.compressionMethods { | ||
| var bytes = 0 | ||
| var captureTime: TimeInterval = 0 | ||
| var totalTime: TimeInterval = 0 | ||
|
|
||
| for _ in 0..<runCount { | ||
| let runResult = await runCompression(method, frames: frames) | ||
| bytes = runResult.bytes | ||
| captureTime += runResult.captureTime | ||
| totalTime += runResult.totalTime | ||
| } | ||
|
|
||
| results.append((compression: method, bytes: bytes, captureTime: captureTime, totalTime: totalTime)) | ||
| } | ||
|
|
||
| return results | ||
| } | ||
|
|
||
| private func runCompression(_ method: SessionReplayOptions.CompressionMethod, frames: [RawFrame]) async -> (bytes: Int, captureTime: TimeInterval, totalTime: TimeInterval) { | ||
| let exportDiffManager = ExportDiffManager(compression: method, scale: 1.0) | ||
| let eventGenerator = RRWebEventGenerator(log: OSLog.default, title: "Benchmark", method: method) | ||
| let encoder = JSONEncoder() | ||
| var bytes = 0 | ||
| var captureTime: TimeInterval = 0 | ||
|
|
||
| let start = CFAbsoluteTimeGetCurrent() | ||
|
|
||
| for frame in frames { | ||
| let captureStart = CFAbsoluteTimeGetCurrent() | ||
| guard let exportFrame = exportDiffManager.exportFrame(from: frame, onTiledFrameComputed: { | ||
| captureTime += CFAbsoluteTimeGetCurrent() - captureStart | ||
| }) else { | ||
| continue | ||
| } | ||
|
|
||
| let item = EventQueueItem(payload: ImageItemPayload(exportFrame: exportFrame)) | ||
| let events = await eventGenerator.generateEvents(items: [item]) | ||
|
|
||
| if let data = try? encoder.encode(events) { | ||
| bytes += data.count | ||
| } | ||
| } | ||
|
|
||
| return (bytes: bytes, captureTime: captureTime, totalTime: CFAbsoluteTimeGetCurrent() - start) | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
166 changes: 166 additions & 0 deletions
166
Sources/LaunchDarklySessionReplay/BenchMark/RawFrameIO.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| #if canImport(UIKit) | ||
|
|
||
| import UIKit | ||
| import Foundation | ||
|
|
||
| final class RawFrameWriter { | ||
| let directory: URL | ||
| private var frameIndex: Int = 0 | ||
| private var imageIndex: Int = 0 | ||
| private var lastImageData: Data? | ||
| private let csvHandle: FileHandle | ||
|
|
||
| init() throws { | ||
| let dir = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent("RawFrames-\(UUID().uuidString)") | ||
| try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) | ||
| self.directory = dir | ||
| print("RawFrameWriter: directory = \(dir)") | ||
| let csvURL = dir.appendingPathComponent("frames.csv") | ||
| FileManager.default.createFile(atPath: csvURL.path, contents: nil) | ||
| self.csvHandle = try FileHandle(forWritingTo: csvURL) | ||
|
|
||
| let header = "frameIndex,imageIndex,timestamp,orientation,areas\n" | ||
| csvHandle.write(Data(header.utf8)) | ||
| } | ||
|
|
||
| deinit { | ||
| try? csvHandle.close() | ||
| } | ||
|
|
||
| func write(rawFrame: RawFrame) throws { | ||
| let index = frameIndex | ||
| frameIndex += 1 | ||
|
|
||
| guard let pngData = rawFrame.image.pngData() else { | ||
| throw CocoaError(.fileWriteUnknown) | ||
| } | ||
|
|
||
| let currentImageIndex: Int | ||
| if pngData == lastImageData { | ||
| currentImageIndex = imageIndex - 1 | ||
| } else { | ||
| currentImageIndex = imageIndex | ||
| let imageURL = directory.appendingPathComponent(String(format: "%06d.png", currentImageIndex)) | ||
| try pngData.write(to: imageURL) | ||
| lastImageData = pngData | ||
| imageIndex += 1 | ||
| } | ||
|
|
||
| let areasArray: [[String: [String: CGFloat]]] = rawFrame.areas.map { area in | ||
| [ | ||
| "rect": [ | ||
| "x": area.rect.origin.x, | ||
| "y": area.rect.origin.y, | ||
| "width": area.rect.size.width, | ||
| "height": area.rect.size.height | ||
| ], | ||
| "offset": [ | ||
| "x": area.offset.x, | ||
| "y": area.offset.y | ||
| ] | ||
| ] | ||
| } | ||
| let areasData = try JSONSerialization.data(withJSONObject: areasArray) | ||
| let areasJSON = String(data: areasData, encoding: .utf8) ?? "[]" | ||
| let escapedAreas = areasJSON.replacingOccurrences(of: "\"", with: "\"\"") | ||
|
|
||
| let row = "\(index),\(currentImageIndex),\(rawFrame.timestamp),\(rawFrame.orientation),\"\(escapedAreas)\"\n" | ||
| csvHandle.write(Data(row.utf8)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - RawFrameReader | ||
|
|
||
| final class RawFrameReader: Sequence { | ||
| private let directory: URL | ||
| private let rows: [String] | ||
|
|
||
| init(directory: URL) throws { | ||
| self.directory = directory | ||
| let csvURL = directory.appendingPathComponent("frames.csv") | ||
| let content = try String(contentsOf: csvURL, encoding: .utf8) | ||
| self.rows = content.components(separatedBy: "\n") | ||
| .dropFirst() | ||
| .filter { !$0.isEmpty } | ||
| } | ||
|
|
||
| func makeIterator() -> Iterator { | ||
| Iterator(directory: directory, rows: rows) | ||
| } | ||
|
|
||
| struct Iterator: IteratorProtocol { | ||
| private let directory: URL | ||
| private let rows: [String] | ||
| private var index = 0 | ||
| private var imageCache = [Int: UIImage]() | ||
|
|
||
| init(directory: URL, rows: [String]) { | ||
| self.directory = directory | ||
| self.rows = rows | ||
| } | ||
|
|
||
| mutating func next() -> RawFrame? { | ||
| guard index < rows.count else { return nil } | ||
| defer { index += 1 } | ||
| return parse(line: rows[index]) | ||
| } | ||
|
|
||
| private mutating func parse(line: String) -> RawFrame? { | ||
| let columns = Self.parseCSV(line: line) | ||
| guard columns.count >= 5, | ||
| let imageIndex = Int(columns[1]), | ||
| let timestamp = TimeInterval(columns[2]), | ||
| let orientation = Int(columns[3]) | ||
| else { return nil } | ||
|
|
||
| let image: UIImage | ||
| if let cached = imageCache[imageIndex] { | ||
| image = cached | ||
| } else { | ||
| let imageURL = directory.appendingPathComponent(String(format: "%06d.png", imageIndex)) | ||
| guard let loaded = UIImage(contentsOfFile: imageURL.path) else { return nil } | ||
| imageCache[imageIndex] = loaded | ||
| image = loaded | ||
| } | ||
|
|
||
| let areasJSON = columns[4].replacingOccurrences(of: "\"\"", with: "\"") | ||
| var areas = [OffsettedArea]() | ||
| if let data = areasJSON.data(using: .utf8), | ||
| let array = try? JSONSerialization.jsonObject(with: data) as? [[String: [String: CGFloat]]] { | ||
| for dict in array { | ||
| guard let r = dict["rect"], let o = dict["offset"], | ||
| let x = r["x"], let y = r["y"], let w = r["width"], let h = r["height"], | ||
| let ox = o["x"], let oy = o["y"] | ||
| else { continue } | ||
| areas.append(OffsettedArea( | ||
| rect: CGRect(x: x, y: y, width: w, height: h), | ||
| offset: CGPoint(x: ox, y: oy) | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| return RawFrame(image: image, timestamp: timestamp, orientation: orientation, areas: areas) | ||
| } | ||
|
|
||
| private static func parseCSV(line: String) -> [String] { | ||
| var result = [String]() | ||
| var current = "" | ||
| var inQuotes = false | ||
| for ch in line { | ||
| if ch == "\"" { | ||
| inQuotes.toggle() | ||
| } else if ch == "," && !inQuotes { | ||
| result.append(current) | ||
| current = "" | ||
| } else { | ||
| current.append(ch) | ||
| } | ||
| } | ||
| result.append(current) | ||
| return result | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.