-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_script.py
176 lines (116 loc) · 3.67 KB
/
test_script.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import blogpost_ffi
import pyarrow as pa
import time
import tracing
from opentelemetry.trace.propagation.tracecontext import (
TraceContextTextMapPropagator,
)
pa.array([])
value = [1] * 100_000_000
# Default benchmark
print(f"---Default Benchmark---")
start_time = time.time()
array = blogpost_ffi.create_list(value)
print(f"default: {time.time() - start_time:.3f}s")
assert len(array) == 100_000_000, "Not enough data"
assert array[-1] == 1, "data value is wrong"
print(f"------")
print()
# PyBytes benchmark
print(f"---PyBytes Benchmark---")
value_bytes = bytes(value)
start_time = time.time()
array = blogpost_ffi.create_list_bytes(value_bytes)
print(f"bytes: {time.time() - start_time:.3f}s")
assert len(array) == 100_000_000, "Not enough data"
assert array[-1] == 1, "Value is wrong"
print(f"------")
print()
# Arrow benchmark
print(f"---Arrow Benchmark---")
value_arrow = pa.array(value, type=pa.uint8())
start_time = time.time()
array = blogpost_ffi.create_list_arrow(value_arrow)
print(f"arrow: {time.time() - start_time:.3f}s")
assert len(array) == 100_000_000, "Not enough data"
assert array[-1].as_py() == 1, f"Value is wrong. Expected: 1, got: {array[-1]}"
print(f"------")
print()
# Debugging eyre
print(f"---Eyre error---")
# Try:
# array = blogpost_ffi.create_list_arrow(1)
#
## This error panics the whole program and is therefore uncatchable.
ERROR_WITHOUT_EYRE = """
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: PyErr { type: <class 'TypeError'>, value: TypeError('Expected instance of pyarrow.lib.Array, got builtins.int'), traceback: None }', src/lib.rs:45:62
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
File "/home/peter/Documents/work/blogpost_ffi/test_script.py", line 79, in <module>
array = blogpost_ffi.create_list_arrow(1)
pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value: PyErr { type: <class 'TypeError'>, value: TypeError('Expected instance of pyarrow.lib.Array, got builtins.int'), traceback: None }
"""
try:
array = blogpost_ffi.create_list_arrow_eyre(1)
except Exception as e:
print(f"eyre says: {e}")
print(f"------")
print()
# Eyre default
print(f"---Eyre no traceback---")
def abc():
assert False, "I have no idea what is wrong"
try:
array = blogpost_ffi.call_func_eyre(abc)
except Exception as e:
print(f"eyre no traceback says: {e}")
print(f"------")
print()
# Eyre traceback
print(f"---Eyre traceback---")
def abc():
assert False, "I have no idea what is wrong"
try:
array = blogpost_ffi.call_func_eyre_traceback(abc)
except Exception as e:
print(f"eyre traceback says: {e}")
print(f"------")
print()
# Unbounded Memory Growth
print(f"---Memory Growth---")
print(f"-->Open a Memory analyzer")
array = blogpost_ffi.unbounded_memory_growth()
print(f"------")
print()
# Unbounded Memory Growth
print(f"---Memory Growth---")
print(f"-->Open a Memory analyzer")
array = blogpost_ffi.bounded_memory_growth()
print(f"------")
print()
# GIL Lock
print(f"---GIL Lock---")
array = blogpost_ffi.gil_lock()
print(f"------")
print()
# GIL unlock
print(f"---GIL Lock---")
array = blogpost_ffi.gil_unlock()
print(f"------")
print()
# Global tracing
print(f"---Global tracing---")
def abc(cx):
propagator = TraceContextTextMapPropagator()
context = propagator.extract(carrier=cx)
with tracing.tracer.start_as_current_span(
name="Python_span", context=context
) as child_span:
child_span.add_event("in Python!")
output = {}
tracing.propagator.inject(output)
time.sleep(2)
return output
array = blogpost_ffi.global_tracing(abc)
print(f"------")
print()