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

Cadence Profiler #178

Closed
2 tasks done
m-Peter opened this issue May 3, 2023 · 16 comments
Closed
2 tasks done

Cadence Profiler #178

m-Peter opened this issue May 3, 2023 · 16 comments

Comments

@m-Peter
Copy link

m-Peter commented May 3, 2023

Cadence Profiler for Smart Contracts / Scripts / Transactions

Grant category

  • Developer tools / services
  • Open source maintenance

Description

Cadence is the native programming language for developing smart contracts / scripts / transactions that run on the Flow Blockchain. The Flow Emulator displays profiling data in logs about scripts and transactions, including computationUsed and memoryEstimate (this one is currently being added by our team, onflow/flow-emulator#366). These logs, however, are not enough to drill down and get to the source of the bottleneck that is causing execution issues, due to high execution efforts.
Hence, I would like to implement a Cadence Profiler, to enable Flow builders to build more robust smart contracts.

Problem statement

When designing smart contracts, one important concern is their performance and ability to scale. In some cases, after a period of time, the smart contract and some of its APIs, might become unwieldy due to their growth in computation & memory usage. This means that developers will not be able to run certain scripts / transactions, as they will reach the maximum execution limit. One such case can be seen here:

Proposed solution

Make use of the existing code that calculates the computation & memory used

and expose these metrics in a readable & accessible format to developers, through the Flow Emulator / Flow CLI.

Impact

Developers will be able to understand why scripts & transactions need a certain amount of computation & memory to run, and in turn they would be able to optimize their contracts.

Milestones and funding

Milestone Deliverables Timeline Risks USD proposal
1 - Emulator API for ProfilingReport Create a new Emulator API to display a ProfilingReport including all scripts / transactions ~ 4 weeks - 7,500
2 - Detailed ProfilingReport for contracts Break down of computation/memory used by each statement on a contract ~ 8 weeks - 15,000
3 - Adoption Use the profiler on at least 5 contracts ~ 2 weeks - 1,000/contract
4 - Miscellaneous Add documentation and usage examples. Raise awareness in relevant channels and promote the practice of profiling through technical articles and blogs ~ 2 weeks - 1,000

Total Cost: 28,500 USD. Of course, I am open to discussion, regarding the cost and timeline 🙂 .

Team

Name Role Bio Contact
Ardit Marku Back-end Engineer A seasoned back-end engineer with a focus on problem solving and designing robust software systems. Over 8 years of experience building high-traffic search engines, marketplaces, exchanges and in-house analytics solutions. Led cross-functional teams to deliver both web and mobile apps, participating in all steps of the software development lifecycle. Open source lover and advocate. [email protected] m_peter
@m-Peter m-Peter changed the title Cadence Profiler for Smart Contracts / Scripts / Transactions Cadence Profiler May 3, 2023
@sideninja
Copy link

I really like this proposal. It will be super useful for understanding Cadence execution.

@turbolent
Copy link
Member

Thank you for opening this proposal @m-Peter!

We are currently in the process of publishing more developer grants for Cadence and related tooling, guided by importance and impact for the ecosystem, and are planning to get back to reviewing this proposal afterwards. Thank you for your patience

@m-Peter
Copy link
Author

m-Peter commented May 5, 2023

Thanks for the heads up @turbolent 🙇
Let me know if I can be of any help, regarding the upcoming developer grants for Cadence. I would like to tackle some of the important needs of the ecosystem, and make an impact 🙏

@m-Peter
Copy link
Author

m-Peter commented Jul 12, 2023

@franklywatson Since this onflow/flow-emulator#388 is gonna be solved soon, is there any positive sentiment from the Flow team for this grant to proceed?

@joshuahannan
Copy link
Member

I'm definitely in support of this. It would be super useful for me and many others in the ecosystem!

@joshuahannan
Copy link
Member

I just want to make sure this will also be able to estimate gas usage because that really is the metric that most developers care about because they need to know what to set their gas limit at

@m-Peter
Copy link
Author

m-Peter commented Jul 14, 2023

@joshuahannan That makes total sense, I will experiment locally, how to calculate the gas for scripts & transactions 🙏

@bluesign
Copy link

bluesign commented Jul 14, 2023

I think this computation tracking per statement is amazing feature too. If we can integrate with tools like code-coverage (like @m-Peter did before perfectly), it can make things a bit more predictable.

@m-Peter
Copy link
Author

m-Peter commented Jul 14, 2023

Yes, the end goal would to be get general execution metadata, including things like line hits, computation and memory usage. So instead of just coverage report that we have under the Emulator endpoint (http://localhost:8080/emulator/codeCoverage), we could have a unified report with execution metadata.

@sideninja
Copy link

Yes, the end goal would to be get general execution metadata, including things like line hits, computation and memory usage. So instead of just coverage report that we have under the Emulator endpoint (http://localhost:8080/emulator/codeCoverage), we could have a unified report with execution metadata.

This is awesome! I can't wait for this to get integrated into tools.

@Aliserag
Copy link
Collaborator

Aliserag commented Dec 6, 2024

Hey gang, hasn't been activity on this thread for over a year, wanted to check what the status on this was :)

@m-Peter
Copy link
Author

m-Peter commented Dec 6, 2024

Hey gang, hasn't been activity on this thread for over a year, wanted to check what the status on this was :)

This was actually completed to a good degree. It's bundled in flow-emulator, and can be used for gathering computation reports for scripts & transactions. Since then I've been quite busy with the EVM Gateway, and Flow EVM, so I had no capacity to make tutorials and use it in certain projects 😅

@turbolent
Copy link
Member

cc @j1010001 and @onflow/flow-smart-contracts for potential follow-up work (e.g. tutorial on how to optimize Cadence code)

@sisyphusSmiling
Copy link

This is an exciting feature! I'm wondering if you all have any thoughts about how to expose this in Flow CLI. For instance, it would be helpful to run a script or transaction and output a computation report (maybe behind a flag like --computation-report <OUTPUT_DIRECTORY>.

@m-Peter
Copy link
Author

m-Peter commented Dec 10, 2024

This is an exciting feature! I'm wondering if you all have any thoughts about how to expose this in Flow CLI. For instance, it would be helpful to run a script or transaction and output a computation report (maybe behind a flag like --computation-report <OUTPUT_DIRECTORY>.

@sisyphusSmiling That's a good idea, the computation reports are already available on an admin endpoint (http://localhost:8080/emulator/computationReport), where they are grouped by either script / transaction ID. We could also add some extra utility flags, for flow scripts execute & flow transactions send commands. It shouldn't be very hard to do so 🙏

@Aliserag
Copy link
Collaborator

Legend! Going to close this one down :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants