@@ -5,12 +5,45 @@ use rand::Rng;
55
66const MAX_KEY_VALUE_PAIRS : usize = 64 ;
77
8+ // Run this benchmark with:
9+ // cargo bench --bench baggage
10+ // Adding results in comments for a quick reference.
11+ // Apple M4 Pro
12+ // Total Number of Cores: 14 (10 performance and 4 efficiency)
13+ // Results:
14+ // set_baggage_static_key_value 12 ns
15+ // set_baggage_static_key 28 ns
16+ // set_baggage_dynamic 60 ns
17+ // set_baggage_dynamic_with_metadata 112 ns
18+
819fn criterion_benchmark ( c : & mut Criterion ) {
9- set_baggage_value ( c) ;
10- set_baggage_value_with_metadata ( c) ;
20+ set_baggage_static_key_value ( c) ;
21+ set_baggage_static_key ( c) ;
22+ set_baggage_dynamic ( c) ;
23+ set_baggage_dynamic_with_metadata ( c) ;
24+ }
25+
26+ fn set_baggage_static_key_value ( c : & mut Criterion ) {
27+ let mut baggage = Baggage :: new ( ) ;
28+
29+ c. bench_function ( "set_baggage_static_key_value" , move |b| {
30+ b. iter ( || {
31+ baggage. insert ( "key" , "value" ) ;
32+ } )
33+ } ) ;
34+ }
35+
36+ fn set_baggage_static_key ( c : & mut Criterion ) {
37+ let mut baggage = Baggage :: new ( ) ;
38+
39+ c. bench_function ( "set_baggage_static_key" , move |b| {
40+ b. iter ( || {
41+ baggage. insert ( "key" , "value" . to_string ( ) ) ;
42+ } )
43+ } ) ;
1144}
1245
13- fn set_baggage_value ( c : & mut Criterion ) {
46+ fn set_baggage_dynamic ( c : & mut Criterion ) {
1447 let mut baggage = Baggage :: new ( ) ;
1548
1649 let mut rng = rand:: rng ( ) ;
@@ -23,7 +56,7 @@ fn set_baggage_value(c: &mut Criterion) {
2356 } )
2457 . collect :: < Vec < ( String , String ) > > ( ) ;
2558
26- c. bench_function ( "set_baggage_value " , move |b| {
59+ c. bench_function ( "set_baggage_dynamic " , move |b| {
2760 b. iter_batched (
2861 || rng. random_range ( 0 ..MAX_KEY_VALUE_PAIRS ) ,
2962 |idx| {
@@ -35,7 +68,7 @@ fn set_baggage_value(c: &mut Criterion) {
3568 } ) ;
3669}
3770
38- fn set_baggage_value_with_metadata ( c : & mut Criterion ) {
71+ fn set_baggage_dynamic_with_metadata ( c : & mut Criterion ) {
3972 let mut baggage = Baggage :: new ( ) ;
4073
4174 let mut rng = rand:: rng ( ) ;
@@ -49,7 +82,7 @@ fn set_baggage_value_with_metadata(c: &mut Criterion) {
4982 } )
5083 . collect :: < Vec < ( String , String , String ) > > ( ) ;
5184
52- c. bench_function ( "set_baggage_value_with_metadata " , move |b| {
85+ c. bench_function ( "set_baggage_dynamic_with_metadata " , move |b| {
5386 b. iter_batched (
5487 || rng. random_range ( 0 ..MAX_KEY_VALUE_PAIRS ) ,
5588 |idx| {
0 commit comments