performancedemo.java

来自「全面实现ilog地功能,没有使用第三方lib.」· Java 代码 · 共 133 行

JAVA
133
字号
/* * This source code is part of TWaver 1.3.1 * * SERVA Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * Copyright 2000-2005 SERVA Software, Inc. All rights reserved. */package demo.network;

import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Iterator;import javax.swing.JButton;import javax.swing.JOptionPane;import twaver.AlarmSeverity;import twaver.Element;import twaver.Link;import twaver.Node;import twaver.TDataBox;import twaver.network.TNetwork;import demo.DemoPane;
public class PerformanceDemo extends DemoPane {
    TDataBox box = new TDataBox();
    TNetwork network = new TNetwork(box);
    JButton button = new JButton("Press me to start...");

    public PerformanceDemo() {
        this.add(network, BorderLayout.CENTER);
        this.add(button, BorderLayout.SOUTH);        button.setBackground(new Color(0, 0, 91));        button.setForeground(Color.GREEN);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                test();
            }
        });
    }

    private void test() {
        box.clear();

        int x = 30, y = 30;
        int gap = 50;

        //create nodes
        int counter = 0;
        long time = System.currentTimeMillis();
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                Node node = new Node("node-" + i + "-" + j);
                node.setLocation(gap * (i + 1), gap * (j + 1));
                box.addElement(node);
                counter++;
            }
        }
        JOptionPane.showMessageDialog(this,
                                      "Create " + counter +
                                      " nodes, time cost: " +
                                      (System.currentTimeMillis() - time));
        time = System.currentTimeMillis();
        counter = 0;
        //create links.
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (i > 0) {
                    Node from = (Node) box.getElementByID("node-" + i + "-" + j);
                    Node to = (Node) box.getElementByID("node-" + (i - 1) + "-" + j);
                    Link link = new Link(from, to);
                    box.addElement(link);
                    counter++;
                }
                if (j > 0) {
                    Node from = (Node) box.getElementByID("node-" + i + "-" + j);
                    Node to = (Node) box.getElementByID("node-" + i + "-" + (j - 1));
                    Link link = new Link(from, to);
                    box.addElement(link);
                    counter++;
                }
            }
        }
        JOptionPane.showMessageDialog(this,
                                      "Create " + counter +
                                      " links, time cost: " +
                                      (System.currentTimeMillis() - time));
        //change alarm state for each element.
        time = System.currentTimeMillis();
        counter = 0;
        //raise alarms.
        Iterator it = box.iterator();
        while (it.hasNext()) {
            Element element = (Element) it.next();
            element.getAlarmState().addNewAlarm(AlarmSeverity.CRITICAL);
            counter++;
        }
        JOptionPane.showMessageDialog(this,
                                      "Raise alarm for " + counter +
                                      " elements, time cost: " +
                                      (System.currentTimeMillis() - time));
        //clear alarms.
        time = System.currentTimeMillis();
        counter = 0;
        it = box.iterator();
        while (it.hasNext()) {
            Element element = (Element) it.next();
            element.getAlarmState().clear();
            counter++;
        }
        JOptionPane.showMessageDialog(this,
                                      "Clear alarm state for " + counter +
                                      " elements, time cost: " +
                                      (System.currentTimeMillis() - time));
        //clear box.
        time = System.currentTimeMillis();
        box.clear();
        JOptionPane.showMessageDialog(this,
                                      "Clear data box, time cost: " +
                                      (System.currentTimeMillis() - time));
    }

    public String getTitle() {
        return "Subnetwork Performance Demo";
    }

    public String getHelp() {
        return "This demo creates a great deal of elements and update it to test" +
            " the performance of network component.";
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?