Skip to content

Instantly share code, notes, and snippets.

@Tishka17
Last active October 10, 2024 20:11
Show Gist options
  • Save Tishka17/d51e977a2c42b55712fee7772fac2dbd to your computer and use it in GitHub Desktop.
Save Tishka17/d51e977a2c42b55712fee7772fac2dbd to your computer and use it in GitHub Desktop.
dishka benchmark
from enum import auto
from dishka import BaseScope
try:
from dishka.entities.scope import new_scope
except ImportError:
new_scope = str
class B:
def __init__(self, x: int):
self.x = x
def __repr__(self):
return f"<B:{self.x}>"
class CAAAAA:
def __init__(self):
pass
class CAAAA:
def __init__(self, x: CAAAAA):
pass
class CAAA:
def __init__(self, x: CAAAA):
pass
class CAA:
def __init__(self, x: CAAA):
pass
class CA:
def __init__(self, x: CAA):
pass
class C:
def __init__(self, x: CA):
pass
class A:
def __init__(self, b: B, c: C):
self.b = b
self.c = c
def __repr__(self):
return f"<<A:{self.b}-{self.c}>>"
class A1(A):
pass
class MyScope(BaseScope):
R = new_scope("R", skip=True)
APP = new_scope("APP")
SESSION = new_scope("SESSION", skip=True)
REQUEST = new_scope("REQUEST")
NUMBER = 1000000
from classes import *
from dishka import Provider, provide
from dishka.container import make_container
class MyProvider(Provider):
a = provide(A1, scope=MyScope.REQUEST, provides=A)
c1 = provide(CA, scope=MyScope.REQUEST)
c2 = provide(CAA, scope=MyScope.REQUEST)
c3 = provide(CAAA, scope=MyScope.REQUEST)
c4 = provide(CAAAA, scope=MyScope.REQUEST)
c5 = provide(CAAAAA, scope=MyScope.REQUEST)
@provide(scope=MyScope.REQUEST)
def make_b(self) -> B:
return B(2)
@provide(scope=MyScope.REQUEST)
def make_c(self, x: CA) -> C:
return C(x)
def main():
container = make_container(MyProvider(), scopes=MyScope)
for x in range(NUMBER):
with container() as state:
state.get(A)
if __name__ == '__main__':
main()
from classes import *
def main():
for x in range(NUMBER*10):
a = A1(
b=B(2),
c=C(CA(CAA(CAAA(CAAAA(CAAAAA())))))
)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment