memory_usage 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/memory_usage CHANGED
@@ -2,9 +2,10 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'memory_usage'
5
+ require 'memory_usage_optparse'
5
6
 
6
7
 
7
- a = MemoryUsage.new
8
+ a = MemoryUsage.new(ARGV)
8
9
  a.report
9
10
 
10
11
  #a.refresh
data/lib/memory_usage.rb CHANGED
@@ -1,13 +1,24 @@
1
1
 
2
+ path = File.expand_path __FILE__
3
+ path = File.dirname(path)
4
+
5
+ require "#{path}/memory_usage_optparse"
6
+
2
7
  #Class which has function for gathering memory stats.
3
8
  # Should work on unix/linux and OS X
4
9
  # Example usage at the bottom
5
10
  class MemoryUsage
6
11
 
7
- def initialize
12
+ def initialize(argv=[""])
13
+ @options = MemoryUsageOptparse.parse(argv)
8
14
  refresh
9
15
  end
10
16
 
17
+ def filter (text="")
18
+ @options.filter = (text)
19
+ end
20
+
21
+
11
22
  #Recalculate Memory usage. stops have to call MemoryUsage.new
12
23
  def refresh
13
24
  #Based on: http://gist.github.com/290988
@@ -21,20 +32,29 @@ class MemoryUsage
21
32
  p =~ /(\d+)\s+(\d+)\s+(.+)/
22
33
 
23
34
  pid, rss, command = [$1, $2, $3]
24
- rss = rss.to_f
25
- @usage << {:pid=>pid, :rss=>(rss/1024), :command=>command }
26
- @total += rss/1024
27
35
 
28
- if pid.to_s.size > @max[:pid]
29
- @max[:pid] = pid.to_s.size
30
- end
36
+ # Some complicated logic
37
+ #Allow if no options set OR
38
+ #Allow if command matches filter but not with --filter (because that is this file) OR
39
+ #Filter is memory_usage we will allow it to profile itself
40
+ if (@options.filter == "") or
41
+ ((command.match(/.*#{@options.filter}.*/)) and (not command.match(/.*--filter #{@options.filter}.*/) )) or
42
+ ((command.match(/.*#{@options.filter}.*/)) and (@options.filter.match(/.*memory_usage.*/)))
43
+ rss = rss.to_f
44
+ @usage << {:pid=>pid, :rss=>(rss/1024), :command=>command }
45
+ @total += rss/1024
31
46
 
32
- if rss.to_s.size > @max[:rss]
33
- @max[:rss] = rss.to_s.size
34
- end
35
-
36
- if command.size > @max[:command]
37
- @max[:command] = command.size
47
+ if pid.to_s.size > @max[:pid]
48
+ @max[:pid] = pid.to_s.size
49
+ end
50
+
51
+ if rss.to_s.size > @max[:rss]
52
+ @max[:rss] = rss.to_s.size
53
+ end
54
+
55
+ if command.size > @max[:command]
56
+ @max[:command] = command.size
57
+ end
38
58
  end
39
59
  #puts pid + (" %.2f MB " % (rss/1024)) + command
40
60
 
@@ -59,4 +79,23 @@ class MemoryUsage
59
79
  "%.2f" % @total
60
80
  end
61
81
  end
82
+
83
+
84
+
85
+
86
+
87
+ #Run example only if called directly
88
+ #ie not if included/required for class
89
+ if $0 == __FILE__
90
+ a = MemoryUsage.new
91
+ a.report
92
+
93
+ #a.refresh
94
+ #a.report
95
+ #puts a.total
96
+
97
+ #command = MemoryUsage.new
98
+ #exit command.run!
99
+
100
+ end
62
101
 
@@ -0,0 +1,67 @@
1
+ require 'optparse'
2
+ require 'optparse/time'
3
+ require 'ostruct'
4
+
5
+ class MemoryUsageOptparse
6
+
7
+
8
+ #
9
+ # Return a structure describing the options.
10
+ #
11
+ def self.parse(args)
12
+ @VERSION = "0.0.3"
13
+
14
+ # The options specified on the command line will be collected in *options*.
15
+ # We set default values here.
16
+ options = OpenStruct.new
17
+
18
+ options.verbose = false
19
+ options.filter = ""
20
+ options.min = 0.0
21
+
22
+
23
+ opts = OptionParser.new do |opts|
24
+ opts.banner = "Usage: #{__FILE__} [options]"
25
+ opts.separator ""
26
+ opts.separator "Common options:"
27
+
28
+ # No argument, shows at tail. This will print an options summary.
29
+ opts.on_tail("-h", "--help", "Show this message") do
30
+ puts opts
31
+ exit
32
+ end
33
+
34
+ # Another typical switch to print the version.
35
+ opts.on_tail("--version", "Show version") do
36
+ #puts OptionParser::Version.join('.')
37
+ puts "Version #{@VERSION}"
38
+ exit
39
+ end
40
+
41
+ # Boolean switch.
42
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
43
+ options.verbose = v
44
+ end
45
+
46
+ opts.separator ""
47
+ opts.separator "Specific options:"
48
+
49
+
50
+ # Cast 'delay' argument to a Float.
51
+ opts.on("--filter name", String, "text to filter process") do |n|
52
+ options.filter = n
53
+ end
54
+
55
+ opts.on("--gt N", Float, "Memory usage greater than N") do |n|
56
+ options.min = n
57
+ end
58
+
59
+
60
+ end
61
+
62
+ options.leftovers = opts.parse!(args)
63
+ options
64
+ end # parse()
65
+
66
+ end # class OptparseExample
67
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memory_usage
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Morgan Prior
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-18 00:00:00 +01:00
18
+ date: 2010-06-22 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -34,6 +34,7 @@ extra_rdoc_files: []
34
34
  files:
35
35
  - bin/memory_usage
36
36
  - lib/memory_usage.rb
37
+ - lib/memory_usage_optparse.rb
37
38
  has_rdoc: true
38
39
  homepage: http://amaras-tech.co.uk/software/memory_usage
39
40
  licenses: []