SlowdownTest.java
     1: //========================================================================================
     2: //  SlowdownTest.java
     3: //    en:Slowdown test
     4: //    ja:鈍化テスト
     5: //  Copyright (C) 2003-2017 Makoto Kamada
     6: //
     7: //  This file is part of the XEiJ (X68000 Emulator in Java).
     8: //  You can use, modify and redistribute the XEiJ if the conditions are met.
     9: //  Read the XEiJ License for more details.
    10: //  http://stdkmd.com/xeij/
    11: //========================================================================================
    12: 
    13: //----------------------------------------------------------------------------------------
    14: //  コアのスレッドにダミーのタスクを走らせることでコアのタスクが動ける時間を減らす
    15: //  MPUの動作周波数を上げる負荷テストとは異なり、遅いマシンで動かしたときにしか生じない問題がわかるかも知れない
    16: //----------------------------------------------------------------------------------------
    17: 
    18: package xeij;
    19: 
    20: import java.awt.event.*;  //ActionEvent,ActionListener,ComponentAdapter,ComponentEvent,ComponentListener,FocusAdapter,FocusEvent,FocusListener,InputEvent,KeyAdapter,KeyEvent,KeyListener,MouseAdapter,MouseEvent,MouseListener,MouseMotionAdapter,MouseWheelEvent,WindowAdapter,WindowEvent,WindowListener,WindowStateListener
    21: import java.lang.*;  //Boolean,Character,Class,Comparable,Double,Exception,Float,IllegalArgumentException,Integer,Long,Math,Number,Object,Runnable,SecurityException,String,StringBuilder,System
    22: import java.util.*;  //ArrayList,Arrays,Calendar,GregorianCalendar,HashMap,Map,Map.Entry,Timer,TimerTask,TreeMap
    23: import javax.swing.*;  //AbstractButton,AbstractSpinnerModel,Box,ButtonGroup,DefaultListModel,ImageIcon,JApplet,JButton,JCheckBox,JCheckBoxMenuItem,JComponent,JDialog,JFileChooser,JFrame,JLabel,JList,JMenu,JMenuBar,JMenuItem,JPanel,JRadioButton,JScrollPane,JSpinner,JTextArea,JTextField,JTextPane,JViewport,ScrollPaneConstants,SpinnerListModel,SpinnerNumberModel,SwingConstants,SwingUtilities,UIManager,UIDefaults,UnsupportedLookAndFeelException
    24: import javax.swing.event.*;  //CaretListener,ChangeEvent,ChangeListener,DocumentEvent,DocumentListener,ListSelectionListener
    25: 
    26: public class SlowdownTest {
    27: 
    28:   public static long sdtNano;
    29:   public static TimerTask sdtTask;
    30: 
    31:   public static JCheckBoxMenuItem sdtCheckBoxMenuItem;
    32:   public static SpinnerNumberModel sdtModel;
    33:   public static JSpinner sdtSpinner;
    34:   public static Box sdtBox;
    35: 
    36:   public static void sdtInit () {
    37:     sdtNano = 0L;
    38:     sdtTask = null;
    39: 
    40:     sdtCheckBoxMenuItem =
    41:       Multilingual.mlnText (
    42:         ComponentFactory.createCheckBoxMenuItem (
    43:           false,
    44:           "Slowdown Test",
    45:           new ActionListener () {
    46:             @Override public void actionPerformed (ActionEvent ae) {
    47:               if (((JCheckBoxMenuItem) ae.getSource ()).isSelected ()) {
    48:                 sdtStart ();
    49:               } else {
    50:                 sdtStop ();
    51:               }
    52:             }
    53:           }),
    54:         "ja", "鈍化テスト");
    55:     sdtModel = new SpinnerNumberModel (0.0, 0.0, 99.9, 0.1);
    56:     sdtSpinner = ComponentFactory.createNumberSpinner (sdtModel, 4, new ChangeListener () {
    57:       @Override public void stateChanged (ChangeEvent ce) {
    58:         sdtCheckBoxMenuItem.setSelected (true);
    59:         sdtStart ();
    60:       }
    61:     });
    62:     sdtBox = ComponentFactory.createHorizontalBox (
    63:       Box.createHorizontalStrut (20),
    64:       sdtSpinner,
    65:       ComponentFactory.createLabel ("%"),
    66:       Box.createHorizontalGlue ()
    67:       );
    68:   }  //sdtInit()
    69: 
    70:   //sdtStart ()
    71:   //  鈍化テストを開始する
    72:   public static void sdtStart () {
    73:     sdtStop ();
    74:     sdtNano = XEiJ.TMR_INTERVAL * Math.round (10000.0 * sdtModel.getNumber ().doubleValue ());
    75:     sdtTask = new TimerTask () {
    76:       @Override public void run () {
    77:         long limit = System.nanoTime () + sdtNano;
    78:         while (System.nanoTime () < limit) {
    79:         }
    80:       }
    81:     };
    82:     XEiJ.tmrTimer.scheduleAtFixedRate (sdtTask, XEiJ.TMR_DELAY, XEiJ.TMR_INTERVAL);
    83:   }  //sdtStart()
    84: 
    85:   //sdtStop ()
    86:   //  鈍化テストを終了する
    87:   public static void sdtStop () {
    88:     if (sdtTask != null) {
    89:       sdtTask.cancel ();
    90:       sdtTask = null;
    91:     }
    92:   }  //sdtStop()
    93: 
    94: }  //class SlowdownTest
    95: 
    96: 
    97: