1+ use criterion:: { criterion_group, criterion_main, Criterion , BenchmarkId } ;
2+ use osvm:: utils:: ebpf_deploy:: {
3+ load_program, load_program_id, DeployConfig , RpcClientCache ,
4+ } ;
5+ use std:: fs:: File ;
6+ use std:: io:: Write ;
7+ use std:: time:: { Duration , Instant } ;
8+ use tempfile:: tempdir;
9+
10+ /// Benchmark program loading performance
11+ fn benchmark_load_program ( c : & mut Criterion ) {
12+ let dir = tempdir ( ) . unwrap ( ) ;
13+
14+ // Create test programs of different sizes
15+ let sizes = vec ! [
16+ ( "small" , 10_000 ) , // 10KB
17+ ( "medium" , 100_000 ) , // 100KB
18+ ( "large" , 1_000_000 ) , // 1MB
19+ ( "xlarge" , 2_000_000 ) , // 2MB
20+ ] ;
21+
22+ let mut group = c. benchmark_group ( "load_program" ) ;
23+
24+ for ( name, size) in sizes {
25+ let program_path = dir. path ( ) . join ( format ! ( "program_{}.so" , name) ) ;
26+ let program_data = vec ! [ 0u8 ; size] ;
27+ let mut file = File :: create ( & program_path) . unwrap ( ) ;
28+ file. write_all ( & program_data) . unwrap ( ) ;
29+
30+ group. benchmark_with_input (
31+ BenchmarkId :: new ( "load_program" , name) ,
32+ & program_path. to_string_lossy ( ) . to_string ( ) ,
33+ |b, path| {
34+ b. iter ( || {
35+ load_program ( path) . unwrap ( )
36+ } ) ;
37+ } ,
38+ ) ;
39+ }
40+
41+ group. finish ( ) ;
42+ }
43+
44+ /// Benchmark program ID loading performance
45+ fn benchmark_load_program_id ( c : & mut Criterion ) {
46+ let dir = tempdir ( ) . unwrap ( ) ;
47+ let program_id_file = dir. path ( ) . join ( "program_id.json" ) ;
48+
49+ // Create test program ID file
50+ let program_id_content = r#"{"programId": "HN4tEEGheziD9dqcWg4xZd29htcerjXKGoGiQXM5hxiS"}"# ;
51+ let mut file = File :: create ( & program_id_file) . unwrap ( ) ;
52+ file. write_all ( program_id_content. as_bytes ( ) ) . unwrap ( ) ;
53+
54+ c. bench_function ( "load_program_id" , |b| {
55+ b. iter ( || {
56+ load_program_id ( program_id_file. to_str ( ) . unwrap ( ) ) . unwrap ( )
57+ } ) ;
58+ } ) ;
59+ }
60+
61+ /// Benchmark RPC client cache performance
62+ fn benchmark_rpc_client_cache ( c : & mut Criterion ) {
63+ let mut group = c. benchmark_group ( "rpc_client_cache" ) ;
64+
65+ // Test cache performance with multiple URLs
66+ let urls = vec ! [
67+ "https://api.devnet.solana.com" ,
68+ "https://api.testnet.solana.com" ,
69+ "https://api.mainnet-beta.solana.com" ,
70+ ] ;
71+
72+ group. bench_function ( "cache_miss" , |b| {
73+ b. iter ( || {
74+ let mut cache = RpcClientCache :: new ( ) ;
75+ for url in & urls {
76+ cache. get_client ( url) ;
77+ }
78+ } ) ;
79+ } ) ;
80+
81+ group. bench_function ( "cache_hit" , |b| {
82+ let mut cache = RpcClientCache :: new ( ) ;
83+ // Pre-populate cache
84+ for url in & urls {
85+ cache. get_client ( url) ;
86+ }
87+
88+ b. iter ( || {
89+ for url in & urls {
90+ cache. get_client ( url) ;
91+ }
92+ } ) ;
93+ } ) ;
94+
95+ group. finish ( ) ;
96+ }
97+
98+ /// Benchmark deployment configuration creation
99+ fn benchmark_deploy_config_creation ( c : & mut Criterion ) {
100+ c. bench_function ( "deploy_config_creation" , |b| {
101+ b. iter ( || {
102+ DeployConfig {
103+ binary_path : "program.so" . to_string ( ) ,
104+ program_id_path : "program_id.json" . to_string ( ) ,
105+ owner_path : "owner.json" . to_string ( ) ,
106+ fee_payer_path : "fee_payer.json" . to_string ( ) ,
107+ publish_idl : true ,
108+ idl_file_path : Some ( "idl.json" . to_string ( ) ) ,
109+ network_selection : "all" . to_string ( ) ,
110+ json_output : false ,
111+ retry_attempts : 3 ,
112+ confirm_large_binaries : false ,
113+ }
114+ } ) ;
115+ } ) ;
116+ }
117+
118+ /// Benchmark JSON serialization performance
119+ fn benchmark_json_operations ( c : & mut Criterion ) {
120+ use osvm:: utils:: ebpf_deploy:: DeploymentResult ;
121+ use solana_sdk:: pubkey:: Pubkey ;
122+
123+ let result = DeploymentResult {
124+ network : "devnet" . to_string ( ) ,
125+ program_id : Pubkey :: new_unique ( ) ,
126+ success : true ,
127+ transaction_signature : Some ( "test_signature_12345" . to_string ( ) ) ,
128+ error_message : None ,
129+ retries_attempted : 3 ,
130+ duration_ms : 5000 ,
131+ } ;
132+
133+ let mut group = c. benchmark_group ( "json_operations" ) ;
134+
135+ group. bench_function ( "serialize" , |b| {
136+ b. iter ( || {
137+ serde_json:: to_string ( & result) . unwrap ( )
138+ } ) ;
139+ } ) ;
140+
141+ let json_str = serde_json:: to_string ( & result) . unwrap ( ) ;
142+ group. bench_function ( "deserialize" , |b| {
143+ b. iter ( || {
144+ let _: DeploymentResult = serde_json:: from_str ( & json_str) . unwrap ( ) ;
145+ } ) ;
146+ } ) ;
147+
148+ group. finish ( ) ;
149+ }
150+
151+ /// Benchmark file operations for deployment
152+ fn benchmark_file_operations ( c : & mut Criterion ) {
153+ let dir = tempdir ( ) . unwrap ( ) ;
154+
155+ // Create test files of various sizes
156+ let file_sizes = vec ! [ 1_000 , 10_000 , 100_000 , 1_000_000 ] ;
157+ let mut test_files = Vec :: new ( ) ;
158+
159+ for size in file_sizes {
160+ let file_path = dir. path ( ) . join ( format ! ( "test_{}.dat" , size) ) ;
161+ let data = vec ! [ 0u8 ; size] ;
162+ let mut file = File :: create ( & file_path) . unwrap ( ) ;
163+ file. write_all ( & data) . unwrap ( ) ;
164+ test_files. push ( ( size, file_path) ) ;
165+ }
166+
167+ let mut group = c. benchmark_group ( "file_operations" ) ;
168+
169+ for ( size, file_path) in test_files {
170+ group. benchmark_with_input (
171+ BenchmarkId :: new ( "read_file" , size) ,
172+ & file_path,
173+ |b, path| {
174+ b. iter ( || {
175+ std:: fs:: read ( path) . unwrap ( )
176+ } ) ;
177+ } ,
178+ ) ;
179+ }
180+
181+ group. finish ( ) ;
182+ }
183+
184+ /// Benchmark memory usage and allocation patterns
185+ fn benchmark_memory_allocation ( c : & mut Criterion ) {
186+ let mut group = c. benchmark_group ( "memory_allocation" ) ;
187+
188+ // Test vector allocation for different program sizes
189+ let sizes = vec ! [ 10_000 , 100_000 , 1_000_000 , 5_000_000 ] ;
190+
191+ for size in sizes {
192+ group. benchmark_with_input (
193+ BenchmarkId :: new ( "vec_allocation" , size) ,
194+ & size,
195+ |b, & size| {
196+ b. iter ( || {
197+ let _data: Vec < u8 > = vec ! [ 0 ; size] ;
198+ } ) ;
199+ } ,
200+ ) ;
201+ }
202+
203+ group. finish ( ) ;
204+ }
205+
206+ criterion_group ! (
207+ benches,
208+ benchmark_load_program,
209+ benchmark_load_program_id,
210+ benchmark_rpc_client_cache,
211+ benchmark_deploy_config_creation,
212+ benchmark_json_operations,
213+ benchmark_file_operations,
214+ benchmark_memory_allocation
215+ ) ;
216+
217+ criterion_main ! ( benches) ;
0 commit comments