Class: Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/oink/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



5
6
7
# File 'lib/oink/cli.rb', line 5

def initialize(args)
  @args = args
end

Instance Method Details

#processObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/oink/cli.rb', line 9

def process
  options = { :format => :short_summary, :type => :memory }

  op = OptionParser.new do |opts|
    opts.banner = "Usage: oink [options] files"

    opts.on("-t", "--threshold [INTEGER]", Integer,
            "Memory threshold in MB") do |threshold|
      options[:threshold] = threshold
    end

    opts.on("-f", "--file filepath", "Output to file") do |filename|
      options[:output_file] = filename
    end

    format_list = (Oink::MemoryUsageReporter::FORMAT_ALIASES.keys + Oink::MemoryUsageReporter::FORMATS).join(',')
    opts.on("--format FORMAT", Oink::MemoryUsageReporter::FORMATS, Oink::MemoryUsageReporter::FORMAT_ALIASES, "Select format",
            "  (#{format_list})") do |format|
      options[:format] = format.to_sym
    end

    opts.on("-m", "--memory", "Check for Memory Threshold (default)") do |v|
       options[:type] = :memory
    end

    opts.on("-r", "--active-record", "Check for Active Record Threshold") do |v|
       options[:type] = :active_record
    end

  end

  op.parse!(@args)

  if @args.empty?
    puts op
    exit
  end

  output = nil

  if options[:output_file]
    output = File.open(options[:output_file], 'w')
  else
    output = STDOUT
  end

  files = get_file_listing(@args)

  handles = files.map { |f| File.open(f) }

  if options[:type] == :memory

    options[:threshold] ||= 75
    options[:threshold] *= 1024

    Oink::MemoryUsageReporter.new(handles, options[:threshold], :format => options[:format]).print(output)

  elsif options[:type] == :active_record

    options[:threshold] ||= 500

    Oink::ActiveRecordInstantiationReporter.new(handles, options[:threshold], :format => options[:format]).print(output)

  end

  output.close
  handles.each { |h| h.close }    
end