OLD | NEW |
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 Loading... |
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, NetworkBytesTransferredRefreshNone) { |
| 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, NetworkBytesTransferredRefreshOutOfOrder) { |
| 347 const int read_bytes = 1024; |
| 348 const int sent_bytes = 1; |
| 349 const int number_of_cycles = 4; |
| 350 int number_of_bytes_transferred = 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_transferred += read_bytes * i; |
| 355 EXPECT_EQ(number_of_bytes_transferred, |
| 356 fake_task.cumulative_network_usage()); |
| 357 fake_task.OnNetworkBytesSent(sent_bytes * i); |
| 358 number_of_bytes_transferred += sent_bytes * i; |
| 359 EXPECT_EQ(number_of_bytes_transferred, |
| 360 fake_task.cumulative_network_usage()); |
| 361 if (i > 0) { |
| 362 EXPECT_EQ((read_bytes + sent_bytes) * (i - 1), |
| 363 fake_task.network_usage_rate()); |
| 364 } |
| 365 fake_task.Refresh(base::TimeDelta::FromSeconds(1), |
| 366 REFRESH_TYPE_NETWORK_USAGE); |
| 367 EXPECT_EQ((read_bytes + sent_bytes) * i, fake_task.network_usage_rate()); |
| 368 } |
| 369 // Refresh to zero out the usage rate. |
| 370 fake_task.Refresh(base::TimeDelta::FromSeconds(1), |
| 371 REFRESH_TYPE_NETWORK_USAGE); |
| 372 EXPECT_EQ(0, fake_task.network_usage_rate()); |
| 373 EXPECT_EQ(number_of_bytes_transferred, fake_task.cumulative_network_usage()); |
| 374 } |
| 375 |
| 376 // Tests that 2 tasks in 1 task group that both sent bytes have correct usage |
| 377 // rates and correct cumulative network usage. |
| 378 TEST_F(TaskGroupTest, NetworkBytesSentAsGroup) { |
| 379 const int sent_bytes1 = 1123; |
| 380 const int sent_bytes2 = 778; |
| 381 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER); |
| 382 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER); |
| 383 |
| 384 task_group_.AddTask(&fake_task1); |
| 385 task_group_.AddTask(&fake_task2); |
| 386 |
| 387 fake_task1.OnNetworkBytesSent(sent_bytes1); |
| 388 fake_task2.OnNetworkBytesSent(sent_bytes2); |
| 389 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 390 base::TimeDelta::FromSeconds(1), |
| 391 REFRESH_TYPE_NETWORK_USAGE); |
| 392 EXPECT_EQ(sent_bytes1 + sent_bytes2, |
| 393 task_group_.per_process_network_usage_rate()); |
| 394 |
| 395 fake_task1.OnNetworkBytesSent(sent_bytes1); |
| 396 fake_task2.OnNetworkBytesSent(sent_bytes2); |
| 397 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 398 base::TimeDelta::FromSeconds(1), |
| 399 REFRESH_TYPE_NETWORK_USAGE); |
| 400 |
| 401 EXPECT_EQ((sent_bytes1 + sent_bytes2) * 2, |
| 402 task_group_.cumulative_per_process_network_usage()); |
| 403 } |
| 404 |
| 405 // Tests that 2 tasks in 1 task group that have one sending and one reading |
| 406 // have correct usage rates for the group and correct cumulative network usage. |
| 407 TEST_F(TaskGroupTest, NetworkBytesTransferredAsGroup) { |
| 408 const int sent_bytes = 1023; |
| 409 const int read_bytes = 678; |
| 410 const int number_of_cycles = 2; |
| 411 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER); |
| 412 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER); |
| 413 |
| 414 task_group_.AddTask(&fake_task1); |
| 415 task_group_.AddTask(&fake_task2); |
| 416 for (int i = 0; i < number_of_cycles; i++) { |
| 417 fake_task1.OnNetworkBytesSent(sent_bytes); |
| 418 fake_task2.OnNetworkBytesRead(read_bytes); |
| 419 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 420 base::TimeDelta::FromSeconds(1), |
| 421 REFRESH_TYPE_NETWORK_USAGE); |
| 422 EXPECT_EQ(sent_bytes + read_bytes, |
| 423 task_group_.per_process_network_usage_rate()); |
| 424 } |
| 425 |
| 426 EXPECT_EQ((read_bytes + sent_bytes) * number_of_cycles, |
| 427 task_group_.cumulative_per_process_network_usage()); |
| 428 } |
| 429 |
| 430 // Tests that after two tasks in a task group read bytes that a refresh will |
| 431 // zero out network usage rate while maintaining the correct cumulative network |
| 432 // usage |
| 433 TEST_F(TaskGroupTest, NetworkBytesReadAsGroupThenNone) { |
| 434 const int read_bytes1 = 1013; |
| 435 const int read_bytes2 = 679; |
| 436 const int number_of_cycles = 2; |
| 437 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER); |
| 438 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER); |
| 439 |
| 440 task_group_.AddTask(&fake_task1); |
| 441 task_group_.AddTask(&fake_task2); |
| 442 |
| 443 for (int i = 0; i < number_of_cycles; i++) { |
| 444 fake_task1.OnNetworkBytesRead(read_bytes1); |
| 445 fake_task2.OnNetworkBytesRead(read_bytes2); |
| 446 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 447 base::TimeDelta::FromSeconds(1), |
| 448 REFRESH_TYPE_NETWORK_USAGE); |
| 449 EXPECT_EQ(read_bytes1 + read_bytes2, |
| 450 task_group_.per_process_network_usage_rate()); |
| 451 } |
| 452 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 453 base::TimeDelta::FromSeconds(1), |
| 454 REFRESH_TYPE_NETWORK_USAGE); |
| 455 EXPECT_EQ(0, task_group_.per_process_network_usage_rate()); |
| 456 EXPECT_EQ((read_bytes1 + read_bytes2) * number_of_cycles, |
| 457 task_group_.cumulative_per_process_network_usage()); |
| 458 } |
| 459 |
| 460 // Tests that after two tasks in a task group send bytes that a refresh will |
| 461 // zero out network usage rate while maintaining the correct cumulative network |
| 462 // usage |
| 463 TEST_F(TaskGroupTest, NetworkBytesSentAsGroupThenNone) { |
| 464 const int sent_bytes1 = 1023; |
| 465 const int sent_bytes2 = 678; |
| 466 const int number_of_cycles = 2; |
| 467 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER); |
| 468 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER); |
| 469 |
| 470 task_group_.AddTask(&fake_task1); |
| 471 task_group_.AddTask(&fake_task2); |
| 472 |
| 473 for (int i = 0; i < number_of_cycles; i++) { |
| 474 fake_task1.OnNetworkBytesSent(sent_bytes1); |
| 475 fake_task2.OnNetworkBytesSent(sent_bytes2); |
| 476 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 477 base::TimeDelta::FromSeconds(1), |
| 478 REFRESH_TYPE_NETWORK_USAGE); |
| 479 EXPECT_EQ(sent_bytes1 + sent_bytes2, |
| 480 task_group_.per_process_network_usage_rate()); |
| 481 } |
| 482 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 483 base::TimeDelta::FromSeconds(1), |
| 484 REFRESH_TYPE_NETWORK_USAGE); |
| 485 EXPECT_EQ(0, task_group_.per_process_network_usage_rate()); |
| 486 EXPECT_EQ((sent_bytes1 + sent_bytes2) * number_of_cycles, |
| 487 task_group_.cumulative_per_process_network_usage()); |
| 488 } |
| 489 |
| 490 // Tests that after two tasks in a task group transferred bytes that a refresh |
| 491 // will zero out network usage rate while maintaining the correct cumulative |
| 492 // network usage |
| 493 TEST_F(TaskGroupTest, NetworkBytesTransferredAsGroupThenNone) { |
| 494 const int read_bytes = 321; |
| 495 const int sent_bytes = 987; |
| 496 const int number_of_cycles = 3; |
| 497 FakeTask fake_task1(base::Process::Current().Pid(), Task::RENDERER); |
| 498 FakeTask fake_task2(base::Process::Current().Pid(), Task::RENDERER); |
| 499 |
| 500 task_group_.AddTask(&fake_task1); |
| 501 task_group_.AddTask(&fake_task2); |
| 502 |
| 503 for (int i = 0; i < number_of_cycles; i++) { |
| 504 fake_task1.OnNetworkBytesRead(read_bytes); |
| 505 fake_task2.OnNetworkBytesSent(sent_bytes); |
| 506 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 507 base::TimeDelta::FromSeconds(1), |
| 508 REFRESH_TYPE_NETWORK_USAGE); |
| 509 EXPECT_EQ(read_bytes + sent_bytes, |
| 510 task_group_.per_process_network_usage_rate()); |
| 511 } |
| 512 task_group_.Refresh(gpu::VideoMemoryUsageStats(), |
| 513 base::TimeDelta::FromSeconds(1), |
| 514 REFRESH_TYPE_NETWORK_USAGE); |
| 515 EXPECT_EQ(0, task_group_.per_process_network_usage_rate()); |
| 516 EXPECT_EQ((read_bytes + sent_bytes) * number_of_cycles, |
| 517 task_group_.cumulative_per_process_network_usage()); |
| 518 } |
| 519 |
169 } // namespace task_manager | 520 } // namespace task_manager |
OLD | NEW |