dify - 💡(How to fix) Fix [Refactor/Chore] flaky test TestSchemaResolverClass.test_cache_performance

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

The test case TestSchemaResolverClass.test_cache_performance failed with the following error while running with other unit tests.

[gw6] darwin -- Python 3.12.12 /Users/qg/workspace/langgenius/dify/api/.venv/bin/python

self = <tests.unit_tests.core.schemas.test_resolver.TestSchemaResolverClass object at 0x129ecc890>

    def test_cache_performance(self):
        """Test that caching improves performance"""
        SchemaResolver.clear_cache()
    
        # Create a schema with many references to the same schema
        schema = {
            "type": "object",
            "properties": {
                f"prop_{i}": {"$ref": "https://dify.ai/schemas/v1/file.json"}
                for i in range(50)  # Reduced to avoid depth issues
            },
        }
    
        # First run (no cache) - run multiple times to warm up
        results1 = []
        for _ in range(3):
            SchemaResolver.clear_cache()
            start = time.perf_counter()
            result1 = resolve_dify_schema_refs(schema)
            time_no_cache = time.perf_counter() - start
            results1.append(time_no_cache)
    
        avg_time_no_cache = sum(results1) / len(results1)
    
        # Second run (with cache) - run multiple times
        # Warm up cache first
        resolve_dify_schema_refs(schema)
    
        results2 = []
        for _ in range(3):
            start = time.perf_counter()
            result2 = resolve_dify_schema_refs(schema)
            time_with_cache = time.perf_counter() - start
            results2.append(time_with_cache)
    
        avg_time_with_cache = sum(results2) / len(results2)
    
        # Cache should make it faster (more lenient check)
        assert result1 == result2
        # Cache should provide some performance benefit (allow for measurement variance)
        # We expect cache to be faster, but allow for small timing variations
        performance_ratio = avg_time_with_cache / avg_time_no_cache if avg_time_no_cache > 0 else 1.0
>       assert performance_ratio <= 2.0, f"Cache performance degraded too much: {performance_ratio}"
E       AssertionError: Cache performance degraded too much: 4.544141620163394
E       assert 4.544141620163394 <= 2.0

api/tests/unit_tests/core/schemas/test_resolver.py:516: AssertionError

However, when running separately, the test case passes without any issue.

I suspect that the performance highly depends on the resource utilization of host OS. While the host cpu usage is high, the performance degrades and test fails.

Error Message

[gw6] darwin -- Python 3.12.12 /Users/qg/workspace/langgenius/dify/api/.venv/bin/python

self = <tests.unit_tests.core.schemas.test_resolver.TestSchemaResolverClass object at 0x129ecc890>

def test_cache_performance(self):
    """Test that caching improves performance"""
    SchemaResolver.clear_cache()

    # Create a schema with many references to the same schema
    schema = {
        "type": "object",
        "properties": {
            f"prop_{i}": {"$ref": "https://dify.ai/schemas/v1/file.json"}
            for i in range(50)  # Reduced to avoid depth issues
        },
    }

    # First run (no cache) - run multiple times to warm up
    results1 = []
    for _ in range(3):
        SchemaResolver.clear_cache()
        start = time.perf_counter()
        result1 = resolve_dify_schema_refs(schema)
        time_no_cache = time.perf_counter() - start
        results1.append(time_no_cache)

    avg_time_no_cache = sum(results1) / len(results1)

    # Second run (with cache) - run multiple times
    # Warm up cache first
    resolve_dify_schema_refs(schema)

    results2 = []
    for _ in range(3):
        start = time.perf_counter()
        result2 = resolve_dify_schema_refs(schema)
        time_with_cache = time.perf_counter() - start
        results2.append(time_with_cache)

    avg_time_with_cache = sum(results2) / len(results2)

    # Cache should make it faster (more lenient check)
    assert result1 == result2
    # Cache should provide some performance benefit (allow for measurement variance)
    # We expect cache to be faster, but allow for small timing variations
    performance_ratio = avg_time_with_cache / avg_time_no_cache if avg_time_no_cache > 0 else 1.0
  assert performance_ratio <= 2.0, f"Cache performance degraded too much: {performance_ratio}"

E AssertionError: Cache performance degraded too much: 4.544141620163394 E assert 4.544141620163394 <= 2.0

api/tests/unit_tests/core/schemas/test_resolver.py:516: AssertionError

Root Cause

The test case TestSchemaResolverClass.test_cache_performance failed with the following error while running with other unit tests.

[gw6] darwin -- Python 3.12.12 /Users/qg/workspace/langgenius/dify/api/.venv/bin/python

self = <tests.unit_tests.core.schemas.test_resolver.TestSchemaResolverClass object at 0x129ecc890>

    def test_cache_performance(self):
        """Test that caching improves performance"""
        SchemaResolver.clear_cache()
    
        # Create a schema with many references to the same schema
        schema = {
            "type": "object",
            "properties": {
                f"prop_{i}": {"$ref": "https://dify.ai/schemas/v1/file.json"}
                for i in range(50)  # Reduced to avoid depth issues
            },
        }
    
        # First run (no cache) - run multiple times to warm up
        results1 = []
        for _ in range(3):
            SchemaResolver.clear_cache()
            start = time.perf_counter()
            result1 = resolve_dify_schema_refs(schema)
            time_no_cache = time.perf_counter() - start
            results1.append(time_no_cache)
    
        avg_time_no_cache = sum(results1) / len(results1)
    
        # Second run (with cache) - run multiple times
        # Warm up cache first
        resolve_dify_schema_refs(schema)
    
        results2 = []
        for _ in range(3):
            start = time.perf_counter()
            result2 = resolve_dify_schema_refs(schema)
            time_with_cache = time.perf_counter() - start
            results2.append(time_with_cache)
    
        avg_time_with_cache = sum(results2) / len(results2)
    
        # Cache should make it faster (more lenient check)
        assert result1 == result2
        # Cache should provide some performance benefit (allow for measurement variance)
        # We expect cache to be faster, but allow for small timing variations
        performance_ratio = avg_time_with_cache / avg_time_no_cache if avg_time_no_cache > 0 else 1.0
>       assert performance_ratio <= 2.0, f"Cache performance degraded too much: {performance_ratio}"
E       AssertionError: Cache performance degraded too much: 4.544141620163394
E       assert 4.544141620163394 <= 2.0

api/tests/unit_tests/core/schemas/test_resolver.py:516: AssertionError

However, when running separately, the test case passes without any issue.

I suspect that the performance highly depends on the resource utilization of host OS. While the host cpu usage is high, the performance degrades and test fails.

Code Example

[gw6] darwin -- Python 3.12.12 /Users/qg/workspace/langgenius/dify/api/.venv/bin/python

self = <tests.unit_tests.core.schemas.test_resolver.TestSchemaResolverClass object at 0x129ecc890>

    def test_cache_performance(self):
        """Test that caching improves performance"""
        SchemaResolver.clear_cache()
    
        # Create a schema with many references to the same schema
        schema = {
            "type": "object",
            "properties": {
                f"prop_{i}": {"$ref": "https://dify.ai/schemas/v1/file.json"}
                for i in range(50)  # Reduced to avoid depth issues
            },
        }
    
        # First run (no cache) - run multiple times to warm up
        results1 = []
        for _ in range(3):
            SchemaResolver.clear_cache()
            start = time.perf_counter()
            result1 = resolve_dify_schema_refs(schema)
            time_no_cache = time.perf_counter() - start
            results1.append(time_no_cache)
    
        avg_time_no_cache = sum(results1) / len(results1)
    
        # Second run (with cache) - run multiple times
        # Warm up cache first
        resolve_dify_schema_refs(schema)
    
        results2 = []
        for _ in range(3):
            start = time.perf_counter()
            result2 = resolve_dify_schema_refs(schema)
            time_with_cache = time.perf_counter() - start
            results2.append(time_with_cache)
    
        avg_time_with_cache = sum(results2) / len(results2)
    
        # Cache should make it faster (more lenient check)
        assert result1 == result2
        # Cache should provide some performance benefit (allow for measurement variance)
        # We expect cache to be faster, but allow for small timing variations
        performance_ratio = avg_time_with_cache / avg_time_no_cache if avg_time_no_cache > 0 else 1.0
>       assert performance_ratio <= 2.0, f"Cache performance degraded too much: {performance_ratio}"
E       AssertionError: Cache performance degraded too much: 4.544141620163394
E       assert 4.544141620163394 <= 2.0

api/tests/unit_tests/core/schemas/test_resolver.py:516: AssertionError
RAW_BUFFERClick to expand / collapse

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for refactors or chores; if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Description

The test case TestSchemaResolverClass.test_cache_performance failed with the following error while running with other unit tests.

[gw6] darwin -- Python 3.12.12 /Users/qg/workspace/langgenius/dify/api/.venv/bin/python

self = <tests.unit_tests.core.schemas.test_resolver.TestSchemaResolverClass object at 0x129ecc890>

    def test_cache_performance(self):
        """Test that caching improves performance"""
        SchemaResolver.clear_cache()
    
        # Create a schema with many references to the same schema
        schema = {
            "type": "object",
            "properties": {
                f"prop_{i}": {"$ref": "https://dify.ai/schemas/v1/file.json"}
                for i in range(50)  # Reduced to avoid depth issues
            },
        }
    
        # First run (no cache) - run multiple times to warm up
        results1 = []
        for _ in range(3):
            SchemaResolver.clear_cache()
            start = time.perf_counter()
            result1 = resolve_dify_schema_refs(schema)
            time_no_cache = time.perf_counter() - start
            results1.append(time_no_cache)
    
        avg_time_no_cache = sum(results1) / len(results1)
    
        # Second run (with cache) - run multiple times
        # Warm up cache first
        resolve_dify_schema_refs(schema)
    
        results2 = []
        for _ in range(3):
            start = time.perf_counter()
            result2 = resolve_dify_schema_refs(schema)
            time_with_cache = time.perf_counter() - start
            results2.append(time_with_cache)
    
        avg_time_with_cache = sum(results2) / len(results2)
    
        # Cache should make it faster (more lenient check)
        assert result1 == result2
        # Cache should provide some performance benefit (allow for measurement variance)
        # We expect cache to be faster, but allow for small timing variations
        performance_ratio = avg_time_with_cache / avg_time_no_cache if avg_time_no_cache > 0 else 1.0
>       assert performance_ratio <= 2.0, f"Cache performance degraded too much: {performance_ratio}"
E       AssertionError: Cache performance degraded too much: 4.544141620163394
E       assert 4.544141620163394 <= 2.0

api/tests/unit_tests/core/schemas/test_resolver.py:516: AssertionError

However, when running separately, the test case passes without any issue.

I suspect that the performance highly depends on the resource utilization of host OS. While the host cpu usage is high, the performance degrades and test fails.

Motivation

No response

Additional Context

No response

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

dify - 💡(How to fix) Fix [Refactor/Chore] flaky test TestSchemaResolverClass.test_cache_performance