Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: to get graph of system performance using java pls help me

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post to get graph of system performance using java pls help me

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.awt.geom.Line2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.lang.management.ManagementFactory;
    import java.lang.management.MemoryPoolMXBean;
    import java.util.Date;
     
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
     
     
    public class Performance extends JPanel {
     
      static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp");
     
      public Surface surf;
     
      JPanel controls;
     
      boolean doControls;
     
      JTextField tf;
     
      // Get memory pools.
      static java.util.List<MemoryPoolMXBean> mpools = ManagementFactory.getMemoryPoolMXBeans();
     
      // Total number of memory pools.
      static int numPools = mpools.size();
     
      public Performance() {
        setLayout(new BorderLayout());
        setBorder(new TitledBorder(new EtchedBorder(), "Performance"));
        add(surf = new Surface());
        controls = new JPanel();
        controls.setPreferredSize(new Dimension(135, 80));
        Font font = new Font("serif", Font.PLAIN, 10);
        JLabel label = new JLabel("Sample Rate");
        label.setFont(font);
        label.setForeground(Color.red);
        controls.add(label);
        tf = new JTextField("1000");
        tf.setPreferredSize(new Dimension(45, 20));
        controls.add(tf);
        controls.add(label = new JLabel("ms"));
        label.setFont(font);
        label.setForeground(Color.red);
        controls.add(dateStampCB);
        dateStampCB.setFont(font);
        addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            removeAll();
            if ((doControls = !doControls)) {
              surf.stop();
              add(controls);
            } else {
              try {
                surf.sleepAmount = Long.parseLong(tf.getText().trim());
              } catch (Exception ex) {
              }
              surf.start();
              add(surf);
            }
            validate();
            repaint();
          }
        });
      }
     
      public class Surface extends JPanel implements Runnable {
     
        public Thread thread;
     
        public long sleepAmount = 1000;
     
        public int usageHistCount = 20000;
     
        private int w, h;
     
        private BufferedImage bimg;
     
        private Graphics2D big;
     
        private Font font = new Font("Times New Roman", Font.PLAIN, 11);
     
        private int columnInc;
     
        private float usedMem[][];
     
        private int ptNum[];
     
        private int ascent, descent;
     
        private Rectangle graphOutlineRect = new Rectangle();
     
        private Rectangle2D mfRect = new Rectangle2D.Float();
     
        private Rectangle2D muRect = new Rectangle2D.Float();
     
        private Line2D graphLine = new Line2D.Float();
     
        private Color graphColor = new Color(46, 139, 87);
     
        private Color mfColor = new Color(0, 100, 0);
     
        private String usedStr;
     
        public Surface() {
          setBackground(Color.black);
          addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
              if (thread == null)
                start();
              else
                stop();
            }
          });
          int i = 0;
          usedMem = new float[numPools][];
          ptNum = new int[numPools];
        }
     
        public Dimension getMinimumSize() {
          return getPreferredSize();
        }
     
        public Dimension getMaximumSize() {
          return getPreferredSize();
        }
     
        public Dimension getPreferredSize() {
          return new Dimension(135, 80);
        }
     
        public void paint(Graphics g) {
     
          if (big == null) {
            return;
          }
     
          big.setBackground(getBackground());
          big.clearRect(0, 0, w, h);
     
          h = h / ((numPools + numPools % 2) / 1);
          w = w / 1;
     
          int k = 0; // index of memory pool.
          for (int i = 0; i < 2; i++) {
            for (int j = 0; j < (numPools + numPools % 2) / 1; j++) {
              plotMemoryUsage(w * i, h * j, w, h, k);
              if (++k >= numPools) {
                i = 3;
                j = (numPools + numPools % 2) / 1;
                break;
              }
            }
          }
          g.drawImage(bimg, 0, 0, this);
        }
     
        public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) {
     
          MemoryPoolMXBean mp = mpools.get(npool);
          float usedMemory = mp.getUsage().getUsed();
          float totalMemory = mp.getUsage().getMax();
     
          // .. Draw allocated and used strings ..
          big.setColor(Color.green);
     
          // Print Max memory allocated for this memory pool.
          big.drawString(String.valueOf((int) totalMemory / 1024) + "K Max ", x1 + 4.0f, (float) y1
              + ascent + 0.5f);
          big.setColor(Color.yellow);
     
          // Print the memory pool name.
          big.drawString(mp.getName(), x1 + x2 / 1, (float) y1 + ascent + 0.5f);
     
          // Print the memory used by this memory pool.
          usedStr = String.valueOf((int) usedMemory / 1024) + "K used";
          big.setColor(Color.green);
          big.drawString(usedStr, x1 + 4, y1 + y2 - descent);
     
          // Calculate remaining size
          float ssH = ascent + descent;
          float remainingHeight = (float) (y2 - (ssH * 2) - 0.5f);
          float blockHeight = remainingHeight / 10;
          float blockWidth = 20.0f;
          float remainingWidth = (float) (x2 - blockWidth - 10);
     
          // .. Memory Free ..
          big.setColor(mfColor);
          int MemUsage = (int) (((totalMemory - usedMemory) / totalMemory) * 10);
          int i = 0;
          for (; i < MemUsage; i++) {
            mfRect.setRect(x1 + 5, (float) y1 + ssH + i * blockHeight, blockWidth,
                (float) blockHeight - 1);
            big.fill(mfRect);
          }
     
          // .. Memory Used ..
          big.setColor(Color.green);
          for (; i < 10; i++) {
            muRect.setRect(x1 + 5, (float) y1 + ssH + i * blockHeight, blockWidth,
                (float) blockHeight - 1);
            big.fill(muRect);
          }
     
          // .. Draw History Graph ..
          if (remainingWidth <= 30)
            remainingWidth = (float) 30;
          if (remainingHeight <= ssH)
            remainingHeight = (float) ssH;
          big.setColor(graphColor);
          int graphX = x1 + 30;
          int graphY = y1 + (int) ssH;
          int graphW = (int) remainingWidth;
          int graphH = (int) remainingHeight;
     
          graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
          big.draw(graphOutlineRect);
     
          int graphRow = graphH / 10;
     
          // .. Draw row ..
          for (int j = graphY; j <= graphH + graphY; j += graphRow) {
            graphLine.setLine(graphX, j, graphX + graphW, j);
            big.draw(graphLine);
          }
     
          // .. Draw animated column movement ..
          int graphColumn = graphW / 15;
     
          if (columnInc == 0) {
            columnInc = graphColumn;
          }
     
          for (int j = graphX + columnInc; j < graphW + graphX; j += graphColumn) {
            graphLine.setLine(j, graphY, j, graphY + graphH);
            big.draw(graphLine);
          }
     
          --columnInc;
     
          // Plot memory usage by this memory pool.
          if (usedMem[npool] == null) {
            usedMem[npool] = new float[usageHistCount];
            ptNum[npool] = 0;
          }
     
          // save memory usage history.
          usedMem[npool][ptNum[npool]] = usedMemory;
     
          big.setColor(Color.yellow);
     
          int w1; // width of memory usage history.
          if (ptNum[npool] > graphW) {
            w1 = graphW;
          } else {
            w1 = ptNum[npool];
          }
     
          for (int j = graphX + graphW - w1, k = ptNum[npool] - w1; k < ptNum[npool]; k++, j++) {
            if (k != 0) {
              if (usedMem[npool][k] != usedMem[npool][k - 1]) {
                int h1 = (int) (graphY + graphH * ((totalMemory - usedMem[npool][k - 1]) / totalMemory));
                int h2 = (int) (graphY + graphH * ((totalMemory - usedMem[npool][k]) / totalMemory));
                big.drawLine(j - 1, h1, j, h2);
              } else {
                int h1 = (int) (graphY + graphH * ((totalMemory - usedMem[npool][k]) / totalMemory));
                big.fillRect(j, h1, 1, 1);
              }
            }
          }
          if (ptNum[npool] + 2 == usedMem[npool].length) {
            // throw out oldest point
            for (int j = 1; j < ptNum[npool]; j++) {
              usedMem[npool][j - 1] = usedMem[npool][j];
            }
            --ptNum[npool];
          } else {
            ptNum[npool]++;
          }
        }
     
        public void start() {
          thread = new Thread(this);
          thread.setPriority(Thread.MIN_PRIORITY);
          thread.setName("Performance");
          thread.start();
        }
     
        public synchronized void stop() {
          thread = null;
          notify();
        }
     
        public void run() {
     
          Thread me = Thread.currentThread();
     
          while (thread == me && !isShowing() || getSize().width == 0) {
            try {
              thread.sleep(500);
            } catch (InterruptedException e) {
              return;
            }
          }
     
          while (thread == me && isShowing()) {
            Dimension d = getSize();
            if (d.width != w || d.height != h) {
              w = d.width;
              h = d.height;
              bimg = (BufferedImage) createImage(w, h);
              big = bimg.createGraphics();
              big.setFont(font);
              FontMetrics fm = big.getFontMetrics(font);
              ascent = (int) fm.getAscent();
              descent = (int) fm.getDescent();
            }
            repaint();
            try {
              thread.sleep(sleepAmount);
            } catch (InterruptedException e) {
              break;
            }
            if (Performance.dateStampCB.isSelected()) {
              System.out.println(new Date().toString() + " " + usedStr);
            }
          }
          thread = null;
        }
      }
     
      // Test thread to consume memory
      static class Memeater extends ClassLoader implements Runnable {
        Object y[];
     
        public Memeater() {
        }
     
        public void run() {
          y = new Object[10000000];
          int k = 0;
          while (true) {
            if (k == 5000000)
              k = 0;
            y[k++] = new Object();
            try {
              Thread.sleep(20);
            } catch (Exception x) {
            }
     
            // to consume perm gen storage
            try {
              // the classes are small so we load 10 at a time
              for (int i = 0; i < 10; i++) {
                loadNext();
              }
            } catch (ClassNotFoundException x) {
              // ignore exception
            }
     
          }
     
        }
     
        Class loadNext() throws ClassNotFoundException {
     
          // public class TestNNNNNN extends java.lang.Object{
          // public TestNNNNNN();
          // Code:
          // 0: aload_0
          // 1: invokespecial #1; //Method java/lang/Object."<init>":()V
          // 4: return
          // }
     
          int begin[] = { 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30, 0x00, 0x0a, 0x0a, 0x00, 0x03,
              0x00, 0x07, 0x07, 0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06, 0x3c, 0x69, 0x6e, 0x69,
              0x74, 0x3e, 0x01, 0x00, 0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43, 0x6f, 0x64, 0x65,
              0x0c, 0x00, 0x04, 0x00, 0x05, 0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 };
     
          int end[] = { 0x01, 0x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f,
              0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00,
              0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04, 0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00,
              0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1,
              0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
     
          // TestNNNNNN
     
          String name = "Test" + Integer.toString(count++);
     
          byte value[];
          try {
            value = name.substring(4).getBytes("UTF-8");
          } catch (java.io.UnsupportedEncodingException x) {
            throw new Error();
          }
     
          // construct class file
     
          int len = begin.length + value.length + end.length;
          byte b[] = new byte[len];
          int i, pos = 0;
          for (i = 0; i < begin.length; i++) {
            b[pos++] = (byte) begin[i];
          }
          for (i = 0; i < value.length; i++) {
            b[pos++] = value[i];
          }
          for (i = 0; i < end.length; i++) {
            b[pos++] = (byte) end[i];
          }
     
          return defineClass(name, b, 0, b.length);
     
        }
     
        static int count = 100000;
     
      }
     
      public static void main(String s[]) {
        final MemoryMonitor demo = new MemoryMonitor();
        WindowListener l = new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
     
          public void windowDeiconified(WindowEvent e) {
            demo.surf.start();
          }
     
          public void windowIconified(WindowEvent e) {
            demo.surf.stop();
          }
        };
        JFrame f = new JFrame("Performance");
        f.addWindowListener(l);
        f.getContentPane().add("Center", demo);
        f.pack();
        f.setSize(new Dimension(400, 500));
        f.setVisible(true);
        demo.surf.start();
        Thread thr = new Thread(new Memeater());
        thr.start();
      }
     
    }
    Last edited by Norm; March 6th, 2013 at 07:54 AM. Reason: <> changed to [] in code tags


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: to get graph of system performance using java pls help me

    Do you have a particular question about your program? What is in your code that is not working correctly?

    Format your code with code tags:

    [C0DE=java]
    <code>
    [/CODE]

Similar Threads

  1. To get graph of System performance in java
    By keerthana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 2nd, 2013, 01:19 AM
  2. Mysql query performance issue in java batch
    By siva.wins in forum JDBC & Databases
    Replies: 3
    Last Post: January 9th, 2013, 12:32 PM
  3. How to speed up the performance of the java application
    By Sathiyan in forum Java Theory & Questions
    Replies: 4
    Last Post: December 2nd, 2011, 02:09 AM
  4. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  5. Java Network performance analyser program help
    By Tark221 in forum Java Theory & Questions
    Replies: 4
    Last Post: March 22nd, 2011, 01:02 PM