-
Notifications
You must be signed in to change notification settings - Fork 44
/
test_multiprocessing.py
234 lines (178 loc) · 6.6 KB
/
test_multiprocessing.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from __future__ import division, print_function
from concurrent import futures
import logging
import math
import multiprocessing
import os
import pickle
import platform
import pprint
import random
import time
import timeit
import unittest
import cloudpickle
import pysparkling
class Processor(object):
"""This modifies lines but also keeps track whether it was executed."""
def __init__(self):
self.executed = False
def indent_line(self, line):
self.executed = True
return '--- {}'.format(line)
class LazyTestInjection(object):
def lazy_execution_test(self):
r = self.sc.textFile(__file__) # pylint: disable=no-member
processor = Processor()
r = r.map(processor.indent_line)
self.assertFalse(processor.executed) # pylint: disable=no-member
r = r.map(processor.indent_line).cache()
self.assertFalse(processor.executed) # pylint: disable=no-member
r = r.map(processor.indent_line)
r.collect()
self.assertTrue(processor.executed) # pylint: disable=no-member
class Multiprocessing(unittest.TestCase):
def setUp(self):
self.pool = multiprocessing.Pool(4)
self.sc = pysparkling.Context(pool=self.pool,
serializer=cloudpickle.dumps,
deserializer=pickle.loads)
def test_basic(self):
my_rdd = self.sc.parallelize([1, 3, 4])
r = my_rdd.map(lambda x: x ** 2).collect()
self.assertIn(16, r)
def test_first(self):
my_rdd = self.sc.parallelize([1, 2, 2, 4, 1, 3, 5, 9], 3)
self.assertEqual(my_rdd.first(), 1)
def tearDown(self):
self.pool.close()
def square_op(x):
return x ** 2
class MultiprocessingWithoutCloudpickle(unittest.TestCase):
def setUp(self):
self.pool = multiprocessing.Pool(4)
self.sc = pysparkling.Context(pool=self.pool)
def test_basic(self):
my_rdd = self.sc.parallelize([1, 3, 4])
r = my_rdd.map(square_op).collect()
self.assertIn(16, r)
def tearDown(self):
self.pool.close()
class NotParallel(unittest.TestCase, LazyTestInjection):
"""Test cases in the spirit of the parallel test cases for reference."""
def setUp(self):
self.sc = pysparkling.Context()
class ThreadPool(unittest.TestCase, LazyTestInjection):
def setUp(self):
self.pool = futures.ThreadPoolExecutor(4)
self.sc = pysparkling.Context(pool=self.pool)
def tearDown(self):
self.pool.shutdown()
def test_basic(self):
r = self.sc.parallelize([1, 3, 4]).map(math.sqrt).collect()
self.assertIn(2, r)
class ProcessPool(unittest.TestCase): # cannot work here: LazyTestInjection):
def setUp(self):
self.pool = futures.ProcessPoolExecutor(4)
self.sc = pysparkling.Context(pool=self.pool,
serializer=cloudpickle.dumps,
deserializer=pickle.loads)
def tearDown(self):
self.pool.shutdown()
def test_basic(self):
r = self.sc.parallelize([1, 3, 4]).map(math.sqrt).collect()
self.assertIn(2, r)
def test_zipWithIndex(self):
"""Prevent regression in zipWithIndex().
Test the case of parallelizing data directly form toLocalIterator()
in the multiprocessing case.
"""
r = (self.sc
.parallelize([1, 3, 4, 9, 15, 25, 50, 75, 100], 3)
.zipWithIndex()
.collect())
self.assertIn((4, 2), r)
def test_cache(self):
r = self.sc.parallelize(range(3), 3)
def sleep05(v):
time.sleep(0.5)
return v
r = r.map(sleep05).cache()
self.assertEqual(r.collect(), [0, 1, 2])
start = time.time()
r.collect()
self.assertLess(time.time() - start, 0.5)
class ProcessPoolIdlePerformance(unittest.TestCase):
"""Idle performance tests.
The "load" on these tests are sleeps.
"""
def runtime(self, n=10, processes=1):
start = time.time()
with futures.ProcessPoolExecutor(processes) as pool:
sc = pysparkling.Context(pool=pool,
serializer=cloudpickle.dumps,
deserializer=pickle.loads)
rdd = sc.parallelize(range(n), 10)
rdd.map(lambda _: time.sleep(0.1)).collect()
return time.time() - start
@unittest.skipIf(platform.python_implementation() == 'PyPy',
'test fails in PyPy')
def test_basic(self):
t1 = self.runtime(processes=1)
t10 = self.runtime(processes=10)
self.assertLess(t10, t1 / 2.0)
# pickle-able map function
def map1(ft):
return [random.choice(ft[1].split()) for _ in range(1000)]
def map_pi(n):
return sum((
1 for x in (random.random() ** 2 + random.random() ** 2
for _ in range(n))
if x < 1.0
))
@unittest.skipIf(os.getenv('PERFORMANCE') is None,
'PERFORMANCE env variable not set')
def test_performance():
# not pickle-able map function
# def map2(ft):
# return [random.choice(ft[1].split()) for _ in range(1000)]
def create_context(n_processes=0):
if not n_processes:
return pysparkling.Context()
pool = futures.ProcessPoolExecutor(n_processes)
return pysparkling.Context(pool=pool,
serializer=cloudpickle.dumps,
# serializer=pickle.dumps,
deserializer=pickle.loads)
def test(n_processes):
sc = create_context(n_processes)
timed = timeit.Timer(
lambda: sc.parallelize(
[1000 for _ in range(100)],
100,
).map(map_pi).collect()
).timeit(number=10)
return (timed, sc._stats)
print('starting processing')
n_cpu = multiprocessing.cpu_count()
test_results = {}
for n in range(int(n_cpu * 1.5 + 1)):
test_results[n] = test(n)
print(n, test_results[n][0])
print('results where running on one core with full serialization is 1.0:')
pprint.pprint({
n: 1.0 / (v[0] / test_results[1][0]) for n, v in test_results.items()
})
print('time spent where:')
pprint.pprint({
n: {k: '{:.1%}'.format(t / v[1]['map_exec']) for k, t in v[1].items()}
for n, v in test_results.items()
})
return (n_cpu, test_results)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
# test_performance()
t = ProcessPool()
t.setUp()
t.test_cache()
t.tearDown()