Skip to content

Add docs for JS self-profiling API#37796

Merged
wbamberg merged 6 commits intomdn:mainfrom
wbamberg:js-self-profiling-api
Mar 21, 2025
Merged

Add docs for JS self-profiling API#37796
wbamberg merged 6 commits intomdn:mainfrom
wbamberg:js-self-profiling-api

Conversation

@wbamberg
Copy link
Copy Markdown
Collaborator

@wbamberg wbamberg commented Jan 24, 2025

Docs for the JS Self-Profiling API.

  • I've included a page for samplebufferfull which is mentioned in the spec but not defined, and which doesn't have a BCD entry yet.
  • in my experiments I often see samples which only have a timestamp and no stack ID. I believe that these are when the browser is doing some other work that isn't directly executing the program, like GC. Is that right? I should probably say something about this.
  • this API is protected using Document Policy which AFAIK is not documented on MDN. I haven't done that as part of this PR but we should. I think there is an issue somewhere to document it but I can't find it in mdn/content.

@wbamberg wbamberg requested review from a team as code owners January 24, 2025 19:10
@wbamberg wbamberg requested review from bsmth and sideshowbarker and removed request for a team January 24, 2025 19:10
@github-actions github-actions bot added the Content:WebAPI Web API docs label Jan 24, 2025
@github-actions github-actions bot added the size/m [PR only] 51-500 LoC changed label Jan 24, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 24, 2025

Preview URLs (6 pages)
Flaws (2)

Note! 5 documents with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/API/JS_Self-Profiling_API
Title: JS Self-Profiling API
Flaw count: 2

  • broken_links:
    • /en-US/docs/Glossary/call_stack is ill cased
  • macros:
    • Macro produces link /en-US/docs/Glossary/call_stack which is a redirect
External URLs (2)

URL: /en-US/docs/Web/API/Profiler/Profiler
Title: Profiler: Profiler() constructor


URL: /en-US/docs/Web/API/JS_Self-Profiling_API
Title: JS Self-Profiling API

(comment last updated: 2025-02-28 22:05:22)

@sideshowbarker sideshowbarker removed their request for review January 25, 2025 12:14
Copy link
Copy Markdown
Contributor

@captainbrosset captainbrosset left a comment

Choose a reason for hiding this comment

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

Looks great to me from a tech writing perspective. Thanks Will.
I'll ping someone from my side to take a look at the technical details too.

@@ -0,0 +1,49 @@
---
title: "Profiler: samplebufferfull event"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is indeed mentioned briefly in the spec, and is implemented in chromium (it seems).
I think this should be spec'd properly, and then added to BCD.

Co-authored-by: Patrick Brosset <patrickbrosset@gmail.com>
@skyclouds2001
Copy link
Copy Markdown
Contributor

see mdn/mdn#507

@wbamberg
Copy link
Copy Markdown
Collaborator Author

@captainbrosset , did you find anyone who could take a look at this?

@captainbrosset
Copy link
Copy Markdown
Contributor

@captainbrosset , did you find anyone who could take a look at this?

@wbamberg I will ask again. Thanks for the ping.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

small correction - the stack frame has resource id which points to an index in the resources array.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

What I'm trying to talk about here is the logical structure of a profile, not the data structure of the object returned by the API. The intention in this page is to talk about the (1) the logical structure, (2) the data structure, and (3) how to map from (2) to (1).


Each stack frame contains information about the location of its corresponding function in the program:

- The URL of the script
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

similar to the other comment - this is not the URL but an index to the URL of the script

@nhelfman
Copy link
Copy Markdown

Let me just say that this is great you are doing this!

I'm wondering if best practices for using the Profiler should also be included to help users avoid shooting themselves in the foot.

For example, it may be worth mentioning that there might be some overhead in collecting profile and processing trace data and developers should be measure performance impact carefully.

Some potential strategies to address that:

  1. Increase sampling interval - this will produce less samples but will have lower impact.
  2. Collect profiles for short periods in a sampled manner. E.g. trace for 5 seconds every 60 seconds.
  3. Process the samples in a web worker to avoid impacting performance on the main thread.
  4. The Profiler has a potential to generate huge amount of data which will be hard to send to a central place for processing. It is possible to aggregate samples on the client before sending them to analytics/telemetry system.

Caveat - since in production systems the JS is typically minified - the captured traces will include minified function names which can be hard to reason about. Some ammonification logic has to happen wither on the client before sending or on the analytics system to un-minify the traces based on source maps.

@wbamberg
Copy link
Copy Markdown
Collaborator Author

wbamberg commented Feb 26, 2025

I'm wondering if best practices for using the Profiler should also be included to help users avoid shooting themselves in the foot.

Yes, this makes a lot of sense! I will write something up based on your comment. Thanks for taking a look.

@wbamberg
Copy link
Copy Markdown
Collaborator Author

Thanks for the review @nhelfman ! I added a bit on "best practices". I didn't change the bit about the stack frame containing the resource URL because that part is intended to describe the abstract/logical structure of a trace, not the implementation. If there's anything I can do to make that clearer I'm happy to have a go at it.

@nhelfman
Copy link
Copy Markdown

Thanks for the review @nhelfman ! I added a bit on "best practices". I didn't change the bit about the stack frame containing the resource URL because that part is intended to describe the abstract/logical structure of a trace, not the implementation. If there's anything I can do to make that clearer I'm happy to have a go at it.

Maybe indicate that this is an abstracted logical illustration so it will be clearer to the reader.

@wbamberg
Copy link
Copy Markdown
Collaborator Author

OK, I added some bits in dbef9f2 and 4236c92 to try to clarify that this section is not a description of the object format, and expanded on the preamble to try to describe the overall purpose of this page better (which is basically how to interpret the object returned, so you can reason about it).

@bsmth bsmth removed their request for review March 10, 2025 15:35
@wbamberg
Copy link
Copy Markdown
Collaborator Author

I'm merging this, it has an r+ and has been around for a while.

@wbamberg wbamberg merged commit 49f3683 into mdn:main Mar 21, 2025
9 checks passed
wbamberg added a commit to wbamberg/content that referenced this pull request Apr 4, 2025
* upstream/main: (172 commits)
  chore: improve code style guide (mdn#38715)
  fix: typo on `Error.isError()` page (mdn#38754)
  plural consistency (mdn#38747)
  fix: auto-cleanup by bot (mdn#38695)
  Synchronize with BCD v5.7.4 (mdn#38709)
  Add docs for JS self-profiling API (mdn#37796)
  Better SameSite docs (mdn#38710)
  Added missing explanation for Array Literals (mdn#38745)
  Add a page on CSRF (mdn#38151)
  Fix description of several Range methods (mdn#38518)
  Remove extraneous span (mdn#38696)
  Add a definition for media containers, improve how the media files are defined and Remove wrong information (mdn#38721)
  Move visited selector guide to CSS selectors module (mdn#38642)
  Make JSON learning article more technically precise (mdn#38644)
  Make translate3d() interactive example code valid (mdn#38647)
  Clarity on Safari support for custom elements (mdn#38727)
  feat(css): Link to learning doc about text direction (mdn#38719)
  Fix typo (mdn#38739)
  move guide to module: inline formatting context (mdn#38637)
  Fix CSS pseudo-class lists (mdn#38576)
  ...
cssinate pushed a commit to cssinate/content that referenced this pull request Apr 11, 2025
* Add docs for JS self-profiling API

* Apply suggestions from code review

Co-authored-by: Patrick Brosset <patrickbrosset@gmail.com>

* Review feedback

* Clarify that 'anatomy of a profile' doesn't describe object format

* More clarification

---------

Co-authored-by: Patrick Brosset <patrickbrosset@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants