@@ -2,29 +2,16 @@ use std::time::Duration;
2
2
3
3
use criterion:: { Criterion , criterion_group, criterion_main} ;
4
4
5
+ mod common;
6
+
5
7
#[ allow( clippy:: expect_used, clippy:: unwrap_used) ]
6
8
pub ( crate ) mod trustify_benches {
7
- use std:: io:: Error ;
8
- use std:: ops:: Add ;
9
- use std:: str:: FromStr ;
10
- use std:: sync:: Arc ;
11
9
use std:: time:: { Duration , Instant } ;
12
10
13
- use bytes:: Bytes ;
14
- use cpe:: cpe:: Cpe ;
15
- use cpe:: uri:: OwnedUri ;
16
11
use criterion:: { Criterion , black_box} ;
17
- use csaf:: Csaf ;
18
- use csaf:: definitions:: { BranchesT , ProductIdentificationHelper } ;
19
- use csaf:: product_tree:: ProductTree ;
20
- use csaf:: vulnerability:: Vulnerability ;
21
- use packageurl:: PackageUrl ;
22
- use sea_orm:: ConnectionTrait ;
23
- use test_context:: AsyncTestContext ;
24
- use tokio:: runtime:: Runtime ;
12
+ use std:: ops:: Add ;
25
13
use trustify_entity:: labels:: Labels ;
26
14
use trustify_module_ingestor:: service:: Format ;
27
- use trustify_test_context:: { TrustifyContext , document} ;
28
15
29
16
use opentelemetry:: trace:: TracerProvider as _;
30
17
use opentelemetry_sdk:: {
@@ -35,6 +22,8 @@ pub(crate) mod trustify_benches {
35
22
use tracing_opentelemetry:: OpenTelemetryLayer ;
36
23
use tracing_subscriber:: { layer:: SubscriberExt , util:: SubscriberInitExt } ;
37
24
25
+ use crate :: common;
26
+
38
27
fn resource ( ) -> Resource {
39
28
Resource :: builder ( )
40
29
. with_service_name ( "ingestion_csaf" . to_string ( ) )
@@ -87,19 +76,19 @@ pub(crate) mod trustify_benches {
87
76
static INIT : Once = Once :: new ( ) ;
88
77
89
78
pub fn ingestion ( c : & mut Criterion ) {
90
- let ( runtime, ctx) = setup_runtime_and_ctx ( ) ;
79
+ let ( runtime, ctx) = common :: setup_runtime_and_ctx ( ) ;
91
80
c. bench_function ( "ingestion_csaf" , |b| {
92
81
b. to_async ( & runtime) . iter_custom ( |count| {
93
82
let ctx = ctx. clone ( ) ;
94
83
async move {
95
84
INIT . call_once ( init_tracing_subscriber) ;
96
85
log:: info!( "db reset..." ) ;
97
- reset_db ( & ctx) . await ;
86
+ common :: reset_db ( & ctx) . await ;
98
87
99
88
let mut duration = Duration :: default ( ) ;
100
89
for i in 0 ..count {
101
90
log:: info!( "inserting document {}..." , i) ;
102
- let data = document_generated_from ( "csaf/cve-2023-33201.json" , i)
91
+ let data = common :: document_generated_from ( "csaf/cve-2023-33201.json" , i)
103
92
. await
104
93
. expect ( "data ok" ) ;
105
94
@@ -118,112 +107,6 @@ pub(crate) mod trustify_benches {
118
107
} ) ;
119
108
} ) ;
120
109
}
121
-
122
- async fn document_generated_from ( path : & str , rev : u64 ) -> Result < Bytes , Error > {
123
- let ( mut doc, _) : ( Csaf , _ ) = document ( path) . await . expect ( "load ok" ) ;
124
- doc. document . tracking . id = format ! ( "{}-{}" , doc. document. tracking. id, rev) ;
125
-
126
- fn rev_branches ( branches : & mut Option < BranchesT > , rev : u64 ) {
127
- if let Some ( BranchesT ( branches) ) = branches {
128
- for branch in branches. iter_mut ( ) {
129
- if let Some ( product) = & mut branch. product {
130
- rev_product_helper ( & mut product. product_identification_helper , rev) ;
131
- }
132
- rev_branches ( & mut branch. branches , rev) ;
133
- }
134
- }
135
- }
136
-
137
- fn rev_product_helper ( helper : & mut Option < ProductIdentificationHelper > , rev : u64 ) {
138
- if let Some ( helper) = helper {
139
- if let Some ( cpe) = & mut helper. cpe {
140
- helper. cpe = Some ( rev_cpe ( cpe, rev) )
141
- }
142
- if let Some ( purl) = & mut helper. purl {
143
- helper. purl = Some ( rev_purl ( purl. clone ( ) , rev) ) ;
144
- }
145
- }
146
- }
147
-
148
- fn rev_purl ( from : PackageUrl , rev : u64 ) -> PackageUrl {
149
- let name = from. name ( ) ;
150
- let new_name = format ! ( "{}-{}" , name, rev) ;
151
- PackageUrl :: from_str (
152
- from. to_string ( )
153
- . replacen ( name, new_name. as_str ( ) , 1 )
154
- . as_str ( ) ,
155
- )
156
- . unwrap ( )
157
- }
158
- fn rev_cpe ( cpe : & OwnedUri , rev : u64 ) -> OwnedUri {
159
- let uri = format ! ( "{:0}" , cpe) ;
160
- let mut uri = cpe:: uri:: Uri :: parse ( uri. as_str ( ) ) . unwrap ( ) ;
161
- let x = format ! ( "{}_{}" , uri. product( ) , rev) ;
162
- uri. set_product ( x. as_str ( ) ) . unwrap ( ) ;
163
- uri. to_owned ( )
164
- }
165
- fn rev_product_tree ( product_tree : & mut ProductTree , rev : u64 ) {
166
- rev_branches ( & mut product_tree. branches , rev) ;
167
- for relationships in product_tree. relationships . iter_mut ( ) {
168
- for relationship in relationships. iter_mut ( ) {
169
- // rev_product_id(&mut relationship.full_product_name.product_id, rev);
170
- rev_product_helper (
171
- & mut relationship. full_product_name . product_identification_helper ,
172
- rev,
173
- ) ;
174
- }
175
- }
176
- }
177
- fn rev_vulnerability ( vulnerability : & mut Vulnerability , rev : u64 ) {
178
- for cve in vulnerability. cve . iter_mut ( ) {
179
- * cve = format ! ( "{}-{}" , cve, rev) ;
180
- }
181
- }
182
-
183
- for product_tree in doc. product_tree . iter_mut ( ) {
184
- rev_product_tree ( product_tree, rev) ;
185
- }
186
- for vulnerabilities in doc. vulnerabilities . iter_mut ( ) {
187
- for vulnerability in vulnerabilities. iter_mut ( ) {
188
- rev_vulnerability ( vulnerability, rev) ;
189
- }
190
- }
191
-
192
- let data = serde_json:: to_vec_pretty ( & doc) . expect ( "serialize ok" ) ;
193
- Ok ( Bytes :: from ( data) )
194
- }
195
-
196
- fn setup_runtime_and_ctx ( ) -> ( Runtime , Arc < TrustifyContext > ) {
197
- let runtime = tokio:: runtime:: Builder :: new_multi_thread ( )
198
- . enable_all ( )
199
- . build ( )
200
- . unwrap ( ) ;
201
- let ctx = runtime. block_on ( async { TrustifyContext :: setup ( ) . await } ) ;
202
- ( runtime, Arc :: new ( ctx) )
203
- }
204
-
205
- async fn reset_db ( ctx : & Arc < TrustifyContext > ) {
206
- // reset DB tables to a clean state...
207
- for table in [
208
- "advisory" ,
209
- "base_purl" ,
210
- "versioned_purl" ,
211
- "qualified_purl" ,
212
- "cvss3" ,
213
- "cpe" ,
214
- "version_range" ,
215
- "vulnerability" ,
216
- ] {
217
- ctx. db
218
- . execute_unprepared ( format ! ( "DELETE FROM {table}" ) . as_str ( ) )
219
- . await
220
- . expect ( "DELETE ok" ) ;
221
- }
222
- ctx. db
223
- . execute_unprepared ( "VACUUM ANALYZE" )
224
- . await
225
- . expect ( "vacuum analyze ok" ) ;
226
- }
227
110
}
228
111
229
112
criterion_group ! {
0 commit comments