Rev 110: make PerformanceCounter.get_timer() public. in http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

John Arbash Meinel john at arbash-meinel.com
Tue Oct 27 16:18:22 GMT 2009


At http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

------------------------------------------------------------
revno: 110
revision-id: john at arbash-meinel.com-20091027161810-6bll36xdo2i3w41l
parent: john at arbash-meinel.com-20091027151139-2jfe61bir92bfmjk
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Tue 2009-10-27 11:18:10 -0500
message:
  make PerformanceCounter.get_timer() public.
  
  Add a script to spawn a subprocess and watch its memory consumption.
  The only thing this really benefits is 'mem-secs', the idea of how
  many MB have been allocated for how long. This is slightly more
  useful than peak memory for stuff like codehosting.
  Since even if you peak very shortly, keeping the total memory consumption
  lower gives you better concurrency.
-------------- next part --------------
=== modified file 'meliae/perf_counter.py'
--- a/meliae/perf_counter.py	2009-10-27 14:57:31 +0000
+++ b/meliae/perf_counter.py	2009-10-27 16:18:10 +0000
@@ -80,7 +80,7 @@
         try:
             c = self._counters[name]
         except KeyError:
-            c = _Counter(name, self._get_timer())
+            c = _Counter(name, self.get_timer())
             self._counters[name] = c
         return c
 
@@ -95,14 +95,14 @@
 
 class _LinuxPerformanceCounter(PerformanceCounter):
 
-    def _get_timer(self):
+    def get_timer(self):
         # This returns wall-clock time
         return time.time
 
 
 class _Win32PerformanceCounter(PerformanceCounter):
 
-    def _get_timer(self):
+    def get_timer(self):
         # This returns wall-clock time, but using a much higher precision than
         # time.time() [which has a resolution of only 15ms]
         return time.clock

=== modified file 'meliae/tests/test_perf_counter.py'
--- a/meliae/tests/test_perf_counter.py	2009-10-27 15:11:39 +0000
+++ b/meliae/tests/test_perf_counter.py	2009-10-27 16:18:10 +0000
@@ -62,7 +62,7 @@
     def test_create_counter(self):
         counter = perf_counter.perf_counter.get_counter('test-counter')
         self.assertEqual('test-counter', counter.name)
-        self.assertEqual(counter._timer, perf_counter.perf_counter._get_timer())
+        self.assertEqual(counter._timer, perf_counter.perf_counter.get_timer())
         self.assertTrue('test-counter' in perf_counter.perf_counter._counters)
 
     def test_get_counter(self):

=== added file 'track_memory.py'
--- a/track_memory.py	1970-01-01 00:00:00 +0000
+++ b/track_memory.py	2009-10-27 16:18:10 +0000
@@ -0,0 +1,62 @@
+# Copyright (C) 2009 Canonical Ltd
+# 
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+# 
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""A program to spawn a subprocess and track how memory is consumed."""
+
+import subprocess
+import time
+
+def spawn_and_track(opts, args):
+    from meliae import perf_counter
+    timer = perf_counter.perf_counter.get_timer()
+    start = timer()
+    print 'spawning: %s' % (args,)
+    # We have to use shell=False, otherwise we end up tracking the 'cmd.exe' or
+    # 'sh' process, rather than the actual process we care about.
+    p = subprocess.Popen(args, shell=False)
+    mb = 1.0/(1024.0*1024)
+    last = start
+    last_print = 0
+    mem_secs = 0
+    while p.poll() is None:
+        now = timer()
+        cur_mem, peak_mem = perf_counter.perf_counter.get_memory(p)
+        mem_secs += cur_mem * (now - last)
+        last = now
+        time.sleep(opts.sleep_time)
+        if now - last_print > 3:
+            print '%8.3fs %6.1fMB %6.1fMB %8.1fMB*s         \r' % (
+                now - start, cur_mem*mb, peak_mem*mb, mem_secs*mb),
+            last_print = now
+    print '%8.3fs %6.1fMB %6.1fMB %8.1fMB*s          ' % (now - start,
+        cur_mem*mb, peak_mem*mb, mem_secs*mb)
+
+
+def main(args):
+    import optparse
+    p = optparse.OptionParser('%prog [local opts] command [opts]')
+    p.add_option('--trace-file', type=str, default=None,
+                 help='Save the memory usage information to this file')
+    p.add_option('--sleep-time', type=float, default=0.1,
+                 help='Check the status after this many seconds.')
+    # All options after the first 'command' are passed to the command, so don't
+    # try to process them ourselves.
+    p.disable_interspersed_args()
+    opts, args = p.parse_args(args)
+    spawn_and_track(opts, args)
+
+
+if __name__ == '__main__':
+    import sys
+    sys.exit(main(sys.argv[1:]))



More information about the bazaar-commits mailing list