11use crate :: selector:: CompileTestCase ;
22use crate :: {
3- ArtifactCollection , ArtifactId , ArtifactIdNumber , BenchmarkJob , BenchmarkRequest ,
4- BenchmarkRequestIndex , BenchmarkRequestStatus , BenchmarkSet , CodegenBackend , CollectorConfig ,
5- CompileBenchmark , Target ,
3+ ArtifactCollection , ArtifactId , ArtifactIdNumber , BenchmarkJob , BenchmarkJobStatus ,
4+ BenchmarkRequest , BenchmarkRequestIndex , BenchmarkRequestStatus , BenchmarkSet , CodegenBackend ,
5+ CollectorConfig , CompileBenchmark , Target ,
66} ;
77use crate :: { CollectionId , Index , Profile , QueuedCommit , Scenario , Step } ;
88use chrono:: { DateTime , Utc } ;
@@ -246,7 +246,7 @@ pub trait Connection: Send + Sync {
246246 /// Get the confiuguration for a collector by the name of the collector
247247 async fn get_collector_config ( & self , collector_name : & str ) -> anyhow:: Result < CollectorConfig > ;
248248
249- /// Get the confiuguration for a collector by the name of the collector
249+ /// Dequeue benchmark job
250250 async fn dequeue_benchmark_job (
251251 & self ,
252252 collector_name : & str ,
@@ -256,10 +256,17 @@ pub trait Connection: Send + Sync {
256256
257257 /// Try and mark the benchmark_request as completed. Will return `true` if
258258 /// it has been marked as completed else `false` meaning there was no change
259- async fn mark_benchmark_request_as_completed (
259+ async fn mark_benchmark_request_as_completed ( & self , tag : & str ) -> anyhow:: Result < bool > ;
260+
261+ /// Mark the job as completed. Sets the status to 'failed' or 'success'
262+ /// depending on the enum's completed state being a success
263+ async fn mark_benchmark_job_as_completed (
260264 & self ,
261- benchmark_request : & BenchmarkRequest ,
262- ) -> anyhow:: Result < bool > ;
265+ request_tag : & str ,
266+ benchmark_set : u32 ,
267+ target : & Target ,
268+ status : & BenchmarkJobStatus ,
269+ ) -> anyhow:: Result < ( ) > ;
263270}
264271
265272#[ async_trait:: async_trait]
@@ -395,6 +402,53 @@ mod tests {
395402 }
396403 }
397404
405+ async fn complete_request (
406+ db : & dyn Connection ,
407+ request_tag : & str ,
408+ collector_name : & str ,
409+ benchmark_set : u32 ,
410+ target : & Target ,
411+ ) {
412+ let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
413+ /* Create job for the request */
414+ db. enqueue_benchmark_job (
415+ request_tag,
416+ & target,
417+ & CodegenBackend :: Llvm ,
418+ & Profile :: Opt ,
419+ benchmark_set,
420+ )
421+ . await
422+ . unwrap ( ) ;
423+
424+ let job = db
425+ . dequeue_benchmark_job ( collector_name, & target, & BenchmarkSet ( benchmark_set) )
426+ . await
427+ . unwrap ( )
428+ . unwrap ( ) ;
429+
430+ assert_eq ! ( job. request_tag( ) , request_tag) ;
431+
432+ /* Mark the job as complete */
433+ db. mark_benchmark_job_as_completed (
434+ request_tag,
435+ benchmark_set,
436+ & target,
437+ & BenchmarkJobStatus :: Completed {
438+ started_at : time,
439+ completed_at : time,
440+ collector_name : collector_name. into ( ) ,
441+ success : true ,
442+ } ,
443+ )
444+ . await
445+ . unwrap ( ) ;
446+
447+ db. mark_benchmark_request_as_completed ( request_tag)
448+ . await
449+ . unwrap ( ) ;
450+ }
451+
398452 #[ tokio:: test]
399453 async fn pstat_returns_empty_vector_when_empty ( ) {
400454 run_db_test ( |ctx| async {
@@ -482,28 +536,31 @@ mod tests {
482536 #[ tokio:: test]
483537 async fn multiple_non_completed_try_requests ( ) {
484538 run_postgres_test ( |ctx| async {
485- let db = ctx. db_client ( ) ;
486- let db = db. connection ( ) . await ;
539+ let db = ctx. db_client ( ) . connection ( ) . await ;
540+ let target = & Target :: X86_64UnknownLinuxGnu ;
541+ let collector_name = "collector-1" ;
542+ let benchmark_set = 1 ;
487543
488- // Completed
544+ db. add_collector_config ( collector_name, & target, benchmark_set, true )
545+ . await
546+ . unwrap ( ) ;
547+
548+ // Complete parent
549+ let parent = BenchmarkRequest :: create_release ( "sha-parent-1" , Utc :: now ( ) ) ;
550+ // Complete
489551 let req_a = BenchmarkRequest :: create_try_without_artifacts ( 42 , Utc :: now ( ) , "" , "" ) ;
490552 // WaitingForArtifacts
491553 let req_b = BenchmarkRequest :: create_try_without_artifacts ( 42 , Utc :: now ( ) , "" , "" ) ;
492554 let req_c = BenchmarkRequest :: create_try_without_artifacts ( 42 , Utc :: now ( ) , "" , "" ) ;
493555
556+ db. insert_benchmark_request ( & parent) . await . unwrap ( ) ;
494557 db. insert_benchmark_request ( & req_a) . await . unwrap ( ) ;
495558 db. attach_shas_to_try_benchmark_request ( 42 , "sha1" , "sha-parent-1" )
496559 . await
497560 . unwrap ( ) ;
498561
499- db. update_benchmark_request_status (
500- "sha1" ,
501- BenchmarkRequestStatus :: Completed {
502- completed_at : Utc :: now ( ) ,
503- } ,
504- )
505- . await
506- . unwrap ( ) ;
562+ complete_request ( & * db, "sha-parent-1" , collector_name, benchmark_set, & target) . await ;
563+ complete_request ( & * db, "sha1" , collector_name, benchmark_set, & target) . await ;
507564
508565 // This should be fine, req_a was completed
509566 db. insert_benchmark_request ( & req_b) . await . unwrap ( ) ;
@@ -550,8 +607,15 @@ mod tests {
550607 #[ tokio:: test]
551608 async fn load_pending_benchmark_requests ( ) {
552609 run_postgres_test ( |ctx| async {
553- let db = ctx. db_client ( ) ;
610+ let db = ctx. db_client ( ) . connection ( ) . await ;
554611 let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
612+ let target = & Target :: X86_64UnknownLinuxGnu ;
613+ let collector_name = "collector-1" ;
614+ let benchmark_set = 1 ;
615+
616+ db. add_collector_config ( collector_name, & target, benchmark_set, true )
617+ . await
618+ . unwrap ( ) ;
555619
556620 // ArtifactsReady
557621 let req_a = BenchmarkRequest :: create_master ( "sha-1" , "parent-sha-1" , 42 , time) ;
@@ -562,24 +626,17 @@ mod tests {
562626 // InProgress
563627 let req_d = BenchmarkRequest :: create_master ( "sha-2" , "parent-sha-2" , 51 , time) ;
564628 // Completed
565- let req_e = BenchmarkRequest :: create_master ( "sha-3" , "parent-sha-3" , 52 , time) ;
629+ let req_e = BenchmarkRequest :: create_release ( "1.79.0" , time) ;
566630
567- let db = db. connection ( ) . await ;
568631 for & req in & [ & req_a, & req_b, & req_c, & req_d, & req_e] {
569632 db. insert_benchmark_request ( req) . await . unwrap ( ) ;
570633 }
571634
635+ complete_request ( & * db, "1.79.0" , collector_name, benchmark_set, & target) . await ;
636+
572637 db. update_benchmark_request_status ( "sha-2" , BenchmarkRequestStatus :: InProgress )
573638 . await
574639 . unwrap ( ) ;
575- db. update_benchmark_request_status (
576- "sha-3" ,
577- BenchmarkRequestStatus :: Completed {
578- completed_at : Utc :: now ( ) ,
579- } ,
580- )
581- . await
582- . unwrap ( ) ;
583640
584641 let requests = db. load_pending_benchmark_requests ( ) . await . unwrap ( ) ;
585642
@@ -844,4 +901,95 @@ mod tests {
844901 } )
845902 . await ;
846903 }
904+
905+ #[ tokio:: test]
906+ async fn mark_request_as_complete_empty ( ) {
907+ run_postgres_test ( |ctx| async {
908+ let db = ctx. db_client ( ) . connection ( ) . await ;
909+ let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
910+
911+ let insert_result = db
912+ . add_collector_config ( "collector-1" , & Target :: X86_64UnknownLinuxGnu , 1 , true )
913+ . await ;
914+ assert ! ( insert_result. is_ok( ) ) ;
915+
916+ let benchmark_request =
917+ BenchmarkRequest :: create_master ( "sha-1" , "parent-sha-1" , 42 , time) ;
918+ db. insert_benchmark_request ( & benchmark_request)
919+ . await
920+ . unwrap ( ) ;
921+ assert ! ( db
922+ . mark_benchmark_request_as_completed( "sha-1" )
923+ . await
924+ . is_ok( ) ) ;
925+ Ok ( ctx)
926+ } )
927+ . await ;
928+ }
929+
930+ #[ tokio:: test]
931+ async fn mark_request_as_complete ( ) {
932+ run_postgres_test ( |ctx| async {
933+ let db = ctx. db_client ( ) . connection ( ) . await ;
934+ let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
935+ let benchmark_set = BenchmarkSet ( 0u32 ) ;
936+ let tag = "sha-1" ;
937+ let collector_name = "collector-1" ;
938+ let target = Target :: X86_64UnknownLinuxGnu ;
939+
940+ let insert_result = db
941+ . add_collector_config ( collector_name, & target, 1 , true )
942+ . await ;
943+ assert ! ( insert_result. is_ok( ) ) ;
944+
945+ /* Create the request */
946+ let benchmark_request = BenchmarkRequest :: create_release ( tag, time) ;
947+ db. insert_benchmark_request ( & benchmark_request)
948+ . await
949+ . unwrap ( ) ;
950+
951+ /* Create job for the request */
952+ db. enqueue_benchmark_job (
953+ benchmark_request. tag ( ) . unwrap ( ) ,
954+ & target,
955+ & CodegenBackend :: Llvm ,
956+ & Profile :: Opt ,
957+ benchmark_set. 0 ,
958+ )
959+ . await
960+ . unwrap ( ) ;
961+
962+ let job = db
963+ . dequeue_benchmark_job ( collector_name, & target, & benchmark_set)
964+ . await
965+ . unwrap ( )
966+ . unwrap ( ) ;
967+
968+ assert_eq ! ( job. request_tag( ) , benchmark_request. tag( ) . unwrap( ) ) ;
969+
970+ /* Mark the job as complete */
971+ db. mark_benchmark_job_as_completed (
972+ tag,
973+ benchmark_set. 0 ,
974+ & target,
975+ & BenchmarkJobStatus :: Completed {
976+ started_at : time,
977+ completed_at : time,
978+ collector_name : collector_name. into ( ) ,
979+ success : false ,
980+ } ,
981+ )
982+ . await
983+ . unwrap ( ) ;
984+
985+ db. mark_benchmark_request_as_completed ( tag) . await . unwrap ( ) ;
986+
987+ let completed = db. load_benchmark_request_index ( ) . await . unwrap ( ) ;
988+
989+ assert ! ( completed. contains_tag( "sha-1" ) ) ;
990+
991+ Ok ( ctx)
992+ } )
993+ . await ;
994+ }
847995}
0 commit comments