-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Cost of contract calls snapshot tests #7440
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
58e204d
better tests to measure cost of contract calls
xunilrj 3fa070e
fix SubtsType for TyConstantDecl
xunilrj dbd26f6
bring whole snapshot test back
xunilrj 3fb4e31
fmt and clippy issues
xunilrj 100948d
fmt and clippy issues
xunilrj 3d8d19b
fix snapshot test harness
xunilrj c8bd7d3
removing todos
xunilrj 71e5ad6
fmt and clippy issues
xunilrj a127ada
update just file
xunilrj a763fb2
rename tests for better historic comparison
xunilrj 41c12b7
fix CI
xunilrj 9debe2f
fix typo
xunilrj bdd5b43
fixing CI
xunilrj 424a7bf
fixing CI
xunilrj d378dc4
fixing CI
xunilrj f7761d6
clippy and fmt
xunilrj f57dd52
fix parsed_decl_id
xunilrj 95b931d
fix parsed_decl_id
xunilrj cf28620
PR suggestions
xunilrj afc4711
update tests
xunilrj d614ca1
explains tyconstantdecl impl
xunilrj 3be5de3
Merge branch 'master' into xunilrj/cost-of-contract-calls
tritao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/bin/bash | ||
| echo '<!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Pivot Demo</title> | ||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> | ||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> | ||
|
|
||
| <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/pivottable/2.23.0/pivot.min.css"> | ||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pivottable/2.23.0/pivot.min.js"></script> | ||
|
|
||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | ||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pivottable/2.23.0/d3_renderers.min.js"></script> | ||
|
|
||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.min.js"></script> | ||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pivottable/2.23.0/c3_renderers.min.js"></script> | ||
|
|
||
| <script src="https://cdn.plot.ly/plotly-basic-latest.min.js"></script> | ||
| <script src="https://cdnjs.cloudflare.com/ajax/libs/pivottable/2.23.0/plotly_renderers.min.js"></script> | ||
|
|
||
| <style> | ||
| body {font-family: Verdana;} | ||
| .node { | ||
| border: solid 1px white; | ||
| font: 10px sans-serif; | ||
| line-height: 12px; | ||
| overflow: hidden; | ||
| position: absolute; | ||
| text-indent: 2px; | ||
| } | ||
| </style> | ||
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script> | ||
| </head> | ||
| <body> | ||
| <script type="text/javascript"> | ||
| $(function(){ | ||
| var renderers = $.extend( | ||
| $.pivotUtilities.renderers, | ||
| $.pivotUtilities.c3_renderers, | ||
| $.pivotUtilities.d3_renderers, | ||
| $.pivotUtilities.plotly_renderers | ||
| ); | ||
| $("#output").pivotUI($("table"), { | ||
| renderers, | ||
| rendererName: "Line Chart", | ||
| }); | ||
| }); | ||
| </script> | ||
| <div id="output" style="margin: 30px;"></div> | ||
| <br /> | ||
| <table> | ||
| <thead>' | ||
| head -n 1 "$1" | sed -e 's/^/<tr><th>/' -e 's/,/<\/th><th>/g' -e 's/$/<\/th><\/tr>/' | ||
| echo ' </thead> | ||
| <tbody>' | ||
| tail -n +2 "$1" | sed -e 's/^/<tr><td>/' -e 's/,/<\/td><td>/g' -e 's/$/<\/td><\/tr>/' | ||
| echo " </tbody> | ||
| </table> | ||
| <body> | ||
| </html>" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # Snapshot tests | ||
|
|
||
| There are two ways to run snapshot tests: | ||
|
|
||
| ``` | ||
| > cargo r -p test --release | ||
| > cargo r -p test --release -- -k snapshot | ||
| ``` | ||
|
|
||
| When the snapshot flag is enabled (the default) the test harness will search for `snapshot.toml` files. For every `toml` file found a new snapshot test will run. If the `toml` file is empty, it will be interpreted as simply being: | ||
|
|
||
| ```toml | ||
| # this is how the test harness understand when it sees an empty snapshot.toml | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cmds = [ | ||
| \"forc build --path {root}\" | ||
| ] | ||
| ``` | ||
|
|
||
| When the test harness runs a snapshot test, it will iterate the `cmds` array of each file, run each command, and append everything into the snapshot. | ||
|
|
||
| So the snapshot of the above file would be something like | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
| > forc test --path test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/const_of_contract_call | ||
| exit status: 0 | ||
| output: | ||
| Building test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/const_of_contract_call | ||
| Compiling library std (test/src/e2e_vm_tests/reduced_std_libs/sway-lib-std-core) | ||
| Compiling contract const_of_contract_call (test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/const_of_contract_call) | ||
| Finished release [optimized + fuel] target(s) [1.88 KB] in ??? | ||
| ``` | ||
|
|
||
| To make snapshot "environment free", the test harness changes `forc` output a little bit. | ||
|
|
||
| 1 - First it does not show complete file paths. All paths are relative to the swat repo root. | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 2 - Test harness also remove all printing of times. | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 3 - It also removes all ANSI codes for coloring and others. | ||
|
|
||
| ## Commands | ||
|
|
||
| To make snapshot tests more versatile there are a lot of options of what one can use inside `cmds`: | ||
|
|
||
| 1 - `forc`; Any native forc command is available; | ||
| 2 - Forc plugins; Currently only `forc doc` is available; | ||
| 3 - `echo`; One can use echo to write a message. Usage is: | ||
|
|
||
| ```toml | ||
| cmds = [ | ||
| "echo Explain something here.", | ||
| "forc build --path {root}", | ||
| ] | ||
| ``` | ||
|
|
||
| 4 - `sub`; Sub will iterate all lines of the previous command and filter in only those that contain its argument. | ||
|
|
||
| ```toml | ||
| cmds = [ | ||
| "forc build --path {root} --asm final | sub ecal" | ||
| ] | ||
| ``` | ||
|
|
||
| The example above will only show asm lines that contains the `ecal` instruction. | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 5 - `regex`; Regex is very similar to `sub`, but allows a regex to be written. | ||
|
|
||
| ```toml | ||
| cmds = [ | ||
| "forc build --path {root} --ir initial | regex ' (v0 = call call|v0 = const|v1 = const|revert)'" | ||
| ] | ||
| ``` | ||
|
|
||
| 6 - `filter-fn`. This command only shows IR, ASM for a specific function. It needs that previous command return a complete IR of a program. | ||
|
|
||
| ```toml | ||
| cmds = [ | ||
| "forc build --path {root} --ir final --asm final | filter-fn {name} transmute_by_reference_7", | ||
| ] | ||
| ``` | ||
|
|
||
| In the example above, the snapshot will only contain IR and asm from the function "transmute_by_reference_7" | ||
|
|
||
| Some commands manipulate files. These commands have an "undo" list that will restore the file original content when they a finished. | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| So it is guaranteed that if the test harness finishes gracefully, `main.sw` will have its original value. | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| For security reasons, these commands can ONLY manipulate files under its project folder. | ||
|
|
||
| 1 - `replace`. Replace a simple string for another. | ||
|
|
||
| ```toml | ||
| cmds = [ | ||
| "replace src/main.sw \"fn cost_of_in\" \"fn isolated_cost_of_in\"", | ||
| ] | ||
| ``` | ||
|
|
||
| ## Variables | ||
|
|
||
| 1 - `root`. Is the folder of the project being compiled. | ||
| 2 - `name`. Is the name of the specific project being compiled. | ||
|
|
||
| ## Blocks | ||
|
|
||
| Blocks are blocks of code that live inside two comments of the form below: | ||
|
|
||
| ```rust | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /* START BOOL */ | ||
| #[test] | ||
| fn cost_of_in_bool() { | ||
| let _ = abi(MyContract, CONTRACT_ID).in_bool(false); | ||
| } | ||
| /* END BOOL */ | ||
| ``` | ||
|
|
||
| These blocks can be manipulade from inside the snapshot.toml and allowing multiples tests to use the same project. | ||
xunilrj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| To manipulate these blocks one can: | ||
|
|
||
| 1 - Use the `repeat-for-each-block`. | ||
|
|
||
| ```toml | ||
| cmds = [ | ||
| { repeat = "for-each-block", cmds = [ | ||
| "forc test --path {root} --release --experimental const_generics" | ||
| ] } | ||
| ] | ||
| ``` | ||
|
|
||
| In the example above, the test harness will collect all "blocks" in the project being compiled, and will run the `cmds` inside the inner table for each block, removing all others. So for example: | ||
|
|
||
| ```rust | ||
| /* START BOOL */ | ||
| #[test] | ||
| fn cost_of_in_bool() { | ||
| let _ = abi(MyContract, CONTRACT_ID).in_bool(false); | ||
| } | ||
| /* END BOOL */ | ||
|
|
||
| /* START U8 */ | ||
| #[test] | ||
| fn cost_of_in_u8() { | ||
| let _ = abi(MyContract, CONTRACT_ID).in_u8(0); | ||
| } | ||
| /* END U8 */ | ||
| ``` | ||
|
|
||
| In the example above, the `repeat = "for-each-block"` means that its `cmds` list will be run twice. First removing the block `U8`; and after, it will restore the original file contents and remove "BOOL". | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.