w3c / event-timing

A proposal for an Event Timing specification.
https://w3c.github.io/event-timing/
Other
44 stars 13 forks source link

How does Event Timing API relate to/overlap with Long Tasks API? #122

Closed mmocny closed 5 months ago

mmocny commented 2 years ago

The Long Tasks api overlaps with Event Timing in some ways, and folks have asked for increased clarity. Here's a start:

Similarities:

Differences:

I've likely missed a lot of differences. Net/net, there are long tasks which are reported for things outside events, and events reported even with tasks are not long... but it is also the case that some of the features already overlap and some more overlap may even be desired.

mmocny commented 1 year ago

A few more additions:

noamr commented 1 year ago

How would a really long frame have more than two tasks? you mean e.g. the compositor is blocking?

mmocny commented 1 year ago

Hypothetical example:

Of course, we have before covered that the Long Tasks API can group things in unexpected ways, so I wanted to run some experiments...


Now. You nerd-sniped me real bad with this question as I tried to create a test page to showcase what happens in practice.

What I'm finding is that the Long Task API is even odder than I expected, such that I may need to sync up with you to understand what the design intent really is. I have found that it sometimes merges tasks together across boundaries I was not expecting.

For example, my test page always schedules work in 50ms increments, only one 50ms task per platform entry point (i.e. 50 per input, 50 per animation frame callback, etc). I can easily end up with Long Frames (~1 second long), and tracing shows many independent tasks, as I expect. Yet, I still only get ~2 Long Tasks reported per frame (as you suggested).

Here is a trace to showcase what I mean. I will focus on the Frame which starts at ~9280ms in the timeline.

You can see that DevTools CrRendererMain nests "tasks" in different ways:

Screenshot 2023-02-03 at 1 36 14 PM

The LongTasks API seems to always "slice" that middle Task in half and report that. I always get 2x Long Tasks entries as I get those Tasks:

Screenshot 2023-02-03 at 2 57 21 PM

But I don't understand what concept that maps to...

Here's a closer look at the main thread activity...

First half:

Screenshot 2023-02-03 at 2 59 37 PM

Second half:

Screenshot 2023-02-03 at 3 00 10 PM

I'll upload the demo and we can chat, but I want to clean it up a bit more.

mmocny commented 1 year ago

So, seems I can regularly create 3-4 Long Tasks per frame outside of Interactions... perhaps because on Chrome we don't prioritize rendering work more often than every 100ms, or after input.

But I also finally recorded trace with 3 long tasks in a frame which had Input.

Screenshot 2023-02-03 at 9 12 20 PM

Link to Trace, you can find that frame @740ms

noamr commented 1 year ago

OK these are actually 1-2 tasks per frame, with 3-4 long scripts in a task. DevTools marks both of them as long (because why not?)

The UI events spec does not specify a relationship between UI events and the event loop. This leaves a lot of room to optimize and differentiate in implementations. Those differences are not web-observable.... except in the long tasks API in some scenarios.

If to follow the spec literally, the language is usually in the format of "A user agent MUST dispatch event FOO when some conditions are met" (see mousemove).

This is usually implemented as:

The above is spec-compliant but not spec-mandated.

So in essence, both in spec and in chromium, we could currently have a max of (1 task + update the rendering) per frame.

noamr commented 1 year ago

IMO he differences in the OP are not inherent to the nature of long tasks and event timing... A lot of them are API decisions that could be interchangeable between the two APIs. e.g. we could have event timing attribution or set the durationThreshold of long tasks if we wanted to. ... another difference is that event-timing is per-iframe and longtasks is only for the top level window. But it's again somewhat of an artificial difference.

The main difference between event timing and long tasks is that long tasks measure potential blockages and event timing rely on actual user behavior (UI events). An additional difference is the rare case where event timing would be long without a long task/frame, and also event timing deals with interactionId for INP.

Seems to me that apart from the above rare cases, event timing entries would be a subset of long task attribution (especially if we use long-frames rather than long-tasks) with additional information specific to UI events.


But the way I make sense of this is by looking at long tasks/frames and event-timing as two views into an overlapping dataset. One is focused on "am I hogging the main thread in a way that can delay animation frames or interaction feedback?" and the other one is "what's the journey a UI event has to go through from input until feedback?" The first one is a diagnostic and the second one more of a regression test. So creating separate observers for them also makes some sense. We should perhaps start viewing event types as different views into a timeline rather than separate "APIs".

noamr commented 1 year ago

To summarize thoughts:

Equivalent to how resource timing measures cause and paint timing measures effect.

Because of what we know about long tasks and events, long tasks are likely to be the root cause of long event timing. However, this is not a given and should not be assumed. There could be long tasks that don't create long events, and there could be long events not created by long tasks.

mmocny commented 1 year ago

Cause vs Effect is one way to summarize.

Event Timing also exposes metadata specific to events:

I don't know if those are fair to label as "effect" so much as "The event's contribution to cause, which mostly may have came from other things in the LoAF" or something like that.

clelland commented 1 year ago

@noamr, do you think there are spec changes to be made here to clarify this in Event TIming?

mmocny commented 5 months ago

I think we've documented a bunch of this, and LoAF has made lots of progress.

Here is one document: https://github.com/w3c/long-animation-frames?tab=readme-ov-file#overlap-with-event-timing