Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simple memory graphing app

  • Loading branch information...
commit ad08a9f82dec34cbbccc2b23c988699758972198 1 parent 9f9b09e
@nicksieger authored
View
6 Rakefile
@@ -14,6 +14,12 @@ namespace :jmx do
require 'rubyadvent/jmxexamples'
JMXExamples.print_memory_usage
end
+
+ task :memory do
+ require 'rubyadvent/gruffexamples'
+ require 'rubyadvent/jmxexamples'
+ GruffExamples.memory_visualizer
+ end
end
task :irb => :requires do
View
87 lib/java2d/image.rb
@@ -4,46 +4,75 @@ module Java2d
import javax.imageio.ImageIO
import javax.swing.JFrame
+ class ImagePanel < javax.swing.JPanel
+ def initialize(image, x=0, y=0)
+ super()
+ @image, @x, @y = image, x, y
+ end
+
+ def image=(image)
+ @image = image
+ repaint
+ end
+
+ def getPreferredSize
+ java.awt.Dimension.new(@image.width, @image.height)
+ end
+
+ def paintComponent(graphics)
+ graphics.draw_image(@image.java_image, @x, @y, nil)
+ end
+ end
+
+ class WindowClosed
+ def initialize(block = nil)
+ @block = block || proc { java.lang.System.exit(0) }
+ end
+ def method_missing(meth,*args); end
+ def windowClosing(event); @block.call; end
+ end
+
class Image
- class JImagePanel < javax.swing.JPanel
- def initialize(image, x=0, y=0)
- super()
- @image, @x, @y = image, x, y
- end
+ def initialize(bytes)
+ @bytes = bytes
+ end
- def image=(image)
- @image = image
- invalidate
- end
+ def width
+ java_image.width
+ end
- def getPreferredSize
- java.awt.Dimension.new(@image.width, @image.height)
- end
+ def height
+ java_image.height
+ end
- def paintComponent(graphics)
- graphics.draw_image(@image, @x, @y, nil)
+ def java_image
+ unless @image
+ bytes = String === @bytes ? @bytes.to_java_bytes : @bytes
+ @image = ImageIO.read(ByteArrayInputStream.new(bytes))
end
+ @image
end
+ end
- class WindowClosed
- def initialize(block = nil)
- @block = block || proc { java.lang.System.exit(0) }
- end
- def method_missing(meth,*args); end
- def windowClosing(event); @block.call; end
+ class ImageApp
+ attr_accessor :title
+
+ def initialize(image = nil)
+ @title = "Preview"
+ @image = image
end
- def initialize(bytes)
- bytes = bytes.to_java_bytes if String === bytes
- @image = ImageIO.read(ByteArrayInputStream.new(bytes))
+ def image=(image)
+ @image = image
+ @panel.image = image if @panel
end
- def preview(&block)
- frame = JFrame.new("Preview")
- frame.add_window_listener WindowClosed.new(block)
- frame.set_bounds 0, 0, @image.width + 20, @image.height + 40
- frame.add JImagePanel.new(@image, 10, 10)
- frame.visible = true
+ def show(&block)
+ @frame = JFrame.new(@title)
+ @frame.add_window_listener WindowClosed.new(block)
+ @frame.set_bounds 0, 0, @image.width + 20, @image.height + 40
+ @panel = @frame.add(ImagePanel.new(@image, 10, 10))
+ @frame.visible = true
end
end
end
View
27 lib/rubyadvent/gruffexamples.rb
@@ -2,6 +2,12 @@
require 'gruff'
require 'java2d/image'
+class Gruff::Base
+ def to_image
+ Java2d::Image.new(to_blob)
+ end
+end
+
class GruffExamples
def self.simple
g = Gruff::Line.new
@@ -14,6 +20,25 @@ def self.simple
g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}
- Java2d::Image.new(g.to_blob).preview
+ app = Java2d::ImageApp.new(g.to_image)
+ app.title = g.title
+ app.show
+ end
+
+ def self.memory_visualizer
+ @app = Java2d::ImageApp.new
+ @app.title = "Memory"
+ @sampler = JMXExamples::MemorySampler.new do |s|
+ g = Gruff::Line.new
+ g.title = @app.title
+ g.y_axis_label = "MB"
+ s.samples.each_pair {|key,val| g.data(key, val) }
+ g.labels = {}
+ g.hide_dots = true
+ @app.image = g.to_image
+ @app.show unless @started
+ @started = true
+ end
+ @sampler.start
end
end
View
36 lib/rubyadvent/jmxexamples.rb
@@ -1,12 +1,16 @@
require 'jmx'
class JMXExamples
- def self.client
- @client ||= JMX::MBeanServer.new
+ def self.server(*args)
+ @server ||= if args.length > 0
+ JMX.connect(*args)
+ else
+ JMX::MBeanServer.new
+ end
end
def self.memory_bean
- @memory_bean ||= client["java.lang:type=Memory"]
+ @memory_bean ||= server["java.lang:type=Memory"]
end
def self.in_mb(value)
@@ -22,7 +26,29 @@ def self.print_memory_usage
puts "Heap: #{in_mb(heap)}, Non-Heap: #{in_mb(non_heap)}"
end
- def self.print_jruby_config
-
+ class MemorySampler
+ def initialize(&block)
+ @samples = []
+ @keep = 50
+ @interval = 2
+ @callback = block
+ end
+
+ def samples
+ dat = @samples.dup.transpose
+ { "Heap" => dat.first, "Non-Heap" => dat.last }
+ end
+
+ def start
+ @thread = Thread.new do
+ while true do
+ @samples << JMXExamples.memory_usage.map {|m| m.to_f / (1024*1024) }
+ @samples.shift if @samples.size > @keep
+ @callback.call(self) if @callback
+ sleep @interval
+ end
+ end
+ @thread.join
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.