|
9 | 9 |
|
10 | 10 | using namespace NActors;
|
11 | 11 |
|
12 |
| -#define VALUES_EQUAL(a, b, ...) \ |
13 |
| - UNIT_ASSERT_VALUES_EQUAL_C((a), (b), (i64)semaphore.OldSemaphore \ |
14 |
| - << ' ' << (i64)semaphore.CurrentSleepThreadCount \ |
15 |
| - << ' ' << (i64)semaphore.CurrentThreadCount __VA_ARGS__); |
16 |
| - |
17 | 12 | ////////////////////////////////////////////////////////////////////////////////
|
18 | 13 |
|
19 | 14 | struct TEvMsg : public NActors::TEventLocal<TEvMsg, 10347> {
|
@@ -90,191 +85,8 @@ THolder<TActorSystemSetup> GetActorSystemSetup(TBasicExecutorPool* pool)
|
90 | 85 | return setup;
|
91 | 86 | }
|
92 | 87 |
|
93 |
| -Y_UNIT_TEST_SUITE(WaitingBenchs) { |
94 |
| - |
95 |
| - Y_UNIT_TEST(SpinPause) { |
96 |
| - const ui32 count = 1'000'000; |
97 |
| - ui64 startTs = GetCycleCountFast(); |
98 |
| - for (ui32 idx = 0; idx < count; ++idx) { |
99 |
| - SpinLockPause(); |
100 |
| - } |
101 |
| - ui64 stopTs = GetCycleCountFast(); |
102 |
| - Cerr << Ts2Us(stopTs - startTs) / count << Endl; |
103 |
| - Cerr << double(stopTs - startTs) / count << Endl; |
104 |
| - } |
105 |
| - |
106 |
| - struct TThread : public ISimpleThread { |
107 |
| - static const ui64 CyclesInMicroSecond; |
108 |
| - std::array<ui64, 128> Hist; |
109 |
| - ui64 WakingTime = 0; |
110 |
| - ui64 AwakeningTime = 0; |
111 |
| - ui64 SleepTime = 0; |
112 |
| - ui64 IterationCount = 0; |
113 |
| - |
114 |
| - std::atomic<ui64> Awakens = 0; |
115 |
| - std::atomic<ui64> *OtherAwaken; |
116 |
| - |
117 |
| - TThreadParkPad OwnPad; |
118 |
| - TThreadParkPad *OtherPad; |
119 |
| - |
120 |
| - bool IsWaiting = false; |
121 |
| - |
122 |
| - void GoToWait() { |
123 |
| - ui64 start = GetCycleCountFast(); |
124 |
| - OwnPad.Park(); |
125 |
| - ui64 elapsed = GetCycleCountFast() - start; |
126 |
| - AwakeningTime += elapsed; |
127 |
| - ui64 idx = std::min(Hist.size() - 1, (elapsed - 20 * CyclesInMicroSecond) / CyclesInMicroSecond); |
128 |
| - Hist[idx]++; |
129 |
| - Awakens++; |
130 |
| - } |
131 |
| - |
132 |
| - void GoToWakeUp() { |
133 |
| - ui64 start = GetCycleCountFast(); |
134 |
| - OtherPad->Unpark(); |
135 |
| - ui64 elapsed = GetCycleCountFast() - start; |
136 |
| - WakingTime += elapsed; |
137 |
| - ui64 idx = std::min(Hist.size() - 1, elapsed / CyclesInMicroSecond); |
138 |
| - Hist[idx]++; |
139 |
| - } |
140 |
| - |
141 |
| - void GoToSleep() { |
142 |
| - ui64 start = GetCycleCountFast(); |
143 |
| - ui64 stop = start; |
144 |
| - while (stop - start < 20 * CyclesInMicroSecond) { |
145 |
| - SpinLockPause(); |
146 |
| - stop = GetCycleCountFast(); |
147 |
| - } |
148 |
| - SleepTime += stop - start; |
149 |
| - } |
150 |
| - |
151 |
| - void* ThreadProc() { |
152 |
| - for (ui32 idx = 0; idx < IterationCount; ++idx) { |
153 |
| - if (IsWaiting) { |
154 |
| - GoToWait(); |
155 |
| - } else { |
156 |
| - GoToSleep(); |
157 |
| - GoToWakeUp(); |
158 |
| - while(OtherAwaken->load() == idx) { |
159 |
| - SpinLockPause(); |
160 |
| - } |
161 |
| - } |
162 |
| - } |
163 |
| - return nullptr; |
164 |
| - } |
165 |
| - }; |
166 |
| - |
167 |
| - const ui64 TThread::CyclesInMicroSecond = NHPTimer::GetCyclesPerSecond() * 0.000001; |
168 |
| - |
169 |
| - Y_UNIT_TEST(WakingUpTest) { |
170 |
| - TThread a, b; |
171 |
| - constexpr ui64 iterations = 100'000; |
172 |
| - std::fill(a.Hist.begin(), a.Hist.end(), 0); |
173 |
| - std::fill(b.Hist.begin(), b.Hist.end(), 0); |
174 |
| - a.IterationCount = iterations; |
175 |
| - b.IterationCount = iterations; |
176 |
| - a.IsWaiting = true; |
177 |
| - b.IsWaiting = false; |
178 |
| - b.OtherAwaken = &a.Awakens; |
179 |
| - a.OtherPad = &b.OwnPad; |
180 |
| - b.OtherPad = &a.OwnPad; |
181 |
| - a.Start(); |
182 |
| - b.Start(); |
183 |
| - a.Join(); |
184 |
| - b.Join(); |
185 |
| - |
186 |
| - ui64 awakeningTime = a.AwakeningTime + b.AwakeningTime - a.SleepTime - b.SleepTime; |
187 |
| - ui64 wakingUpTime = a.WakingTime + b.WakingTime; |
188 |
| - |
189 |
| - Cerr << "AvgAwakeningCycles: " << double(awakeningTime) / iterations << Endl; |
190 |
| - Cerr << "AvgAwakeningUs: " << Ts2Us(awakeningTime) / iterations << Endl; |
191 |
| - Cerr << "AvgSleep20usCycles:" << double(b.SleepTime) / iterations << Endl; |
192 |
| - Cerr << "AvgSleep20usUs:" << Ts2Us(b.SleepTime) / iterations << Endl; |
193 |
| - Cerr << "AvgWakingUpCycles: " << double(wakingUpTime) / iterations << Endl; |
194 |
| - Cerr << "AvgWakingUpUs: " << Ts2Us(wakingUpTime) / iterations << Endl; |
195 |
| - |
196 |
| - Cerr << "AwakeningHist:\n"; |
197 |
| - for (ui32 idx = 0; idx < a.Hist.size(); ++idx) { |
198 |
| - if (a.Hist[idx]) { |
199 |
| - if (idx + 1 != a.Hist.size()) { |
200 |
| - Cerr << " [" << idx << "us - " << idx + 1 << "us] " << a.Hist[idx] << Endl; |
201 |
| - } else { |
202 |
| - Cerr << " [" << idx << "us - ...] " << a.Hist[idx] << Endl; |
203 |
| - } |
204 |
| - } |
205 |
| - } |
206 |
| - |
207 |
| - Cerr << "WakingUpHist:\n"; |
208 |
| - for (ui32 idx = 0; idx < b.Hist.size(); ++idx) { |
209 |
| - if (b.Hist[idx]) { |
210 |
| - if (idx + 1 != b.Hist.size()) { |
211 |
| - Cerr << " [" << idx << "us - " << idx + 1 << "us] " << b.Hist[idx] << Endl; |
212 |
| - } else { |
213 |
| - Cerr << " [" << idx << "us - ...] " << b.Hist[idx] << Endl; |
214 |
| - } |
215 |
| - } |
216 |
| - } |
217 |
| - } |
218 |
| - |
219 |
| -} |
220 |
| - |
221 | 88 | Y_UNIT_TEST_SUITE(BasicExecutorPool) {
|
222 | 89 |
|
223 |
| - Y_UNIT_TEST(Semaphore) { |
224 |
| - TBasicExecutorPool::TSemaphore semaphore; |
225 |
| - semaphore = TBasicExecutorPool::TSemaphore::GetSemaphore(0); |
226 |
| - |
227 |
| - VALUES_EQUAL(0, semaphore.ConvertToI64()); |
228 |
| - semaphore = TBasicExecutorPool::TSemaphore::GetSemaphore(-1); |
229 |
| - VALUES_EQUAL(-1, semaphore.ConvertToI64()); |
230 |
| - semaphore = TBasicExecutorPool::TSemaphore::GetSemaphore(1); |
231 |
| - VALUES_EQUAL(1, semaphore.ConvertToI64()); |
232 |
| - |
233 |
| - for (i64 value = -1'000'000; value <= 1'000'000; ++value) { |
234 |
| - VALUES_EQUAL(TBasicExecutorPool::TSemaphore::GetSemaphore(value).ConvertToI64(), value); |
235 |
| - } |
236 |
| - |
237 |
| - for (i8 sleepThreads = -10; sleepThreads <= 10; ++sleepThreads) { |
238 |
| - |
239 |
| - semaphore = TBasicExecutorPool::TSemaphore(); |
240 |
| - semaphore.CurrentSleepThreadCount = sleepThreads; |
241 |
| - i64 initialValue = semaphore.ConvertToI64(); |
242 |
| - |
243 |
| - semaphore = TBasicExecutorPool::TSemaphore::GetSemaphore(initialValue - 1); |
244 |
| - VALUES_EQUAL(-1, semaphore.OldSemaphore); |
245 |
| - |
246 |
| - i64 value = initialValue; |
247 |
| - value -= 100; |
248 |
| - for (i32 expected = -100; expected <= 100; ++expected) { |
249 |
| - semaphore = TBasicExecutorPool::TSemaphore::GetSemaphore(value); |
250 |
| - UNIT_ASSERT_VALUES_EQUAL_C(expected, semaphore.OldSemaphore, (i64)semaphore.OldSemaphore |
251 |
| - << ' ' << (i64)semaphore.CurrentSleepThreadCount |
252 |
| - << ' ' << (i64)semaphore.CurrentThreadCount); |
253 |
| - UNIT_ASSERT_VALUES_EQUAL_C(sleepThreads, semaphore.CurrentSleepThreadCount, (i64)semaphore.OldSemaphore |
254 |
| - << ' ' << (i64)semaphore.CurrentSleepThreadCount |
255 |
| - << ' ' << (i64)semaphore.CurrentThreadCount); |
256 |
| - semaphore = TBasicExecutorPool::TSemaphore(); |
257 |
| - semaphore.OldSemaphore = expected; |
258 |
| - semaphore.CurrentSleepThreadCount = sleepThreads; |
259 |
| - UNIT_ASSERT_VALUES_EQUAL(semaphore.ConvertToI64(), value); |
260 |
| - value++; |
261 |
| - } |
262 |
| - |
263 |
| - for (i32 expected = 101; expected >= -101; --expected) { |
264 |
| - semaphore = TBasicExecutorPool::TSemaphore::GetSemaphore(value); |
265 |
| - UNIT_ASSERT_VALUES_EQUAL_C(expected, semaphore.OldSemaphore, (i64)semaphore.OldSemaphore |
266 |
| - << ' ' << (i64)semaphore.CurrentSleepThreadCount |
267 |
| - << ' ' << (i64)semaphore.CurrentThreadCount); |
268 |
| - UNIT_ASSERT_VALUES_EQUAL_C(sleepThreads, semaphore.CurrentSleepThreadCount, (i64)semaphore.OldSemaphore |
269 |
| - << ' ' << (i64)semaphore.CurrentSleepThreadCount |
270 |
| - << ' ' << (i64)semaphore.CurrentThreadCount); |
271 |
| - value--; |
272 |
| - } |
273 |
| - } |
274 |
| - |
275 |
| - //UNIT_ASSERT_VALUES_EQUAL_C(-1, TBasicExecutorPool::TSemaphore::GetSemaphore(value-1).OldSemaphore); |
276 |
| - } |
277 |
| - |
278 | 90 | Y_UNIT_TEST(CheckCompleteOne) {
|
279 | 91 | const size_t size = 4;
|
280 | 92 | const size_t msgCount = 1e4;
|
|
0 commit comments