|
1 | 1 | use crate::{ |
2 | | - ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, CodegenBackend, |
3 | | - CompileBenchmark, Target, |
| 2 | + ArtifactCollection, ArtifactId, ArtifactIdNumber, BenchmarkRequest, BenchmarkRequestStatus, |
| 3 | + CodegenBackend, CompileBenchmark, Target, |
4 | 4 | }; |
5 | 5 | use crate::{CollectionId, Index, Profile, QueuedCommit, Scenario, Step}; |
6 | 6 | use chrono::{DateTime, Utc}; |
@@ -183,6 +183,21 @@ pub trait Connection: Send + Sync { |
183 | 183 | /// Add an item to the `benchmark_requests`, if the `benchmark_request` |
184 | 184 | /// exists it will be ignored |
185 | 185 | async fn insert_benchmark_request(&self, benchmark_request: &BenchmarkRequest); |
| 186 | + |
| 187 | + /// Gets the benchmark requests matching the status. Optionally provide the |
| 188 | + /// number of days from whence to search from |
| 189 | + async fn get_benchmark_requests_by_status( |
| 190 | + &self, |
| 191 | + statuses: &[BenchmarkRequestStatus], |
| 192 | + days: Option<i32>, |
| 193 | + ) -> Vec<BenchmarkRequest>; |
| 194 | + |
| 195 | + /// Update the status of a `benchmark_request` |
| 196 | + async fn update_benchmark_request_status( |
| 197 | + &mut self, |
| 198 | + benchmark_request: &BenchmarkRequest, |
| 199 | + benchmark_request_status: BenchmarkRequestStatus, |
| 200 | + ); |
186 | 201 | } |
187 | 202 |
|
188 | 203 | #[async_trait::async_trait] |
@@ -424,4 +439,181 @@ mod tests { |
424 | 439 | }) |
425 | 440 | .await; |
426 | 441 | } |
| 442 | + |
| 443 | + #[tokio::test] |
| 444 | + async fn get_benchmark_requests_by_status() { |
| 445 | + // Ensure we get back the requests matching the status with no date |
| 446 | + // limit |
| 447 | + run_postgres_test(|ctx| async { |
| 448 | + let db = ctx.db_client(); |
| 449 | + let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap(); |
| 450 | + let master_benchmark_request = BenchmarkRequest::create_master( |
| 451 | + "a-sha-1", |
| 452 | + "parent-sha-1", |
| 453 | + 42, |
| 454 | + time, |
| 455 | + BenchmarkRequestStatus::WaitingForParent, |
| 456 | + "llvm", |
| 457 | + "", |
| 458 | + ); |
| 459 | + |
| 460 | + let try_benchmark_request = BenchmarkRequest::create_try( |
| 461 | + "b-sha-2", |
| 462 | + "parent-sha-2", |
| 463 | + 32, |
| 464 | + time, |
| 465 | + BenchmarkRequestStatus::Completed, |
| 466 | + "cranelift", |
| 467 | + "", |
| 468 | + ); |
| 469 | + |
| 470 | + let release_benchmark_request = BenchmarkRequest::create_release( |
| 471 | + "1.8.0", |
| 472 | + time, |
| 473 | + BenchmarkRequestStatus::WaitingForParent, |
| 474 | + "cranelift,llvm", |
| 475 | + "", |
| 476 | + ); |
| 477 | + |
| 478 | + let db = db.connection().await; |
| 479 | + db.insert_benchmark_request(&master_benchmark_request).await; |
| 480 | + db.insert_benchmark_request(&try_benchmark_request).await; |
| 481 | + db.insert_benchmark_request(&release_benchmark_request) |
| 482 | + .await; |
| 483 | + |
| 484 | + let requests = db |
| 485 | + .get_benchmark_requests_by_status(&[BenchmarkRequestStatus::WaitingForParent], None) |
| 486 | + .await; |
| 487 | + |
| 488 | + assert_eq!(requests.len(), 2); |
| 489 | + assert_eq!(requests[0].status, BenchmarkRequestStatus::WaitingForParent); |
| 490 | + assert_eq!(requests[1].status, BenchmarkRequestStatus::WaitingForParent); |
| 491 | + |
| 492 | + Ok(ctx) |
| 493 | + }) |
| 494 | + .await; |
| 495 | + } |
| 496 | + |
| 497 | + #[tokio::test] |
| 498 | + async fn get_benchmark_requests_by_status_and_date() { |
| 499 | + run_postgres_test(|ctx| async { |
| 500 | + let db = ctx.db_client(); |
| 501 | + let old_created_at = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap(); |
| 502 | + let created_at = Utc::now() - chrono::Duration::days(5); |
| 503 | + let cutoff_days = 6; |
| 504 | + |
| 505 | + // The following three are the only three that should be returned |
| 506 | + // from the database query. |
| 507 | + let master_benchmark_request = BenchmarkRequest::create_master( |
| 508 | + "a-sha-1", |
| 509 | + "parent-sha-1", |
| 510 | + 42, |
| 511 | + created_at, |
| 512 | + BenchmarkRequestStatus::WaitingForParent, |
| 513 | + "llvm", |
| 514 | + "", |
| 515 | + ); |
| 516 | + let master_benchmark_request_two = BenchmarkRequest::create_master( |
| 517 | + "a-sha-2", |
| 518 | + "parent-sha-1", |
| 519 | + 42, |
| 520 | + created_at, |
| 521 | + BenchmarkRequestStatus::Completed, |
| 522 | + "llvm", |
| 523 | + "", |
| 524 | + ); |
| 525 | + let master_benchmark_request_three = BenchmarkRequest::create_master( |
| 526 | + "a-sha-3", |
| 527 | + "parent-sha-3", |
| 528 | + 42, |
| 529 | + created_at, |
| 530 | + BenchmarkRequestStatus::InProgress, |
| 531 | + "llvm", |
| 532 | + "", |
| 533 | + ); |
| 534 | + |
| 535 | + let try_benchmark_request = BenchmarkRequest::create_try( |
| 536 | + "b-sha-2", |
| 537 | + "parent-sha-2", |
| 538 | + 32, |
| 539 | + old_created_at, |
| 540 | + BenchmarkRequestStatus::Completed, |
| 541 | + "cranelift", |
| 542 | + "", |
| 543 | + ); |
| 544 | + |
| 545 | + let release_benchmark_request = BenchmarkRequest::create_release( |
| 546 | + "1.8.0", |
| 547 | + old_created_at, |
| 548 | + BenchmarkRequestStatus::WaitingForParent, |
| 549 | + "cranelift,llvm", |
| 550 | + "", |
| 551 | + ); |
| 552 | + |
| 553 | + let db = db.connection().await; |
| 554 | + db.insert_benchmark_request(&master_benchmark_request).await; |
| 555 | + db.insert_benchmark_request(&master_benchmark_request_two) |
| 556 | + .await; |
| 557 | + db.insert_benchmark_request(&master_benchmark_request_three) |
| 558 | + .await; |
| 559 | + db.insert_benchmark_request(&try_benchmark_request).await; |
| 560 | + db.insert_benchmark_request(&release_benchmark_request) |
| 561 | + .await; |
| 562 | + |
| 563 | + let requests = db |
| 564 | + .get_benchmark_requests_by_status( |
| 565 | + &[ |
| 566 | + BenchmarkRequestStatus::WaitingForParent, |
| 567 | + BenchmarkRequestStatus::InProgress, |
| 568 | + BenchmarkRequestStatus::Completed, |
| 569 | + ], |
| 570 | + Some(cutoff_days), |
| 571 | + ) |
| 572 | + .await; |
| 573 | + |
| 574 | + assert_eq!(requests.len(), 3); |
| 575 | + |
| 576 | + Ok(ctx) |
| 577 | + }) |
| 578 | + .await; |
| 579 | + } |
| 580 | + |
| 581 | + #[tokio::test] |
| 582 | + async fn update_benchmark_request_status() { |
| 583 | + // Insert one item into the database, change the status and then |
| 584 | + // get the item back out again to ensure it has changed status |
| 585 | + run_postgres_test(|ctx| async { |
| 586 | + let db = ctx.db_client(); |
| 587 | + let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap(); |
| 588 | + let master_benchmark_request = BenchmarkRequest::create_master( |
| 589 | + "a-sha-1", |
| 590 | + "parent-sha-1", |
| 591 | + 42, |
| 592 | + time, |
| 593 | + BenchmarkRequestStatus::WaitingForParent, |
| 594 | + "llvm", |
| 595 | + "", |
| 596 | + ); |
| 597 | + |
| 598 | + let mut db = db.connection().await; |
| 599 | + db.insert_benchmark_request(&master_benchmark_request).await; |
| 600 | + |
| 601 | + db.update_benchmark_request_status( |
| 602 | + &master_benchmark_request, |
| 603 | + BenchmarkRequestStatus::InProgress, |
| 604 | + ) |
| 605 | + .await; |
| 606 | + |
| 607 | + let requests = db |
| 608 | + .get_benchmark_requests_by_status(&[BenchmarkRequestStatus::InProgress], None) |
| 609 | + .await; |
| 610 | + |
| 611 | + assert_eq!(requests.len(), 1); |
| 612 | + assert_eq!(requests[0].tag(), master_benchmark_request.tag()); |
| 613 | + assert_eq!(requests[0].status, BenchmarkRequestStatus::InProgress); |
| 614 | + |
| 615 | + Ok(ctx) |
| 616 | + }) |
| 617 | + .await; |
| 618 | + } |
427 | 619 | } |
0 commit comments