Skip to content

Commit

Permalink
add innodb_buffer_hit_rate command
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed Sep 6, 2013
1 parent d83e96f commit 5e7cec5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
23 changes: 21 additions & 2 deletions lib/mycmd/clis/status_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,27 @@ def size
desc "qcache_hit_rate", "qcache_hit_rate will print query cache hit rate"
def qcache_hit_rate
client = Configuration.connect
rate = client.query("SELECT IFNULL((SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100), 0) AS qcache_hit_rate")
puts "#{rate.first['qcache_hit_rate']} %"
rate = client.query("SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS qcache_hit_rate")
if rate.nil?
rate = "\e[31munknown\e[m"
else
rate = rate.first["qcache_hit_rate"]
rate = rate >= 20 ? "\e[32m#{rate} %\e[m" : "\e[31m#{rate} %\e[m"
end
puts rate
end

desc "innodb_buffer_hit_rate", "innodb_buffer_hit_rate will print buffer hit rate"
def innodb_buffer_hit_rate
client = Configuration.connect
rate = client.query("SELECT (1 - ((SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READS')/(SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READ_REQUESTS'))) * 100 AS innodb_buffer_hit_rate")
if rate.nil?
rate = "\e[31munknown\e[m"
else
rate = rate.first["innodb_buffer_hit_rate"]
rate = rate >= 90 ? "\e[32m#{rate} %\e[m" : "\e[31m#{rate} %\e[m"
end
puts rate
end
end
end
55 changes: 53 additions & 2 deletions spec/mycmd/clis/status_commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,59 @@
Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
end

it "should output the hit rate of query cache" do
conn_mock.should_receive(:query).with("SELECT IFNULL((SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100), 0) AS qcache_hit_rate").and_return(double.as_null_object)
it "should output the query cache hit rate" do
conn_mock.should_receive(:query).with("SELECT (SELECT (SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'QCACHE_HITS')/(SELECT SUM(G.VARIABLE_VALUE) FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME IN ('QCACHE_HITS','QCACHE_INSERTS','QCACHE_NOT_CACHED')) * 100) AS qcache_hit_rate").and_return(double.as_null_object)
end

it "should output red color of char if rate is unknown" do
conn_mock.should_receive(:query).and_return(nil)
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31munknown\e[m\n")
end

it "should output red color of char if rate < 20" do
conn_mock.should_receive(:query).and_return([{'qcache_hit_rate' => 10}])
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31m10 %\e[m\n")
end

it "should output green color of char if rate >= 20" do
conn_mock.should_receive(:query).and_return([{'qcache_hit_rate' => 20}])
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[32m20 %\e[m\n")
end
end

describe "#innodb_buffer_hit_rate" do
let(:args) {["innodb_buffer_hit_rate"]}

before do
conn_mock.stub(:query).and_return(double.as_null_object)
Mycmd::Configuration.stub(:connect).and_return(conn_mock)
end

after do
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).not_to be_nil
end

it "should call Configuration.#connect" do
Mycmd::Configuration.should_receive(:connect).and_return(conn_mock)
end

it "should output the innodb buffer hit rate" do
conn_mock.should_receive(:query).with("SELECT (1 - ((SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READS')/(SELECT G.VARIABLE_VALUE FROM INFORMATION_SCHEMA.GLOBAL_STATUS AS G WHERE G.VARIABLE_NAME = 'INNODB_BUFFER_POOL_READ_REQUESTS'))) * 100 AS innodb_buffer_hit_rate").and_return(double.as_null_object)
end

it "should output red color of char if rate is unknown" do
conn_mock.should_receive(:query).and_return(nil)
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31munknown\e[m\n")
end

it "should output red color of char if rate < 90" do
conn_mock.should_receive(:query).and_return([{'innodb_buffer_hit_rate' => 80}])
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[31m80 %\e[m\n")
end

it "should output green color of char if rate >= 90" do
conn_mock.should_receive(:query).and_return([{'innodb_buffer_hit_rate' => 90}])
expect(capture(:stdout){Mycmd::StatusCommands.start(args)}).to eq("\e[32m90 %\e[m\n")
end
end
end

0 comments on commit 5e7cec5

Please sign in to comment.