Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Added Titan::System, Titan::Thread#used_cpu and Titan::Thread#used_me…
Browse files Browse the repository at this point in the history
…mory
  • Loading branch information
flippingbits committed Feb 6, 2011
1 parent 702d487 commit 21d4ab6
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## dev

- Features

* Added Titan::System that provides common methods
* Added Titan::Thread#used\_cpu and Titan::Thread#used\_memory

## 0.3.0 (December 12, 2010)

- Features
Expand Down
1 change: 1 addition & 0 deletions lib/titan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module Titan
autoload :CLI, "titan/cli"
autoload :System, "titan/system"
autoload :Thread, "titan/thread"
end
22 changes: 22 additions & 0 deletions lib/titan/system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Titan
class System
class << self
#
# Calls the ps function of the OS with a given command and process
# id
#
# E.g., if you want to get the used memory of a given process
# (pid=10), you can do this by:
#
# Titan::System.ps('rss', 10).to_i
#
def ps(command, pid)
if pid > 0
`ps -o #{command}= -p #{pid}`
else
nil
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/titan/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ def pid_file
File.expand_path(@id.to_s + ".pid", Titan::Thread::TITAN_DIRECTORY)
end

#
# Returns the used memory
#
def used_memory
used_memory = Titan::System.ps('rss', @pid)
used_memory ? used_memory.to_i : nil
end

#
# Returns the used CPU
#
def used_cpu
used_cpu = Titan::System.ps('%cpu', @pid)
used_cpu ? used_cpu.to_f : nil
end

#
# Opens the pid file and save its pid in it
#
Expand Down
2 changes: 1 addition & 1 deletion lib/titan/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Titan
VERSION = "0.3.0"
VERSION = "0.4.0.dev"
end
17 changes: 17 additions & 0 deletions spec/titan/system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe Titan::System do
describe ".ps" do
context "given a pid that is greater than 0" do
it "should return a String" do
Titan::System.ps('rss', 1).should be_a(String)
end
end

context "given a pid that is equal 0" do
it "should return nil" do
Titan::System.ps('rss', 0).should be_nil
end
end
end
end
54 changes: 54 additions & 0 deletions spec/titan/thread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,60 @@ def new_thread(id=nil)
end
end

describe "#used_memory" do
before(:each) do
@thread = new_thread
end

it "should call Titan::System.ps" do
Titan::System.should_receive(:ps)
@thread.used_memory
end

context "given a pid exists" do
before(:each) do
@thread.run
end

it "should return an Integer" do
@thread.used_memory.should be_an(Integer)
end
end

context "given no pid exists" do
it "should return nil" do
@thread.used_memory.should be_nil
end
end
end

describe "#used_cpu" do
before(:each) do
@thread = new_thread
end

it "should call Titan::System.ps" do
Titan::System.should_receive(:ps)
@thread.used_cpu
end

context "given a pid exists" do
before(:each) do
@thread.run
end

it "should return a Float" do
@thread.used_cpu.should be_an(Float)
end
end

context "given no pid exists" do
it "should return nil" do
@thread.used_cpu.should be_nil
end
end
end

describe "#save" do
before(:each) do
@thread = new_thread
Expand Down

0 comments on commit 21d4ab6

Please sign in to comment.