Skip to content

Commit 72949fe

Browse files
authored
[benchmarks] Add fibonacci benchmark (#17988)
* Add fibonacci benchmark * Use same number for each benchmark * Waive * Run rustfmt
1 parent ca9b3a0 commit 72949fe

File tree

7 files changed

+130
-1
lines changed

7 files changed

+130
-1
lines changed
412 Bytes
Binary file not shown.

crates/transaction-workloads-lib/src/args.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::{
5-
move_workloads::{LoopType, PreBuiltPackagesImpl},
5+
move_workloads::{FibonacciFunctionType, LoopType, PreBuiltPackagesImpl},
66
token_workflow::TokenWorkflowKind,
77
EntryPoints, MonotonicCounterType, OrderBookState,
88
};
@@ -106,6 +106,11 @@ pub enum TransactionTypeArg {
106106
MonotonicCounterMultiple10,
107107
MonotonicCounterMultiple100,
108108
MonotonicCounterMultiple1000,
109+
110+
// Fibonacci tests
111+
FibonacciRecursive20,
112+
FibonacciTailRecursive20,
113+
FibonacciIterative20,
109114
}
110115

111116
impl TransactionTypeArg {
@@ -487,6 +492,24 @@ impl TransactionTypeArg {
487492
counter_type: MonotonicCounterType::Multiple { count: 1000 },
488493
})
489494
},
495+
TransactionTypeArg::FibonacciRecursive20 => {
496+
call_custom_module(EntryPoints::Fibonacci {
497+
n: 20,
498+
function_type: FibonacciFunctionType::Recursive,
499+
})
500+
},
501+
TransactionTypeArg::FibonacciTailRecursive20 => {
502+
call_custom_module(EntryPoints::Fibonacci {
503+
n: 20,
504+
function_type: FibonacciFunctionType::TailRecursive,
505+
})
506+
},
507+
TransactionTypeArg::FibonacciIterative20 => {
508+
call_custom_module(EntryPoints::Fibonacci {
509+
n: 20,
510+
function_type: FibonacciFunctionType::Iterative,
511+
})
512+
},
490513
}
491514
}
492515

crates/transaction-workloads-lib/src/move_workloads.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ pub enum LoopType {
4747
BcsToBytes { len: u64 },
4848
}
4949

50+
#[derive(Debug, Clone)]
51+
pub enum FibonacciFunctionType {
52+
Recursive,
53+
TailRecursive,
54+
Iterative,
55+
}
56+
5057
/// Monotonically increasing counter test variants
5158
#[derive(Debug, Copy, Clone)]
5259
pub enum MonotonicCounterType {
@@ -127,6 +134,11 @@ pub enum EntryPoints {
127134
loop_count: Option<u64>,
128135
loop_type: LoopType,
129136
},
137+
/// Compute the n-th fibonacci number using the given function type.
138+
Fibonacci {
139+
n: u64,
140+
function_type: FibonacciFunctionType,
141+
},
130142
// next 2 functions, second arg must be existing account address with data
131143
// Sets `Resource` to the max from two addresses
132144
Maximize,
@@ -314,6 +326,7 @@ impl EntryPointTrait for EntryPoints {
314326
| EntryPoints::Double
315327
| EntryPoints::Half
316328
| EntryPoints::Loop { .. }
329+
| EntryPoints::Fibonacci { .. }
317330
| EntryPoints::GetFromConst { .. }
318331
| EntryPoints::SetId
319332
| EntryPoints::SetName
@@ -384,6 +397,7 @@ impl EntryPointTrait for EntryPoints {
384397
| EntryPoints::Double
385398
| EntryPoints::Half
386399
| EntryPoints::Loop { .. }
400+
| EntryPoints::Fibonacci { .. }
387401
| EntryPoints::GetFromConst { .. }
388402
| EntryPoints::SetId
389403
| EntryPoints::SetName
@@ -501,6 +515,16 @@ impl EntryPointTrait for EntryPoints {
501515
};
502516
get_payload(module_id, ident_str!(method).to_owned(), args)
503517
},
518+
EntryPoints::Fibonacci { n, function_type } => {
519+
let method = match function_type {
520+
FibonacciFunctionType::Recursive => "fibonacci_recursive",
521+
FibonacciFunctionType::TailRecursive => "fibonacci_tail_recursive",
522+
FibonacciFunctionType::Iterative => "fibonacci_iterative",
523+
};
524+
get_payload(module_id, ident_str!(method).to_owned(), vec![
525+
bcs::to_bytes(&n).unwrap(),
526+
])
527+
},
504528
EntryPoints::GetFromConst { const_idx } => get_from_random_const(
505529
module_id,
506530
const_idx.unwrap_or_else(
@@ -995,6 +1019,7 @@ impl EntryPointTrait for EntryPoints {
9951019
| EntryPoints::Double
9961020
| EntryPoints::Half
9971021
| EntryPoints::Loop { .. }
1022+
| EntryPoints::Fibonacci { .. }
9981023
| EntryPoints::GetFromConst { .. }
9991024
| EntryPoints::SetId
10001025
| EntryPoints::SetName

execution/executor-benchmark/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,4 +1076,28 @@ mod tests {
10761076
NativeParallelUncoordinatedBlockExecutor<NativeNoStorageRawTransactionExecutor>,
10771077
>(Some(TransactionTypeArg::NoOp), false);
10781078
}
1079+
1080+
#[test]
1081+
fn test_fibonacci_recursive() {
1082+
test_generic_benchmark::<AptosVMBlockExecutor>(
1083+
Some(TransactionTypeArg::FibonacciRecursive20),
1084+
true,
1085+
);
1086+
}
1087+
1088+
#[test]
1089+
fn test_fibonacci_tail_recursive() {
1090+
test_generic_benchmark::<AptosVMBlockExecutor>(
1091+
Some(TransactionTypeArg::FibonacciTailRecursive20),
1092+
true,
1093+
);
1094+
}
1095+
1096+
#[test]
1097+
fn test_fibonacci_iterative() {
1098+
test_generic_benchmark::<AptosVMBlockExecutor>(
1099+
Some(TransactionTypeArg::FibonacciIterative20),
1100+
true,
1101+
);
1102+
}
10791103
}

testsuite/benchmark-workloads/packages/simple/sources/Simple.move

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,56 @@ module 0xABCD::simple {
101101
}
102102
}
103103

104+
public entry fun fibonacci_recursive(_s: &signer, n: u64) {
105+
fib_rec(n);
106+
}
107+
108+
public entry fun fibonacci_tail_recursive(_s: &signer, n: u64) {
109+
fib_tail_rec(n, 0, 1);
110+
}
111+
112+
public entry fun fibonacci_iterative(_s: &signer, n: u64) {
113+
fib_iter(n);
114+
}
115+
116+
/// Computes the n-th fibonacci number using a simple recursive algorithm.
117+
fun fib_rec(n: u64): u64 {
118+
if (n <= 1) {
119+
n
120+
} else {
121+
fib_rec(n - 1) + fib_rec(n - 2)
122+
}
123+
}
124+
125+
/// Computes the n-th fibonacci number using a tail-recursive algorithm.
126+
fun fib_tail_rec(n: u64, a: u64, b: u64): u64 {
127+
if (n == 0) {
128+
a
129+
} else {
130+
fib_tail_rec(n - 1, b, a + b)
131+
}
132+
}
133+
134+
/// Computes the n-th fibonacci number using an iterative algorithm.
135+
fun fib_iter(n: u64): u64 {
136+
if (n <= 1) {
137+
return n;
138+
};
139+
140+
let a = 0;
141+
let b = 1;
142+
143+
let i = 2;
144+
while (i <= n) {
145+
let temp = a + b;
146+
a = b;
147+
b = temp;
148+
i += 1;
149+
};
150+
151+
b
152+
}
153+
104154
// Counter
105155
// This is a constant to change to check versioning of the module published.
106156
// In a simple way this can be used as a verion info, and incremented by 1

testsuite/single_node_performance.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ class RunGroupConfig:
275275
RunGroupConfig(key=RunGroupKey("order-book-balanced-size-skewed80-pct50-markets"), included_in=Flow.ORDER_BOOK | Flow.CONTINUOUS, waived=True),
276276
RunGroupConfig(key=RunGroupKey("monotonic-counter-single"), included_in=Flow.CONTINUOUS, waived=True),
277277

278+
RunGroupConfig(key=RunGroupKey("fibonacci-recursive20"), included_in=Flow.CONTINUOUS, waived=True),
279+
RunGroupConfig(key=RunGroupKey("fibonacci-tail-recursive20"), included_in=Flow.CONTINUOUS, waived=True),
280+
RunGroupConfig(key=RunGroupKey("fibonacci-iterative20"), included_in=Flow.CONTINUOUS, waived=True),
281+
278282
RunGroupConfig(expected_tps=50000, key=RunGroupKey("coin_transfer_connected_components", executor_type="sharded"), key_extra=RunGroupKeyExtra(sharding_traffic_flags="--connected-tx-grps 5000", transaction_type_override=""), included_in=Flow.REPRESENTATIVE, waived=True),
279283
RunGroupConfig(expected_tps=50000, key=RunGroupKey("coin_transfer_hotspot", executor_type="sharded"), key_extra=RunGroupKeyExtra(sharding_traffic_flags="--hotspot-probability 0.8", transaction_type_override=""), included_in=Flow.REPRESENTATIVE, waived=True),
280284

testsuite/single_node_performance_values.tsv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ no_commit_apt-fa-transfer 1 VM 5 0.756 1.016 35479.7
5353
no_commit_apt-fa-transfer 1 NativeVM 5 0.799 1.013 60489.6
5454
no_commit_apt-fa-transfer 1 AptosVMSpeculative 5 0.680 1.014 10473.4
5555
no_commit_apt-fa-transfer 1 NativeSpeculative 5 0.702 1.011 102011.3
56+
fibonacci-recursive-20 1 VM 9 0.989 1.017 3339.3
57+
fibonacci-tail-recursive-20 1 VM 9 0.989 1.017 3339.3
58+
fibonacci-iterative-20 1 VM 9 0.989 1.017 3339.3

0 commit comments

Comments
 (0)