Sky's blog

          我和我追逐的夢

          導航

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(8)

          隨筆分類

          隨筆檔案

          閱讀排行榜

          評論排行榜

          常用鏈接

          統計

          其他鏈接

          友情鏈接

          最新評論

          ejb與java序列化(2)--測試代碼

                  接上篇,有興趣的朋友可以直接拿我的測試代碼自行測試,請自行修改諸如線程數,執行時間,序列化的數據量大小等參數。如果想嘗試做thread dump,可以打開相關的兩個注釋,會更方便一些,代碼中都有相應的注釋可供參考。

          測試代碼如下:
          package test;

          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.io.ObjectOutputStream;
          import java.io.Serializable;
          import java.util.ArrayList;

          public class Test implements Runnable {
              
          //Notice! set the three test parameter to what you want first
              /**
               * thread count to run test
               
          */
              
          private static final int THREAD_COUNT = 50;
              
          /**
               * time in seconds to run test
               
          */
              
          private static final long TEST_TIME_SECOND = 1 * 30;
              
          /**
               * during test, we serialize a Data instance with an ArrayList that contains DataItem instance.
               * This is to set how many DataItem in the ArrayList.
               
          */
              
          private static final long ITEMS_COUNT_IN_TEST_OBJECT = 1000;
              

              
          private static int finishedCount = 0;
              
          private static boolean needStop = false;
              
          private static Object needStopLock = new Object();
              
          private static Object finishedCountLock = new Object();

              
          private static boolean isNeedStop() {
                  
          synchronized (needStopLock) {
                      
          return needStop;
                  }
              }

              
          private static void setNeedStop() {
                  
          synchronized (needStopLock) {
                      needStop 
          = true;
                  }
              }

              
          private static void addFinisedCount() {
                  
          synchronized (finishedCountLock) {
                      finishedCount
          ++;
                  }
              }

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          // run it first to load all the class
                  new Test().test();
                  
          // to dump thread open these
                  
          // try {
                  
          // Thread.sleep(20 * 1000);
                  
          // System.out.println("main sleep. go to find pid, we need it later to send signal");
                  
          // Thread.sleep(2 * 1000);
                  
          // System.out.println("prepard to dump");
                  
          // } catch (InterruptedException e) {
                  
          // e.printStackTrace();
                  
          // }

                  
          long timeBegin = System.currentTimeMillis();
                  
          for (int i = 0; i < THREAD_COUNT; i++) {
                      Thread t 
          = new Thread(new Test());
                      t.setName(
          "testthread" + i);
                      t.start();
                  }

                  
          long timeEnd = timeBegin + TEST_TIME_SECOND * 1000;
                  
          while (System.currentTimeMillis() < timeEnd) {
                      
          try {
                          Thread.sleep(
          50);
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
                  setNeedStop();
                  System.out.println(THREAD_COUNT 
          + " thread finished " + finishedCount
                          
          + " times in " + TEST_TIME_SECOND + " seconds");

                  
          // to dump thread open these
                  
          // try {
                  
          // Thread.sleep(5 * 1000);
                  
          // //System.out.println("dump now");
                  
          // } catch (InterruptedException e) {
                  
          // e.printStackTrace();
                  
          // }
              }

              
          public void run() {
                  
          while (!isNeedStop()) {
                      test();
                      addFinisedCount();
                  }
              }

              
          private void test() {
                  ByteArrayOutputStream bos 
          = new ByteArrayOutputStream();
                  Data data 
          = new Data();

                  
          try {
                      
          // long time1 = System.currentTimeMillis();
                      ObjectOutputStream oos = new ObjectOutputStream(bos);
                      oos.writeObject(data);
                      bos.toByteArray();
                      
          // long time2 = System.currentTimeMillis();

                      
          // System.out.print((time2 - time1) + " ");

                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
              }

              
          private static class Data implements Serializable {
                  
          private static final long serialVersionUID = -376987039014824563L;
                  
          private static final ArrayList DEFAULT = new ArrayList();
                  
          static {
                      
          for (int i = 0; i < ITEMS_COUNT_IN_TEST_OBJECT; i++) {
                          DEFAULT.add(
          new DataItem(i));
                          
          // DEFAULT.add(DataItem.a + i);
                      }
                  }
                  
          private ArrayList content = DEFAULT;
              }

              
          private static class DataItem implements Serializable {
                  
          private static final long serialVersionUID = 1L;
                  
          private static final String a = "sdfsdfsdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
                  
          private int number;
                  
          private String content;

                  
          public DataItem(int number) {
                      
          this.number = number;
                      
          this.content = a + number;
                  }
              }
          }

          也可以從這里直接下載到完整的eclipse項目,除上面的代碼外,還有thread dump文件和已經設置好的jprobe的配置文件。
          (blogjava不能上傳文件,所以只好放fs2you)
          http://www.fs2you.com/files/59a26119-5d1a-11dd-ad4f-0014221b798a/

          posted on 2008-07-29 10:36 sky ao 閱讀(1170) 評論(0)  編輯  收藏 所屬分類: ejb

          主站蜘蛛池模板: 尚志市| 新河县| 定安县| 吕梁市| 曲沃县| 即墨市| 元谋县| 桐柏县| 竹溪县| 马尔康县| 秦安县| 龙海市| 蕲春县| 罗田县| 峨眉山市| 吴川市| 江永县| 英山县| 万荣县| 芒康县| 义乌市| 手机| 大方县| 辽源市| 南投市| 襄樊市| 石家庄市| 广安市| 鄂州市| 昆明市| 玉田县| 鱼台县| 大宁县| 克东县| 漳浦县| 新密市| 和平县| 元谋县| 田东县| 怀集县| 星子县|