Skip to content
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

How to profile a unit test? #60179

Open
stephane-archer opened this issue Feb 20, 2025 · 4 comments
Open

How to profile a unit test? #60179

stephane-archer opened this issue Feb 20, 2025 · 4 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. type-question A question about expected behavior or functionality

Comments

@stephane-archer
Copy link

I have a function that takes more time than I would like to.
I call this function in many unit tests and I would like to improve the overall performance of the function.

How can I see what takes time for each unit test? (ideally a line-by-line analysis)

The CPU profiler seems to be accessible only when running the app.

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. type-question A question about expected behavior or functionality labels Feb 20, 2025
@bwilkerson
Copy link
Member

@kenzieschmoll

@a-siva
Copy link
Contributor

a-siva commented Feb 20, 2025

profiling an app is possible by using the timeline view in devtools and the CPU profiler views on a running app.
This article https://medium.com/dartlang/dart-devtools-analyzing-application-performance-with-the-cpu-profiler-3e94a0ec06ae does a deep dive on a performance analysis problem.

If you do not want to use these tools and instead want a line by line analysis of time spent, you could instrument your program with timers (start and end) for each line and run it.

@stephane-archer
Copy link
Author

So it's not available for unit test but only when running the full app right?

Can you mention what kind of timer can reliably count time between two lines? The interval would be small most of the time. (I think I don't need to be precise for small values)

@kenzieschmoll
Copy link
Contributor

So it's not available for unit test but only when running the full app right?

You can use DevTools against a test. You will just need to be sure the test does not exit upon completion.

dart test test/foo_test.dart --pause-after-load and then open the URL that is printed to console. Although you may hit another issue (flutter/devtools#8812) in this workflow that is causing the debugger to be automatically resumed. If you hit this, I recommend running the test from your IDE with multiple breakpoints at the end of the test. You'll need at least two because, due to the linked issue, the first breakpoint will be automatically resumed so if you don't have a second then the test will exit.

When you run from your IDE you will be able to open the DevTools CPU profiler and the timeline in the Performance page, as @a-siva mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

5 participants