File: test_benchmark.py

package info (click to toggle)
python-promise 2.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 400 kB
  • sloc: python: 2,681; sh: 13; makefile: 4
file content (116 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
from pytest import raises
import time
from promise import Promise, promisify, is_thenable


def test_benchmark_promise_creation(benchmark):
    @benchmark
    def create_promise():  # unnecessary function call
        p = Promise()


def test_benchmark_promise_resolve(benchmark):
    def create_promise():
        return Promise.resolve(True)

    result = benchmark(create_promise).get()
    assert result == True


def test_benchmark_is_thenable_basic_type(benchmark):
    def create_promise():
        return is_thenable(True)

    result = benchmark(create_promise)
    assert result == False


def test_benchmark_is_thenable_custom_type(benchmark):
    class MyType(object):
        pass

    my_type_instance = MyType()

    def create_promise():
        return is_thenable(my_type_instance)

    result = benchmark(create_promise)
    assert result == False


def test_benchmark_promise_creation_with_resolve(benchmark):
    do_resolve = lambda resolve, reject: resolve(True)

    def create_promise():  # unnecessary function call
        p = Promise(do_resolve)
        # p._wait()
        return p

    result = benchmark(create_promise).get()
    assert result == True


def test_benchmark_promise_creation_with_reject(benchmark):
    do_resolve = lambda resolve, reject: reject(Exception("Error"))

    def create_promise():  # unnecessary function call
        p = Promise(do_resolve)
        # p._wait()
        return p

    with raises(Exception) as exc_info:
        result = benchmark(create_promise).get()

    assert str(exc_info.value) == "Error"


# def test_benchmark_promisify_promise(benchmark):
#     instance = Promise()

#     def create_promise():  # unnecessary function call
#         return promisify(instance)

#     result = benchmark(create_promise)

#     assert isinstance(result, Promise)


def test_benchmark_promisify_custom_type(benchmark):
    class CustomThenable(object):
        pass
        # def then(self, resolve, reject):
        #     return resolve(True)

    instance = CustomThenable()

    def create_promise():  # unnecessary function call
        return Promise.resolve(instance)

    result = benchmark(create_promise)

    assert isinstance(result, Promise)
    assert result.get() == instance


def test_benchmark_promise_all(benchmark):
    values = range(1000)

    def create_promise():  # unnecessary function call
        return Promise.all(values)

    result = benchmark(create_promise)

    assert isinstance(result, Promise)
    assert result.get() == list(range(1000))


def test_benchmark_promise_all_promise(benchmark):
    values = [Promise.resolve(i) for i in range(100000)]

    def create_promise():  # unnecessary function call
        return Promise.all(values)

    result = benchmark(create_promise)

    assert isinstance(result, Promise)
    assert result.get() == list(range(100000))