File: test_execute.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (111 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import asyncio
import datetime
import random
from datetime import date
from typing import cast

import pytest
from pytest_codspeed.plugin import BenchmarkFixture

import strawberry
from strawberry.scalars import ID


@pytest.mark.benchmark
def test_execute(benchmark: BenchmarkFixture):
    birthday = datetime.datetime.now()
    pets = ("cat", "shark", "dog", "lama")

    @strawberry.type
    class Pet:
        id: int
        name: str

    @strawberry.type
    class Patron:
        id: int
        name: str
        age: int
        birthday: date
        tags: list[str]

        @strawberry.field
        def pets(self) -> list[Pet]:
            return [
                Pet(
                    id=i,
                    name=random.choice(pets),  # noqa: S311
                )
                for i in range(5)
            ]

    @strawberry.type
    class Query:
        @strawberry.field
        def patrons(self) -> list[Patron]:
            return [
                Patron(
                    id=i,
                    name="Patrick",
                    age=100,
                    birthday=birthday,
                    tags=["go", "ajax"],
                )
                for i in range(1000)
            ]

    schema = strawberry.Schema(query=Query)

    query = """
        query something{
          patrons {
            id
            name
            age
            birthday
            tags
            pets {
                id
                name
            }
          }
        }
    """

    def run():
        return asyncio.run(schema.execute(query))

    benchmark(run)


@pytest.mark.parametrize("ntypes", [2**k for k in range(0, 13, 4)])
def test_interface_performance(benchmark: BenchmarkFixture, ntypes: int):
    @strawberry.interface
    class Item:
        id: ID

    CONCRETE_TYPES: list[type[Item]] = [
        strawberry.type(type(f"Item{i}", (Item,), {})) for i in range(ntypes)
    ]

    @strawberry.type
    class Query:
        items: list[Item]

    schema = strawberry.Schema(query=Query, types=CONCRETE_TYPES)
    query = "query { items { id } }"

    def run():
        return asyncio.run(
            schema.execute(
                query,
                root_value=Query(
                    items=[
                        CONCRETE_TYPES[i % ntypes](id=cast("ID", i))
                        for i in range(1000)
                    ]
                ),
            )
        )

    benchmark(run)