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

Side by Side Diff: base/process/process_metrics_unittest.cc

Issue 888193002: ProcessMetrics::GetCPUUsage() is Slow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 #include <string> 8 #include <string>
9 9
10 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat)); 316 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat));
317 317
318 // cat /proc/self/stat on a random other machine I have. 318 // cat /proc/self/stat on a random other machine I have.
319 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 " 319 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
320 "0 142 0 0 0 " 320 "0 142 0 0 0 "
321 "0 0 0 0 " // <- No CPU, apparently. 321 "0 0 0 0 " // <- No CPU, apparently.
322 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 " 322 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
323 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; 323 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
324 324
325 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat)); 325 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat));
326
327 // Some weird long-running process with a weird name that I created for the
328 // purposes of this test.
329 const char kWeirdNameStat[] = "26115 (Hello) You ())) ) R 24614 26115 24614"
330 " 34839 26115 4218880 227 0 0 0 "
331 "5186 11 0 0 "
332 "20 0 1 0 36933953 4296704 90 18446744073709551615 4194304 4196116 "
333 "140735857761568 140735857761160 4195644 0 0 0 0 0 0 0 17 14 0 0 0 0 0 "
334 "6295056 6295616 16519168 140735857770710 140735857770737 "
335 "140735857770737 140735857774557 0";
336 EXPECT_EQ(5186 + 11, base::ParseProcStatCPU(kWeirdNameStat));
337 }
338
339 TEST(ProcessMetricsTest, GetCpuFromProcStats) {
340 // /proc/self/stat for a process running "top".
341 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 "
342 "4202496 471 0 0 0 "
343 "12 16 0 0 " // <- These are the goods.
344 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
345 "4246868 140733983044336 18446744073709551615 140244213071219 "
346 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
347 EXPECT_EQ(12 + 16, base::GetCpuFromProcStats(kTopStat));
348
349 // cat /proc/self/stat on a random other machine I have.
350 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
351 "0 142 0 0 0 "
352 "0 0 0 0 " // <- No CPU, apparently.
353 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
354 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
355 EXPECT_EQ(0, base::GetCpuFromProcStats(kSelfStat));
356
357 // Some weird long-running process with a weird name that I created for the
358 // purposes of this test.
359 const char kWeirdNameStat[] = "26115 (Hello) You ())) ) R 24614 26115 24614"
360 " 34839 26115 4218880 227 0 0 0 "
361 "5186 11 0 0 "
362 "20 0 1 0 36933953 4296704 90 18446744073709551615 4194304 4196116 "
363 "140735857761568 140735857761160 4195644 0 0 0 0 0 0 0 17 14 0 0 0 0 0 "
364 "6295056 6295616 16519168 140735857770710 140735857770737 "
365 "140735857770737 140735857774557 0";
366 EXPECT_EQ(5186 + 11, base::GetCpuFromProcStats(kWeirdNameStat));
326 } 367 }
327 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 368 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
328 369
329 // Disable on Android because base_unittests runs inside a Dalvik VM that 370 // Disable on Android because base_unittests runs inside a Dalvik VM that
330 // starts and stop threads (crbug.com/175563). 371 // starts and stop threads (crbug.com/175563).
331 #if defined(OS_LINUX) 372 #if defined(OS_LINUX)
332 // http://crbug.com/396455 373 // http://crbug.com/396455
333 TEST(ProcessMetricsTest, DISABLED_GetNumberOfThreads) { 374 TEST(ProcessMetricsTest, DISABLED_GetNumberOfThreads) {
334 const base::ProcessHandle current = base::GetCurrentProcessHandle(); 375 const base::ProcessHandle current = base::GetCurrentProcessHandle();
335 const int initial_threads = base::GetNumberOfThreads(current); 376 const int initial_threads = base::GetNumberOfThreads(current);
336 ASSERT_GT(initial_threads, 0); 377 ASSERT_GT(initial_threads, 0);
337 const int kNumAdditionalThreads = 10; 378 const int kNumAdditionalThreads = 10;
338 { 379 {
339 scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads]; 380 scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads];
340 for (int i = 0; i < kNumAdditionalThreads; ++i) { 381 for (int i = 0; i < kNumAdditionalThreads; ++i) {
341 my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest")); 382 my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest"));
342 my_threads[i]->Start(); 383 my_threads[i]->Start();
343 ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i); 384 ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i);
344 } 385 }
345 } 386 }
346 // The Thread destructor will stop them. 387 // The Thread destructor will stop them.
347 ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current)); 388 ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current));
348 } 389 }
349 #endif // defined(OS_LINUX) 390 #endif // defined(OS_LINUX)
350 391
351 } // namespace debug 392 } // namespace debug
352 } // namespace base 393 } // namespace base
OLDNEW
« base/process/process_metrics_linux.cc ('K') | « base/process/process_metrics_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698