Skip to content

Commit

Permalink
Create sanity benchmark checks for all the arithmetic op codes (#1419)
Browse files Browse the repository at this point in the history
FuelLabs/fuel-core#1386

This PR adds benchmark tests for all the arithmetic op codes. The actual
purpose being running those tests to see if the costs actually line up
for all the codes.

These tests work by starting a vm with a set amount of gas, and running
an "infinite" loop of each op code until the vm runs out of gas. This is
timed and the time is reported.

After running these, I've found a number of divergent op codes that
could possibly require updated gas costs:
```
block target estimation/wddv opcode
                        time:   [4.0950 ms 4.1181 ms 4.1411 ms]
block target estimation/wdmd opcode
                        time:   [3.3510 ms 3.3710 ms 3.3931 ms]
block target estimation/wdam opcode
                        time:   [3.4912 ms 3.5138 ms 3.5363 ms]
block target estimation/wdmm opcode
                        time:   [3.4505 ms 3.4686 ms 3.4868 ms]
block target estimation/wqdv opcode
                        time:   [3.2404 ms 3.2590 ms 3.2796 ms]
block target estimation/wqmd opcode
                        time:   [4.1508 ms 4.1716 ms 4.1928 ms]
block target estimation/wqam opcode
                        time:   [3.6933 ms 3.7081 ms 3.7230 ms]
block target estimation/wqmm opcode
                        time:   [5.2120 ms 5.2395 ms 5.2667 ms]
```
So, that's `wddv`, `wdmd`, `wdam`, `wdmm`, `wqdv`, `wqmd`, `wqam`,
`wqmm`.

Whereas all the other op codes take close to 2ms to run out of gas,
implying that these divergent op codes could be priced higher. These
"wide" op codes all share similar setup, and not all of the "wide" op
codes fail the test, so it's _probably_ not the setup that is causing
these specific op code benchmarks to fail.

***

Here are all of the results:
```
lock target estimation/add opcode
                        time:   [2.1024 ms 2.1142 ms 2.1259 ms]
block target estimation/addi opcode
                        time:   [2.1066 ms 2.1208 ms 2.1369 ms]
block target estimation/aloc opcode
                        time:   [1.8966 ms 1.9104 ms 1.9252 ms]
block target estimation/and opcode
                        time:   [2.0765 ms 2.0877 ms 2.0990 ms]
block target estimation/andi opcode
                        time:   [2.1647 ms 2.1797 ms 2.1963 ms]
block target estimation/div opcode
                        time:   [2.1176 ms 2.1303 ms 2.1440 ms]
block target estimation/divi opcode
                        time:   [2.1445 ms 2.1572 ms 2.1711 ms]
block target estimation/eq opcode
                        time:   [2.0585 ms 2.0721 ms 2.0859 ms]
block target estimation/exp opcode
                        time:   [2.0829 ms 2.0931 ms 2.1031 ms]
block target estimation/expi opcode
                        time:   [2.1114 ms 2.1218 ms 2.1323 ms]
block target estimation/gt opcode
                        time:   [2.0068 ms 2.0176 ms 2.0287 ms]
block target estimation/gtf opcode
                        time:   [2.2672 ms 2.2765 ms 2.2859 ms]
block target estimation/lt opcode
                        time:   [1.9848 ms 1.9959 ms 2.0070 ms]
block target estimation/mlog opcode
                        time:   [2.0176 ms 2.0278 ms 2.0380 ms]
block target estimation/mod opcode
                        time:   [1.9694 ms 1.9819 ms 1.9943 ms]
block target estimation/modi opcode
                        time:   [1.9519 ms 1.9640 ms 1.9760 ms]
block target estimation/move opcode
                        time:   [1.8479 ms 1.8600 ms 1.8716 ms]
block target estimation/movi opcode
                        time:   [1.9531 ms 1.9645 ms 1.9758 ms]
block target estimation/mroo opcode
                        time:   [2.4029 ms 2.4151 ms 2.4272 ms]
block target estimation/mul opcode
                        time:   [2.0221 ms 2.0335 ms 2.0446 ms]
block target estimation/muli opcode
                        time:   [1.9674 ms 1.9781 ms 1.9897 ms]
block target estimation/noop opcode
                        time:   [1.8149 ms 1.8389 ms 1.8631 ms]
block target estimation/not opcode
                        time:   [1.8706 ms 1.8861 ms 1.9008 ms]
block target estimation/or opcode
                        time:   [2.0889 ms 2.0994 ms 2.1111 ms]
block target estimation/ori opcode
                        time:   [2.1002 ms 2.1106 ms 2.1208 ms]
block target estimation/sll opcode
                        time:   [2.1776 ms 2.1864 ms 2.1956 ms]
block target estimation/slli opcode
                        time:   [2.0932 ms 2.1038 ms 2.1153 ms]
block target estimation/srl opcode
                        time:   [2.0355 ms 2.0453 ms 2.0555 ms]
block target estimation/srli opcode
                        time:   [2.0444 ms 2.0509 ms 2.0578 ms]
block target estimation/sub opcode
                        time:   [2.1181 ms 2.1269 ms 2.1358 ms]
block target estimation/subi opcode
                        time:   [1.9773 ms 1.9905 ms 2.0050 ms]
block target estimation/xor opcode
                        time:   [2.0735 ms 2.0837 ms 2.0937 ms]
block target estimation/xori opcode
                        time:   [2.0139 ms 2.0246 ms 2.0354 ms]
block target estimation/wdcm opcode
                        time:   [2.2144 ms 2.2272 ms 2.2406 ms]
block target estimation/wdop opcode
                        time:   [2.5049 ms 2.5220 ms 2.5395 ms]
block target estimation/wdml opcode
                        time:   [2.6808 ms 2.6963 ms 2.7130 ms]
block target estimation/wddv opcode
                        time:   [4.2766 ms 4.3064 ms 4.3368 ms]
block target estimation/wdmd opcode
                        time:   [3.5144 ms 3.5451 ms 3.5798 ms]
block target estimation/wdam opcode
                        time:   [3.7095 ms 3.7397 ms 3.7760 ms]
block target estimation/wdmm opcode
                        time:   [3.6330 ms 3.6595 ms 3.6915 ms]
block target estimation/wqcm opcode
                        time:   [2.4236 ms 2.4410 ms 2.4597 ms]
block target estimation/wqop opcode
                        time:   [2.7717 ms 2.7920 ms 2.8154 ms]
block target estimation/wqml opcode
                        time:   [2.9315 ms 2.9631 ms 2.9986 ms]
block target estimation/wqdv opcode
                        time:   [3.4322 ms 3.4572 ms 3.4846 ms]
block target estimation/wqmd opcode
                        time:   [4.4126 ms 4.4383 ms 4.4669 ms]
block target estimation/wqam opcode
                        time:   [3.8279 ms 3.8610 ms 3.8986 ms]
block target estimation/wqmm opcode
                        time:   [5.8005 ms 5.8658 ms 5.9469 ms]
  ```

---------

Co-authored-by: Brandon Vrooman <brandon.vrooman@gmail.com>
Co-authored-by: xgreenx <xgreenx9999@gmail.com>
  • Loading branch information
3 people committed Oct 17, 2023
1 parent 651a3e0 commit 9ff3b11
Show file tree
Hide file tree
Showing 12 changed files with 615 additions and 65 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Description of the upcoming release here.

### Added

- [#1411](https://github.com/FuelLabs/fuel-core/pull/1411) Added WASM and `no_std` compatibility
- [#1419](https://github.com/FuelLabs/fuel-core/pull/1419): Add additional "sanity" benchmarks for arithmetic op code instructions.
- [#1411](https://github.com/FuelLabs/fuel-core/pull/1411): Added WASM and `no_std` compatibility
- [#1371](https://github.com/FuelLabs/fuel-core/pull/1371): Add new client function for querying the `MessageStatus` for a specific message (by `Nonce`)
- [#1356](https://github.com/FuelLabs/fuel-core/pull/1356): Add peer reputation reporting to heartbeat code
- [#1355](https://github.com/FuelLabs/fuel-core/pull/1355): Added new metrics related to block importing, such as tps, sync delays etc
Expand Down
50 changes: 32 additions & 18 deletions benches/benches/block_target_gas.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use block_target_gas_set::alu::run_alu;
use criterion::{
criterion_group,
criterion_main,
Expand All @@ -13,10 +14,19 @@ use fuel_core::service::{
};
use rand::SeedableRng;

use ethnum::U256;
use fuel_core_benches::*;
use fuel_core_types::{
fuel_asm::{
op,
wideint::{
CompareArgs,
CompareMode,
DivArgs,
MathArgs,
MathOp,
MulArgs,
},
GTFArgs,
Instruction,
RegId,
Expand All @@ -29,6 +39,15 @@ use fuel_core_types::{
fuel_types::AssetId,
};

mod utils;

mod block_target_gas_set;

use utils::{
make_u128,
make_u256,
};

// Use Jemalloc during benchmarks
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
Expand Down Expand Up @@ -72,17 +91,17 @@ fn run(
script.clone().into_iter().collect(),
script_data.clone(),
)
.gas_limit(TARGET_BLOCK_GAS_LIMIT - BASE)
.gas_price(1)
.add_unsigned_coin_input(
SecretKey::random(&mut rng),
rng.gen(),
u64::MAX,
AssetId::BASE,
Default::default(),
Default::default(),
)
.finalize_as_transaction();
.gas_limit(TARGET_BLOCK_GAS_LIMIT - BASE)
.gas_price(1)
.add_unsigned_coin_input(
SecretKey::random(&mut rng),
rng.gen(),
u64::MAX,
AssetId::BASE,
Default::default(),
Default::default(),
)
.finalize_as_transaction();
async move {
let tx_id = tx.id(&config.chain_conf.consensus_parameters.chain_id);

Expand Down Expand Up @@ -116,13 +135,6 @@ fn run(
fn block_target_gas(c: &mut Criterion) {
let mut group = c.benchmark_group("block target estimation");

run(
"Script with noop opcode and infinite loop",
&mut group,
[op::noop(), op::jmpb(RegId::ZERO, 0)].to_vec(),
vec![],
);

run(
"Script with meq opcode and infinite loop",
&mut group,
Expand Down Expand Up @@ -239,6 +251,8 @@ fn block_target_gas(c: &mut Criterion) {
// .to_vec(),
// );

run_alu(&mut group);

group.finish();
}

Expand Down
Loading

0 comments on commit 9ff3b11

Please sign in to comment.