Skip to content

Conversation

philipphofmann
Copy link
Member

@philipphofmann philipphofmann commented Sep 30, 2024

📜 Description

Serializing and capturing trace based profiles can run on the main thread and block it for a couple of milliseconds. Therefore, we move this to a background thread to avoid potentially blocking the main thread.

💡 Motivation and Context

This came up while investigating profiles for potentially slowing down UIViewControllers.

💚 How did you test it?

Unit test and checking if the profile is still correctly attached to UIViewController transactions with our iOS-Swift sample app.

📝 Checklist

You have to check all boxes before merging:

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Serializing and capturing a profile can run on the main thread and block
it for a couple of milliseconds. Therefore, we move this to a background
thread to avoid potentially blocking the main thread.
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

Attention: Patch coverage is 89.74359% with 4 lines in your changes missing coverage. Please review.

Project coverage is 91.299%. Comparing base (9b97372) to head (cbb160f).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...y/Profiling/SentryCaptureTransactionWithProfile.mm 78.947% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #4377       +/-   ##
=============================================
+ Coverage   91.294%   91.299%   +0.005%     
=============================================
  Files          609       610        +1     
  Lines        49519     49539       +20     
  Branches     17822     17841       +19     
=============================================
+ Hits         45208     45229       +21     
+ Misses        4218      4217        -1     
  Partials        93        93               
Files with missing lines Coverage Δ
...es/Sentry/Profiling/SentryProfilerSerialization.mm 91.472% <100.000%> (+0.184%) ⬆️
Sources/Sentry/SentryTracer.m 96.912% <100.000%> (+0.167%) ⬆️
...SentryProfilerTests/SentryProfileTestFixture.swift 99.640% <100.000%> (ø)
...SentryProfilerTests/SentryTraceProfilerTests.swift 98.871% <100.000%> (+0.039%) ⬆️
...y/Profiling/SentryCaptureTransactionWithProfile.mm 78.947% <78.947%> (ø)

... and 3 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b97372...cbb160f. Read the comment docs.

Copy link
Contributor

github-actions bot commented Sep 30, 2024

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1228.04 ms 1237.51 ms 9.47 ms
Size 21.58 KiB 704.20 KiB 682.62 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
25d925a 1232.89 ms 1248.41 ms 15.52 ms
2911760 1217.84 ms 1232.48 ms 14.64 ms
e998fd0 1241.49 ms 1262.63 ms 21.14 ms
39b1c35 1235.90 ms 1257.37 ms 21.47 ms
236d8a8 1232.33 ms 1255.55 ms 23.22 ms
d8eb419 1221.91 ms 1253.62 ms 31.71 ms
5d6ce0e 1206.72 ms 1228.67 ms 21.95 ms
e02eb74 1221.81 ms 1258.58 ms 36.77 ms
1bbcb9c 1214.25 ms 1230.04 ms 15.79 ms
649d940 1227.06 ms 1245.19 ms 18.13 ms

App size

Revision Plain With Sentry Diff
25d925a 21.58 KiB 418.82 KiB 397.24 KiB
2911760 21.58 KiB 697.69 KiB 676.11 KiB
e998fd0 21.58 KiB 414.59 KiB 393.01 KiB
39b1c35 22.85 KiB 408.88 KiB 386.03 KiB
236d8a8 21.58 KiB 418.70 KiB 397.12 KiB
d8eb419 22.85 KiB 408.87 KiB 386.02 KiB
5d6ce0e 22.85 KiB 405.38 KiB 382.53 KiB
e02eb74 21.58 KiB 713.54 KiB 691.95 KiB
1bbcb9c 20.76 KiB 426.10 KiB 405.34 KiB
649d940 21.58 KiB 695.36 KiB 673.78 KiB

Copy link
Contributor

@brustolin brustolin left a comment

Choose a reason for hiding this comment

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

Thanks for this. There is one thing to discuss.

philipphofmann and others added 2 commits September 30, 2024 11:37
Co-authored-by: Dhiogo Brustolin <dhiogo.brustolin@sentry.io>
Copy link
Contributor

@brustolin brustolin left a comment

Choose a reason for hiding this comment

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

LGTM!

@philipphofmann
Copy link
Member Author

I'm still waiting for @armcknight to approve this so we can avoid shooting ourselves in the foot.

Copy link
Member

@armcknight armcknight left a comment

Choose a reason for hiding this comment

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

Could you be more specific about how you tested this? I just tried looking at a manual transaction with trace-based profiling on main vs this branch and the profiles look different. I can't get a launch profile to show up when running from this branch.

I tried comparing the differences between main and this branch with continuous profiling but I'm not even seeing what I expect on main so i need to ask the team what's going on there.

At a minimum for anything profiling right now you have to check what it looks like on main against your changes for:

  • trace-based
    • regular transaction
    • launch
  • continuous
    • regular transaction
    • launch

Trace-based regular txn on main (https://sentry-sdks.sentry.io/profiling/profile/sentry-cocoa/cdc0737ef92245a0b14dcdb40c5d68c3/flamegraph/?colorCoding=by%20system%20vs%20application%20frame&fov=0%2C0%2C3822472960%2C19&query=&sorting=call%20order&tid=259&view=top%20down):

image

and on this branch (https://sentry-sdks.sentry.io/profiling/profile/sentry-cocoa/28b4f9cc46ea4c2aa94cdfc400d11497/flamegraph/?colorCoding=by%20system%20vs%20application%20frame&fov=0%2C0%2C5127519744%2C19&query=&sorting=call%20order&tid=259&view=top%20down):

image

@philipphofmann
Copy link
Member Author

Hmm, weird, I didn't test it in great detail as I didn't expect that when all the tests are green, such a small change can have such a big impact. @armcknight, do you have a clue about what could cause this? I will look into this tomorrow.

@armcknight
Copy link
Member

@philipphofmann my guess is that you aren't dispatching the right call to the bg queue, and so it captures a set of profiling data much earlier than it marks the end of the transaction. You may need to look for a more granular point in that process, or create one by refactoring.

@philipphofmann
Copy link
Member Author

philipphofmann commented Oct 2, 2024

I found the problem. This PR only impacts trace based profiles, as only the SentryTracer uses sentry_traceProfileEnvelopeItem. Continuous profiling doesn't use this method.

I tested this for manual transactions, UIViewController transactions and app start transactions.

Main Branch This feature Branch With copy inside async
Manual Transaction CleanShot 2024-10-02 at 09 54 05@2x Profile in Sentry CleanShot 2024-10-02 at 09 55 03@2x Profile in Sentry CleanShot 2024-10-09 at 10 34 10@2x Profile in Sentry
TransactionsViewController CleanShot 2024-10-02 at 09 56 06@2x Profile in Sentry CleanShot 2024-10-02 at 09 56 32@2x Profile in Sentry CleanShot 2024-10-09 at 10 39 03@2xProfile in Sentry
App Start Transaction CleanShot 2024-10-02 at 09 58 00@2x Profile in Sentry CleanShot 2024-10-02 at 09 58 56@2x Profile in Sentry CleanShot 2024-10-09 at 10 41 53@2x Profile in Sentry

Copy link
Member

@armcknight armcknight left a comment

Choose a reason for hiding this comment

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

Really nice test report! ✨ Looking at the actual profile durations in them, it seems the same, and it's the transaction that is actually more variable. I think that variability is not due to this change but is coming from preexisting behavior in the SDK so i'm not worried about it.

A comment on the tests you added, but otherwise things look good here, so when you get that ironed out, feel free to ship it.

Also regarding my earlier comment on continuous profiling, it looks like there's some delay with data coming in, being aggregated, or becoming accessible.

If and when you get around to bg'ing the continuous serialization function (maybe in a separate PR?), test it by looking at https://sentry-sdks.sentry.io/profiling/?project=5428557&statsPeriod=1m&tab=flamegraph:

image

@philipphofmann philipphofmann changed the title impr: Capture profiles on a BG Thread impr: Capture trace based profiles on a BG Thread Oct 8, 2024
@philipphofmann
Copy link
Member Author

If and when you get around to bg'ing the continuous serialization function (maybe in a separate PR?)

Ups, sorry, that's not the goal of this PR. I only want to capture trace-based profiles on a BG thread. I updated the description and title.

@philipphofmann philipphofmann merged commit 0418702 into main Oct 9, 2024
62 of 65 checks passed
@philipphofmann philipphofmann deleted the impr/capture-profile-on-bg-thread branch October 9, 2024 09:52
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