| 1 |
import unittest |
| 2 |
|
| 3 |
from test.support import threading_helper |
| 4 |
|
| 5 |
import cProfile |
| 6 |
import pstats |
| 7 |
|
| 8 |
|
| 9 |
NTHREADS = 10 |
| 10 |
INSERT_PER_THREAD = 1000 |
| 11 |
|
| 12 |
|
| 13 |
@threading_helper.requires_working_threading() |
| 14 |
class TestCProfile(unittest.TestCase): |
| 15 |
def test_cprofile_racing_list_insert(self): |
| 16 |
def list_insert(lst): |
| 17 |
for i in range(INSERT_PER_THREAD): |
| 18 |
lst.insert(0, i) |
| 19 |
|
| 20 |
lst = [] |
| 21 |
|
| 22 |
with cProfile.Profile() as pr: |
| 23 |
threading_helper.run_concurrently( |
| 24 |
worker_func=list_insert, nthreads=NTHREADS, args=(lst,) |
| 25 |
) |
| 26 |
pr.create_stats() |
| 27 |
ps = pstats.Stats(pr) |
| 28 |
stats_profile = ps.get_stats_profile() |
| 29 |
list_insert_profile = stats_profile.func_profiles[ |
| 30 |
"<method 'insert' of 'list' objects>" |
| 31 |
] |
| 32 |
# Even though there is no explicit recursive call to insert, |
| 33 |
# cProfile may record some calls as recursive due to limitations |
| 34 |
# in its handling of multithreaded programs. This issue is not |
| 35 |
# directly related to FT Python itself; however, it tends to be |
| 36 |
# more noticeable when using FT Python. Therefore, consider only |
| 37 |
# the calls section and disregard the recursive part. |
| 38 |
list_insert_ncalls = list_insert_profile.ncalls.split("/")[0] |
| 39 |
self.assertEqual( |
| 40 |
int(list_insert_ncalls), NTHREADS * INSERT_PER_THREAD |
| 41 |
) |
| 42 |
|
| 43 |
self.assertEqual(len(lst), NTHREADS * INSERT_PER_THREAD) |