Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/task_manager/sampling/task_group_unittest.cc

Issue 2905403002: plumb network upload into the task manager (Closed)
Patch Set: added refresh timer tests Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/task_manager/sampling/task_group.h" 5 #include "chrome/browser/task_manager/sampling/task_group.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 159
160 ASSERT_FALSE(background_refresh_complete_); 160 ASSERT_FALSE(background_refresh_complete_);
161 run_loop_->Run(); 161 run_loop_->Run();
162 162
163 EXPECT_TRUE(background_refresh_complete_); 163 EXPECT_TRUE(background_refresh_complete_);
164 #endif // !defined(DISABLE_NACL) 164 #endif // !defined(DISABLE_NACL)
165 165
166 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); 166 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone());
167 } 167 }
168 168
169 // Test the task has correct network usage rate when zero bytes read and sent.
170 TEST_F(TaskGroupTest, NetworkBytesSentReadZero) {
171 const int zero_bytes = 0;
172 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
173 fake_task.OnNetworkBytesRead(zero_bytes);
174 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
175 REFRESH_TYPE_NETWORK_USAGE);
176 EXPECT_EQ(zero_bytes, fake_task.network_usage_rate());
177 fake_task.OnNetworkBytesSent(zero_bytes);
178 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
179 REFRESH_TYPE_NETWORK_USAGE);
180 EXPECT_EQ(zero_bytes, fake_task.network_usage_rate());
181 }
182
183 // Test the task has correct network usage rate when only having read bytes.
184 TEST_F(TaskGroupTest, NetworkBytesRead) {
185 const int read_bytes = 1024;
186 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
187 fake_task.OnNetworkBytesRead(read_bytes);
188 EXPECT_EQ(0, fake_task.network_usage_rate());
189 EXPECT_EQ(read_bytes, fake_task.cumulative_network_usage());
190 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
191 REFRESH_TYPE_NETWORK_USAGE);
192 EXPECT_EQ(read_bytes, fake_task.network_usage_rate());
193 EXPECT_EQ(read_bytes, fake_task.cumulative_network_usage());
194 }
195
196 // Test the task has correct network usage rate when only having sent bytes.
197 TEST_F(TaskGroupTest, NetworkBytesSent) {
198 const int sent_bytes = 1023;
199 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
200 fake_task.OnNetworkBytesSent(sent_bytes);
201 EXPECT_EQ(0, fake_task.network_usage_rate());
202 EXPECT_EQ(sent_bytes, fake_task.cumulative_network_usage());
203 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
204 REFRESH_TYPE_NETWORK_USAGE);
205 EXPECT_EQ(sent_bytes, fake_task.network_usage_rate());
206 EXPECT_EQ(sent_bytes, fake_task.cumulative_network_usage());
207 }
208
209 // Test the task has correct network usage rate when only having read bytes and
210 // having a non 1s refresh time.
211 TEST_F(TaskGroupTest, NetworkBytesRead2SecRefresh) {
212 const int refresh_secs = 2;
213 const int read_bytes = 1024 * refresh_secs; // for integer division
214 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
215 fake_task.OnNetworkBytesRead(read_bytes);
216 EXPECT_EQ(0, fake_task.network_usage_rate());
217 EXPECT_EQ(read_bytes, fake_task.cumulative_network_usage());
218 fake_task.Refresh(base::TimeDelta::FromSeconds(refresh_secs),
219 REFRESH_TYPE_NETWORK_USAGE);
220 EXPECT_EQ(read_bytes / refresh_secs, fake_task.network_usage_rate());
221 EXPECT_EQ(read_bytes, fake_task.cumulative_network_usage());
222 }
223
224 // Test the task has correct network usage rate when only having sent bytes and
225 // having a non 1s refresh time.
226 TEST_F(TaskGroupTest, NetworkBytesSent2SecRefresh) {
227 const int refresh_secs = 2;
228 const int sent_bytes = 1023 * refresh_secs; // for integer division
229 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
230 fake_task.OnNetworkBytesSent(sent_bytes);
231 EXPECT_EQ(0, fake_task.network_usage_rate());
232 EXPECT_EQ(sent_bytes, fake_task.cumulative_network_usage());
233 fake_task.Refresh(base::TimeDelta::FromSeconds(refresh_secs),
234 REFRESH_TYPE_NETWORK_USAGE);
235 EXPECT_EQ(sent_bytes / refresh_secs, fake_task.network_usage_rate());
236 EXPECT_EQ(sent_bytes, fake_task.cumulative_network_usage());
237 }
238
239 // Tests the task has correct usage on receiving and then sending bytes.
240 TEST_F(TaskGroupTest, NetworkBytesReadThenSent) {
241 const int read_bytes = 124;
242 const int sent_bytes = 1027;
243 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
244 fake_task.OnNetworkBytesRead(read_bytes);
245 EXPECT_EQ(read_bytes, fake_task.cumulative_network_usage());
246 fake_task.OnNetworkBytesSent(sent_bytes);
247 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
248 REFRESH_TYPE_NETWORK_USAGE);
249 EXPECT_EQ(read_bytes + sent_bytes, fake_task.network_usage_rate());
250 EXPECT_EQ(read_bytes + sent_bytes, fake_task.cumulative_network_usage());
251 }
252
253 // Tests the task has correct usage rate on sending and then receiving bytes.
254 TEST_F(TaskGroupTest, NetworkBytesSentThenRead) {
255 const int read_bytes = 1025;
256 const int sent_bytes = 10;
257 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
258 fake_task.OnNetworkBytesSent(sent_bytes);
259 fake_task.OnNetworkBytesRead(read_bytes);
260 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
261 REFRESH_TYPE_NETWORK_USAGE);
262 EXPECT_EQ(read_bytes + sent_bytes, fake_task.network_usage_rate());
263 }
264
265 // Tests that the network usage rate goes to 0 after reading bytes then a
266 // refresh with no traffic and that cumulative is still correct.
267 TEST_F(TaskGroupTest, NetworkBytesReadRefreshNone) {
268 const int read_bytes = 1024;
269 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
270 fake_task.OnNetworkBytesRead(read_bytes);
271 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
272 REFRESH_TYPE_NETWORK_USAGE);
273 // Refresh to zero out the usage rate.
274 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
275 REFRESH_TYPE_NETWORK_USAGE);
276 EXPECT_EQ(0, fake_task.network_usage_rate());
277 EXPECT_EQ(read_bytes, fake_task.cumulative_network_usage());
278 }
279
280 // Tests that the network usage rate goes to 0 after sending bytes then a
281 // refresh with no traffic and that cumulative is still correct.
282 TEST_F(TaskGroupTest, NetworkBytesSentRefreshNone) {
283 const int sent_bytes = 1024;
284 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
285 fake_task.OnNetworkBytesSent(sent_bytes);
286 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
287 REFRESH_TYPE_NETWORK_USAGE);
288 // Refresh to zero out the usage rate.
289 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
290 REFRESH_TYPE_NETWORK_USAGE);
291 EXPECT_EQ(0, fake_task.network_usage_rate());
292 EXPECT_EQ(sent_bytes, fake_task.cumulative_network_usage());
293 }
294
295 // Tests that the network usage rate goes to 0 after a refresh with no traffic
296 // and that cumulative is still correct.
297 TEST_F(TaskGroupTest, NetworkBytesTransferedRefreshNone) {
298 const int read_bytes = 1024;
299 const int sent_bytes = 1;
300 const int number_of_cycles = 2;
301 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
302 for (int i = 0; i < number_of_cycles; i++) {
303 fake_task.OnNetworkBytesRead(read_bytes);
304 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
305 REFRESH_TYPE_NETWORK_USAGE);
306 fake_task.OnNetworkBytesSent(sent_bytes);
307 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
308 REFRESH_TYPE_NETWORK_USAGE);
309 }
310 // Refresh to zero out the usage rate.
311 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
312 REFRESH_TYPE_NETWORK_USAGE);
313 EXPECT_EQ(0, fake_task.network_usage_rate());
314 EXPECT_EQ((read_bytes + sent_bytes) * number_of_cycles,
315 fake_task.cumulative_network_usage());
316 }
317
318 // Tests that 2 tasks in 1 task group that both read bytes have correct usage
319 // rates and correct cumulative network usage.
320 TEST_F(TaskGroupTest, NetworkBytesReadAsGroup) {
321 const int read_bytes1 = 1024;
322 const int read_bytes2 = 789;
323 const int number_of_cycles = 2;
324 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER);
325 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER);
326
327 task_group_.AddTask(&fake_task1);
328 task_group_.AddTask(&fake_task2);
329
330 for (int i = 0; i < number_of_cycles; i++) {
331 fake_task1.OnNetworkBytesRead(read_bytes1);
332 fake_task2.OnNetworkBytesRead(read_bytes2);
333 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
334 base::TimeDelta::FromSeconds(1),
335 REFRESH_TYPE_NETWORK_USAGE);
336 EXPECT_EQ(read_bytes1 + read_bytes2,
337 task_group_.per_process_network_usage_rate());
338 }
339
340 EXPECT_EQ((read_bytes1 + read_bytes2) * number_of_cycles,
341 task_group_.cumulative_per_process_network_usage());
342 }
343
344 // Tests that the network usage rate does not get affected until a refresh is
345 // called and that the cumulative is as up to date as possible
346 TEST_F(TaskGroupTest, NetworkBytesTransferedRefreshOutOfOrder) {
ncarter (slow) 2017/06/16 22:28:30 "transfered" -> "transferred" throughout (I'd tho
cburn 2017/06/19 22:07:08 Done, it looks like it didn't destroy everything.
347 const int read_bytes = 1024;
348 const int sent_bytes = 1;
349 const int number_of_cycles = 4;
350 int number_of_bytes_transfered = 0;
351 FakeTask fake_task(base::Process::Current().Pid(), Task::RENDERER);
352 for (int i = 0; i < number_of_cycles; i++) {
353 fake_task.OnNetworkBytesRead(read_bytes * i);
354 number_of_bytes_transfered += read_bytes * i;
355 EXPECT_EQ(number_of_bytes_transfered, fake_task.cumulative_network_usage());
356 fake_task.OnNetworkBytesSent(sent_bytes * i);
357 number_of_bytes_transfered += sent_bytes * i;
358 EXPECT_EQ(number_of_bytes_transfered, fake_task.cumulative_network_usage());
359 if (i > 0) {
360 EXPECT_EQ((read_bytes + sent_bytes) * (i - 1),
361 fake_task.network_usage_rate());
362 }
363 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
364 REFRESH_TYPE_NETWORK_USAGE);
365 EXPECT_EQ((read_bytes + sent_bytes) * i, fake_task.network_usage_rate());
366 }
367 // Refresh to zero out the usage rate.
368 fake_task.Refresh(base::TimeDelta::FromSeconds(1),
369 REFRESH_TYPE_NETWORK_USAGE);
370 EXPECT_EQ(0, fake_task.network_usage_rate());
371 EXPECT_EQ(number_of_bytes_transfered, fake_task.cumulative_network_usage());
372 }
373
374 // Tests that 2 tasks in 1 task group that both sent bytes have correct usage
375 // rates and correct cumulative network usage.
376 TEST_F(TaskGroupTest, NetworkBytesSentAsGroup) {
377 const int sent_bytes1 = 1123;
378 const int sent_bytes2 = 778;
379 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER);
380 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER);
381
382 task_group_.AddTask(&fake_task1);
383 task_group_.AddTask(&fake_task2);
384
385 fake_task1.OnNetworkBytesSent(sent_bytes1);
386 fake_task2.OnNetworkBytesSent(sent_bytes2);
387 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
388 base::TimeDelta::FromSeconds(1),
389 REFRESH_TYPE_NETWORK_USAGE);
390 EXPECT_EQ(sent_bytes1 + sent_bytes2,
391 task_group_.per_process_network_usage_rate());
392
393 fake_task1.OnNetworkBytesSent(sent_bytes1);
394 fake_task2.OnNetworkBytesSent(sent_bytes2);
395 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
396 base::TimeDelta::FromSeconds(1),
397 REFRESH_TYPE_NETWORK_USAGE);
398
399 EXPECT_EQ((sent_bytes1 + sent_bytes2) * 2,
400 task_group_.cumulative_per_process_network_usage());
401 }
402
403 // Tests that 2 tasks in 1 task group that have one sending and one reading
404 // have correct usage rates for the group and correct cumulative network usage.
405 TEST_F(TaskGroupTest, NetworkBytesTransferedAsGroup) {
406 const int sent_bytes = 1023;
407 const int read_bytes = 678;
408 const int number_of_cycles = 2;
409 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER);
410 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER);
411
412 task_group_.AddTask(&fake_task1);
413 task_group_.AddTask(&fake_task2);
414 for (int i = 0; i < number_of_cycles; i++) {
415 fake_task1.OnNetworkBytesSent(sent_bytes);
416 fake_task2.OnNetworkBytesRead(read_bytes);
417 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
418 base::TimeDelta::FromSeconds(1),
419 REFRESH_TYPE_NETWORK_USAGE);
420 EXPECT_EQ(sent_bytes + read_bytes,
421 task_group_.per_process_network_usage_rate());
422 }
423
424 EXPECT_EQ((read_bytes + sent_bytes) * number_of_cycles,
425 task_group_.cumulative_per_process_network_usage());
426 }
427
428 // Tests that after two tasks in a task group read bytes that a refresh will
429 // zero out network usage rate while maintaining the correct cumulative network
430 // usage
431 TEST_F(TaskGroupTest, NetworkBytesReadAsGroupThenNone) {
432 const int read_bytes1 = 1013;
433 const int read_bytes2 = 679;
434 const int number_of_cycles = 2;
435 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER);
436 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER);
437
438 task_group_.AddTask(&fake_task1);
439 task_group_.AddTask(&fake_task2);
440
441 for (int i = 0; i < number_of_cycles; i++) {
442 fake_task1.OnNetworkBytesRead(read_bytes1);
443 fake_task2.OnNetworkBytesRead(read_bytes2);
444 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
445 base::TimeDelta::FromSeconds(1),
446 REFRESH_TYPE_NETWORK_USAGE);
447 EXPECT_EQ(read_bytes1 + read_bytes2,
448 task_group_.per_process_network_usage_rate());
449 }
450 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
451 base::TimeDelta::FromSeconds(1),
452 REFRESH_TYPE_NETWORK_USAGE);
453 EXPECT_EQ(0, task_group_.per_process_network_usage_rate());
454 EXPECT_EQ((read_bytes1 + read_bytes2) * number_of_cycles,
455 task_group_.cumulative_per_process_network_usage());
456 }
457
458 // Tests that after two tasks in a task group send bytes that a refresh will
459 // zero out network usage rate while maintaining the correct cumulative network
460 // usage
461 TEST_F(TaskGroupTest, NetworkBytesSentAsGroupThenNone) {
462 const int sent_bytes1 = 1023;
463 const int sent_bytes2 = 678;
464 const int number_of_cycles = 2;
465 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER);
466 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER);
467
468 task_group_.AddTask(&fake_task1);
469 task_group_.AddTask(&fake_task2);
470
471 for (int i = 0; i < number_of_cycles; i++) {
472 fake_task1.OnNetworkBytesSent(sent_bytes1);
473 fake_task2.OnNetworkBytesSent(sent_bytes2);
474 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
475 base::TimeDelta::FromSeconds(1),
476 REFRESH_TYPE_NETWORK_USAGE);
477 EXPECT_EQ(sent_bytes1 + sent_bytes2,
478 task_group_.per_process_network_usage_rate());
479 }
480 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
481 base::TimeDelta::FromSeconds(1),
482 REFRESH_TYPE_NETWORK_USAGE);
483 EXPECT_EQ(0, task_group_.per_process_network_usage_rate());
484 EXPECT_EQ((sent_bytes1 + sent_bytes2) * number_of_cycles,
485 task_group_.cumulative_per_process_network_usage());
486 }
487
488 // Tests that after two tasks in a task group transfered bytes that a refresh
489 // will zero out network usage rate while maintaining the correct cumulative
490 // network usage
491 TEST_F(TaskGroupTest, NetworkBytesTransferedAsGroupThenNone) {
492 const int read_bytes = 321;
493 const int sent_bytes = 987;
494 const int number_of_cycles = 3;
495 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER);
496 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER);
497
498 task_group_.AddTask(&fake_task1);
499 task_group_.AddTask(&fake_task2);
500
501 for (int i = 0; i < number_of_cycles; i++) {
502 fake_task1.OnNetworkBytesRead(read_bytes);
503 fake_task2.OnNetworkBytesSent(sent_bytes);
504 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
505 base::TimeDelta::FromSeconds(1),
506 REFRESH_TYPE_NETWORK_USAGE);
507 EXPECT_EQ(read_bytes + sent_bytes,
508 task_group_.per_process_network_usage_rate());
509 }
510 task_group_.Refresh(gpu::VideoMemoryUsageStats(),
511 base::TimeDelta::FromSeconds(1),
512 REFRESH_TYPE_NETWORK_USAGE);
513 EXPECT_EQ(0, task_group_.per_process_network_usage_rate());
514 EXPECT_EQ((read_bytes + sent_bytes) * number_of_cycles,
515 task_group_.cumulative_per_process_network_usage());
516 }
517
169 } // namespace task_manager 518 } // namespace task_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698