redis-stat 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -13,7 +13,7 @@ gem install redis-stat
13
13
  ```
14
14
  usage: redis-stat [HOST[:PORT]] [INTERVAL [COUNT]]
15
15
 
16
- --csv=OUTPUT_CSV_FILE_PATH Saves the result in CSV format
16
+ --csv=OUTPUT_CSV_FILE_PATH Save the result in CSV format
17
17
  -v, --verbose Show more info
18
18
  --version Show version
19
19
  --help Show this message
data/lib/redis-stat.rb CHANGED
@@ -22,18 +22,19 @@ class RedisStat
22
22
  @os = output_stream
23
23
  trap('INT') { Thread.main.raise Interrupt }
24
24
 
25
- @csv = File.open(@options[:csv], 'w') if @options[:csv]
25
+ csv = File.open(@options[:csv], 'w') if @options[:csv]
26
26
  update_term_size!
27
27
 
28
+ prev_info = nil
28
29
  begin
29
30
  @started_at = Time.now
30
31
  loop do
31
- @info = @redis.info.insensitive
32
- @info[:at] = Time.now.to_f
32
+ info = @redis.info.insensitive
33
+ info[:at] = Time.now.to_f
33
34
 
34
- output @info, @prev_info, @csv
35
+ output info, prev_info, csv
35
36
 
36
- @prev_info = @info
37
+ prev_info = info
37
38
  @count += 1
38
39
  break if @max_count && @count >= @max_count
39
40
  sleep @options[:interval]
@@ -46,7 +47,7 @@ class RedisStat
46
47
  @os.puts ansi(:red, :bold) { e.to_s }
47
48
  exit 1
48
49
  ensure
49
- @csv.close if @csv
50
+ csv.close if csv
50
51
  end
51
52
  @os.puts ansi(:blue, :bold) {
52
53
  "Elapsed: #{"%.2f" % (Time.now - @started_at)} sec."
@@ -55,6 +56,18 @@ class RedisStat
55
56
 
56
57
  private
57
58
  def update_term_size!
59
+ if RUBY_PLATFORM.match(/java/)
60
+ require 'java'
61
+ begin
62
+ @term ||= Java::jline.Terminal.getTerminal
63
+ @term_width = (@term.getTerminalWidth rescue DEFAULT_TERM_WIDTH)
64
+ @term_height = (@term.getTerminalHeight rescue DEFAULT_TERM_HEIGHT) - 4
65
+ return
66
+ rescue Exception
67
+ # Fallback to tput (which yields incorrect values as of now)
68
+ end
69
+ end
70
+
58
71
  @term_width = (`tput cols` rescue DEFAULT_TERM_WIDTH).to_i
59
72
  @term_height = (`tput lines` rescue DEFAULT_TERM_HEIGHT).to_i - 4
60
73
  end
@@ -230,19 +243,17 @@ private
230
243
  end.to_s
231
244
  end
232
245
 
233
- def humanize_number bytes, k = 1000, suffix = ''
234
- return '-' if bytes.nil?
246
+ def humanize_number num, k = 1000, suffix = ''
247
+ return '-' if num.nil?
235
248
 
236
- bytes = bytes.to_f
237
- if bytes < k
238
- format_number(bytes)
239
- elsif bytes < k ** 2
240
- format_number(bytes / k) + 'K' + suffix
241
- elsif bytes < k ** 3
242
- format_number(bytes / k ** 2) + 'M' + suffix
243
- else
244
- format_number(bytes / k ** 3) + 'G' + suffix
249
+ sign = num >= 0 ? '' : '-'
250
+ num = num.abs
251
+ mult = k.to_f
252
+ ['', 'K', 'M', 'G', 'T', 'P', 'E'].each do |mp|
253
+ return sign + format_number(num * k / mult) + mp + suffix if num < mult || mp == 'E'
254
+ mult *= k
245
255
  end
256
+ return nil
246
257
  end
247
258
 
248
259
  def ansi *args, &block
@@ -1,3 +1,3 @@
1
1
  class RedisStat
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -4,6 +4,39 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
  require 'redis-stat'
5
5
 
6
6
  class TestRedisStat < Test::Unit::TestCase
7
+ def test_humanize_number
8
+ rs = RedisStat.new
9
+ assert_equal '0', rs.send(:humanize_number, 0.00)
10
+ assert_equal '7', rs.send(:humanize_number, 7)
11
+ assert_equal '0.01', rs.send(:humanize_number, 0.00751)
12
+ assert_equal '0.08', rs.send(:humanize_number, 0.0751)
13
+ assert_equal '0.75', rs.send(:humanize_number, 0.751)
14
+ assert_equal '7.51', rs.send(:humanize_number, 7.51)
15
+ assert_equal '75.1', rs.send(:humanize_number, 75.1)
16
+ assert_equal '7.51K', rs.send(:humanize_number, 7510)
17
+ assert_equal '75.1K', rs.send(:humanize_number, 75100)
18
+ assert_equal '751K', rs.send(:humanize_number, 751000)
19
+ assert_equal '7.51M', rs.send(:humanize_number, 7510000)
20
+ assert_equal '75.1M', rs.send(:humanize_number, 75100000)
21
+ assert_equal '751M', rs.send(:humanize_number, 751000000)
22
+ assert_equal '7.51G', rs.send(:humanize_number, 7510000000)
23
+ assert_equal '75.1G', rs.send(:humanize_number, 75100000000)
24
+ assert_equal '751G', rs.send(:humanize_number, 751000000000)
25
+ assert_equal '7.51T', rs.send(:humanize_number, 7510000000000)
26
+ assert_equal '75.1T', rs.send(:humanize_number, 75100000000000)
27
+ assert_equal '751T', rs.send(:humanize_number, 751000000000000)
28
+ assert_equal '7.51P', rs.send(:humanize_number, 7510000000000000)
29
+ assert_equal '75.1P', rs.send(:humanize_number, 75100000000000000)
30
+ assert_equal '751P', rs.send(:humanize_number, 751000000000000000)
31
+ assert_equal '7.51E', rs.send(:humanize_number, 7510000000000000000)
32
+ assert_equal '75.1E', rs.send(:humanize_number, 75100000000000000000)
33
+ assert_equal '751E', rs.send(:humanize_number, 751000000000000000000)
34
+ assert_equal '7510E', rs.send(:humanize_number, 7510000000000000000000)
35
+
36
+ assert_equal '7.51PB', rs.send(:humanize_number, 7.51 * (1024 ** 5), 1024, 'B')
37
+ assert_equal '-7.51PB', rs.send(:humanize_number, -7.51 * (1024 ** 5), 1024, 'B')
38
+ end
39
+
7
40
  def test_option_parse
8
41
  options = RedisStat::Option.parse([])
9
42
  assert_equal RedisStat::Option::DEFAULT.sort, options.sort
@@ -66,8 +99,14 @@ class TestRedisStat < Test::Unit::TestCase
66
99
  end
67
100
 
68
101
  def test_start
69
- rs = RedisStat.new :interval => 0.1, :count => 200, :verbose => true, :csv => '/tmp/redis-stat.csv'
102
+ csv = '/tmp/redis-stat.csv'
103
+ cnt = 50
104
+ rs = RedisStat.new :interval => 0.01, :count => cnt, :verbose => true, :csv => csv
70
105
  rs.start $stdout
106
+
107
+ assert_equal cnt + 1, File.read(csv).lines.to_a.length
108
+ ensure
109
+ File.unlink csv
71
110
  end
72
111
  end
73
112
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-23 00:00:00.000000000 Z
12
+ date: 2012-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ansi