ejb与java序列化(2)--测试代码

接上篇,有兴趣的朋友可以直接拿我的测试代码自行测试,请自行修改诸如线程数,执行时间,序列化的数据量大小等参数。如果想尝试做thread dump,可以打开相关的两个注释,会更方便一些,代码中都有相应的注释可供参考。

测试代码如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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/

你可能感兴趣的:(java,eclipse,thread,ejb,Go)