Skip to content

Consolidate metrics code and linker parameter extraction.#101

Merged
johnno1962 merged 11 commits into
mainfrom
metrics
Oct 21, 2025
Merged

Consolidate metrics code and linker parameter extraction.#101
johnno1962 merged 11 commits into
mainfrom
metrics

Conversation

@johnno1962
Copy link
Copy Markdown
Owner

No description provided.

* Add injection timing metrics for hot reload performance tracking

This commit adds comprehensive timing metrics to InjectionNext that allow
client apps to track hot reload performance. The metrics include:
- Processing time: Time to find target and compilation command
- Compilation time: Time spent compiling the modified file
- Linking time: Time spent linking the dynamic library
- Total time: End-to-end injection time

Changes:
- Extended protocol with InjectionMetrics command type
- Added InjectionMetricsTracker class using Codable for JSON serialization
- Modified link() method to return linking duration
- Integrated metrics tracking throughout injection flow
- Added sendMetrics() method in NextCompiler using existing sendCommand
- Client receives metrics and posts configurable notification

The metrics include a notification_name field allowing extensibility without
protocol changes. Metrics are sent for both successful and failed injections.

* Use INJECTION_METRICS_NOTIFICATION constant from InjectionLite

Update InjectionLite submodule to include the notification constant
definition and use it instead of a hardcoded string.

Depends on: johnno1962/InjectionLite#18

---------

Co-authored-by: Karim Alweheshy <karim.alweheshy@reddit.com>
@johnno1962 johnno1962 force-pushed the metrics branch 2 times, most recently from 1a4c3b4 to a19f04e Compare September 30, 2025 20:25
@johnno1962 johnno1962 force-pushed the metrics branch 4 times, most recently from 591dcec to 4f287b5 Compare October 2, 2025 16:31
@johnno1962 johnno1962 requested a review from Copilot October 2, 2025 18:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +151 to +163
case .metrics:
guard let metricsJSON = readString() else {
return error("Unable to read metrics JSON")
}
if let data = metricsJSON.data(using: .utf8),
let metricsDict = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let notificationName = metricsDict["notification_name"] as? String {
NotificationCenter.default.post(
name: NSNotification.Name(notificationName),
object: nil,
userInfo: metricsDict
)
}
Copy link

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message 'Unable to read metrics JSON' should be more descriptive to help with debugging. Consider including context about what operation was being performed.

Copilot uses AI. Check for mistakes.
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
guard let jsonData = try? encoder.encode(metrics),
let jsonString = String(data: jsonData, encoding: .utf8) else {
Copy link

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent failure in JSON encoding could hide important errors. Consider logging when metrics encoding fails to aid in debugging.

Suggested change
let jsonString = String(data: jsonData, encoding: .utf8) else {
let jsonString = String(data: jsonData, encoding: .utf8) else {
log("Failed to encode metrics to JSON:", metrics)

Copilot uses AI. Check for mistakes.
let toolchain = xcodeDev+"/Toolchains/XcodeDefault.xctoolchain"
let frameworks = Bundle.main.privateFrameworksPath ?? "/tmp"
var testingOptions = ""
func link(object: String, dylib: String, arch: String) -> (String, Double)? {
Copy link

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return tuple (String, Double) is not self-documenting. Consider using a named tuple like (dylib: String, linkingTimeMs: Double) or a dedicated struct for better API clarity.

Copilot uses AI. Check for mistakes.
let compilationTimeMs = (now - compilationStartTime) * 1000
currentMetrics?.compilationTimeMs = compilationTimeMs
detail(String(format: "⚡ Compiled in %.0fms", compilationTimeMs))

Copy link

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern #\"([ $()])"# and replacement \"\\\\$1\" are not self-explanatory. Consider adding a comment explaining what characters are being escaped and why.

Suggested change
// Escape spaces, dollar signs, and parentheses in arguments to safely pass them to the shell.
// This prevents issues with shell interpretation of these special characters.

Copilot uses AI. Check for mistakes.
johnno1962 and others added 8 commits October 3, 2025 09:33
…ection metrics

- Add bazelTarget field to InjectionMetricsTracker
- Normalize sourcePath to workspace-relative before sending to client
- Discover Bazel app target using BazelAQueryParser
- Remove need for client-side enrichment logic

Server now sends fully enriched metrics with:
- sourcePath: workspace-relative path (e.g., 'Modules/Feature/File.swift')
- bazelTarget: discovered app target (e.g., '//Modules/Feature:FeatureSampleApp')
@johnno1962
Copy link
Copy Markdown
Owner Author

@karim-alweheshy, there was a couple of small fixes to review in InjectionLite in. the last commit johnno1962/InjectionLite@91abadd

@johnno1962 johnno1962 merged commit aadb551 into main Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants