def test_import_time(catalog, test_session):
    """
    Outside of the test, you can measure the import time with:
        python -Ximporttime -c 'import datachain'

    To visualize the import profile, consider using `tuna`: https://github.com/nschloe/tuna.
    """
    import_timings = []
    for attempt in range(MAX_ATTEMPTS):
        chain = _import_time_chain(test_session)
        (import_time_ms,) = chain.filter(
            dc.C("import") == "datachain",
        ).collect("cumulative_ms")
        import_timings.append((chain, import_time_ms))
        # pass `--log-cli-level=info` to see these logs live
        logger.info("attempt %d, import time: %dms", attempt + 1, import_time_ms)

    chain, min_import_time = min(import_timings, key=lambda x: x[1])
    # If there is a regression, uncomment the following to find the culprit:
    # dc.show(limit=40)
    for module in lazy_modules:
        assert not list(chain.filter(dc.C("import").startswith(module)).collect()), (
            f"found {module} at import time"
        )
    assert min_import_time < MAX_IMPORT_TIME_MS, (
        f"Possible import time regression; took {min_import_time}ms"
    )

    Outside of the test, you can measure the import time with:
        python -Ximporttime -c 'import datachain'

    To visualize the import profile, consider using `tuna`: https://github.com/nschloe/tuna.
    
tests/test_import_time.py