|
21 | 21 | from crawlee.enqueue_strategy import EnqueueStrategy |
22 | 22 | from crawlee.errors import SessionError, UserDefinedErrorHandlerError |
23 | 23 | from crawlee.models import BaseRequestData, Request |
| 24 | +from crawlee.statistics.models import FinalStatistics |
24 | 25 | from crawlee.storages import Dataset, KeyValueStore, RequestList, RequestQueue |
25 | 26 | from crawlee.types import AddRequestsKwargs, BasicCrawlingContext, HttpHeaders |
26 | 27 |
|
@@ -638,3 +639,51 @@ async def handler(context: BasicCrawlingContext) -> None: |
638 | 639 |
|
639 | 640 | datasets_path = Path(configuration.storage_dir) / 'datasets' / 'default' |
640 | 641 | assert not datasets_path.exists() or list(datasets_path.iterdir()) == [] |
| 642 | + |
| 643 | + |
| 644 | +async def test_logs_final_statistics(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None: |
| 645 | + crawler = BasicCrawler(configure_logging=False) |
| 646 | + |
| 647 | + @crawler.router.default_handler |
| 648 | + async def handler(context: BasicCrawlingContext) -> None: |
| 649 | + await context.push_data({'something': 'something'}) |
| 650 | + |
| 651 | + fake_statistics = FinalStatistics( |
| 652 | + requests_finished=4, |
| 653 | + requests_failed=33, |
| 654 | + retry_histogram=[1, 4, 8], |
| 655 | + request_avg_failed_duration=timedelta(seconds=99), |
| 656 | + request_avg_finished_duration=timedelta(milliseconds=483), |
| 657 | + requests_finished_per_minute=0.33, |
| 658 | + requests_failed_per_minute=0.1, |
| 659 | + request_total_duration=timedelta(minutes=12), |
| 660 | + requests_total=37, |
| 661 | + crawler_runtime=timedelta(minutes=5), |
| 662 | + ) |
| 663 | + |
| 664 | + monkeypatch.setattr(crawler._statistics, 'calculate', lambda: fake_statistics) |
| 665 | + |
| 666 | + result = await crawler.run() |
| 667 | + assert result is fake_statistics |
| 668 | + |
| 669 | + final_statistics = next( |
| 670 | + (record for record in caplog.records if record.msg.startswith('Final')), |
| 671 | + None, |
| 672 | + ) |
| 673 | + |
| 674 | + assert final_statistics is not None |
| 675 | + assert final_statistics.msg.splitlines() == [ |
| 676 | + 'Final request statistics:', |
| 677 | + '┌───────────────────────────────┬───────────┐', |
| 678 | + '│ requests_finished │ 4 │', |
| 679 | + '│ requests_failed │ 33 │', |
| 680 | + '│ retry_histogram │ [1, 4, 8] │', |
| 681 | + '│ request_avg_failed_duration │ 99.0 │', |
| 682 | + '│ request_avg_finished_duration │ 0.483 │', |
| 683 | + '│ requests_finished_per_minute │ 0.33 │', |
| 684 | + '│ requests_failed_per_minute │ 0.1 │', |
| 685 | + '│ request_total_duration │ 720.0 │', |
| 686 | + '│ requests_total │ 37 │', |
| 687 | + '│ crawler_runtime │ 300.0 │', |
| 688 | + '└───────────────────────────────┴───────────┘', |
| 689 | + ] |
0 commit comments