File: performance.py

package info (click to toggle)
python-pykka 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 508 kB
  • sloc: python: 2,813; makefile: 113
file content (71 lines) | stat: -rw-r--r-- 1,559 bytes parent folder | download | duplicates (3)
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
# ruff: noqa: T201

from __future__ import annotations

import time
from typing import Any, Callable

from pykka import ActorRegistry, ThreadingActor


def time_it(func: Callable[[], Any]) -> None:
    start = time.time()
    func()
    elapsed = time.time() - start
    print(f"{func.__name__!r} took {elapsed:.3f}s")


class SomeObject:
    pykka_traversable = False
    cat = "bar.cat"

    def func(self) -> None:
        pass


class AnActor(ThreadingActor):
    bar = SomeObject()
    bar.pykka_traversable = True

    foo = "foo"

    def __init__(self) -> None:
        super().__init__()
        self.cat = "quox"

    def func(self) -> None:
        pass


def test_direct_plain_attribute_access() -> None:
    actor = AnActor.start().proxy()
    for _ in range(10000):
        actor.foo.get()


def test_direct_callable_attribute_access() -> None:
    actor = AnActor.start().proxy()
    for _ in range(10000):
        actor.func().get()


def test_traversable_plain_attribute_access() -> None:
    actor = AnActor.start().proxy()
    for _ in range(10000):
        actor.bar.cat.get()


def test_traversable_callable_attribute_access() -> None:
    actor = AnActor.start().proxy()
    for _ in range(10000):
        actor.bar.func().get()


if __name__ == "__main__":
    try:
        time_it(test_direct_plain_attribute_access)
        time_it(test_direct_callable_attribute_access)
        time_it(test_traversable_plain_attribute_access)
        time_it(test_traversable_callable_attribute_access)
    finally:
        ActorRegistry.stop_all()