基於Loadrunner平臺Socket協議的JavaVuser(多線程)

 

  1. /* 
  2.  * LoadRunner Java script. (Build: 15) 
  3.  * Script Description:  
  4.  * 
  5.  * 作者:谷博濤 
  6.  * 製作時間:2012-1-18 
  7.  * E-mail:[email protected] 
  8.  * Loadrunner:11.00 
  9.  *  
  10.  * 內容概要: 
  11.  * 模擬基於Socket協議的即時消息系統中的客戶端行爲LR腳本, 
  12.  * 爲模擬真實IM客戶端,接受消息和發送消息分兩個線程工作。 
  13.  *  
  14.  */ 
  15.  
  16. import lrapi.lr; 
  17. import java.io.IOException; 
  18. import java.io.InputStream; 
  19. import java.io.OutputStream; 
  20. import java.net.Socket; 
  21. import java.net.UnknownHostException; 
  22. import java.util.Iterator; 
  23. import java.util.concurrent.atomic.AtomicBoolean; 
  24.  
  25. public class Actions 
  26.     // 客戶端 
  27.     private Socket client; 
  28.     // 房間號 
  29.     private String roomId; 
  30.     // 用戶ID 
  31.     private String id; 
  32.     // 輸出流 
  33.     private OutputStream out; 
  34.     // 輸入流 
  35.     private InputStream in; 
  36.     // 連接標誌 
  37.     //private boolean connFlag = false; 
  38.     private  AtomicBoolean connFlag =new AtomicBoolean(false); 
  39.  
  40.     // 客戶端是否結束標誌 
  41.     //private boolean endFlag = true; 
  42.     private  AtomicBoolean endFlag =new AtomicBoolean(true); 
  43.  
  44.  
  45.     public int init() throws Throwable { 
  46.         connect();  
  47.     //lr.think_time(10); 
  48.         return 0
  49.     }//end of init 
  50.  
  51.  
  52.     public int action() throws Throwable { 
  53.         sendAction(); 
  54.         return 0
  55.     }//end of action 
  56.  
  57.  
  58.     public int end() throws Throwable { 
  59.         sendEnd(); 
  60.         return 0
  61.     }//end of end 
  62.  
  63. //====主題代碼==================// 
  64.  
  65.     //連接服務器 
  66.     private void connect() { 
  67.         try { 
  68.                 client = new Socket("127.0.0.1"5222); 
  69.                 System.out.println("connect success!!!"); 
  70.                 out = client.getOutputStream(); 
  71.                 in = client.getInputStream(); 
  72.                 receiveAction(); 
  73.                 login(); 
  74.         } catch (UnknownHostException e) { 
  75.                 System.out.println("UnknownHostException=" + e); 
  76.                 e.printStackTrace(); 
  77.         } catch (IOException e) { 
  78.                 System.out.println("IOException=" + e); 
  79.                 e.printStackTrace(); 
  80.         } 
  81.     } 
  82.  
  83.     //登錄服務器 
  84.     private void login() { 
  85.         String loginMsg = "<msg type=\"login\" channel=\"CCTV1\"></msg>"
  86.         sendMsg(loginMsg); 
  87.     } 
  88.  
  89.     //啓動接受線程 
  90.     private void receiveAction() { 
  91.     new ReceiveThread().start(); 
  92.     } 
  93.  
  94.     //得到房間號碼和用戶ID 
  95.     private void getEleVal(String msg) { 
  96.     int index =0
  97.  
  98.     index = msg.indexOf("to"); 
  99.     msg = msg.substring(index + 4, msg.length()); 
  100.     index = msg.indexOf("'"); 
  101.     id = msg.substring(0, index); 
  102.     System.out.println(roomId); 
  103.  
  104.     index = msg.indexOf("roomId"); 
  105.     msg = msg.substring(index + 8, msg.length()); 
  106.     index = msg.indexOf("'"); 
  107.     roomId = msg.substring(0, index); 
  108.     System.out.println(id); 
  109.  
  110.     connFlag.set(true); 
  111.     } 
  112.  
  113.     //發送消息 
  114.     private void sendAction() { 
  115.     if(connFlag.get()){ 
  116.         System.out.println("發送消息----->"); 
  117.         String msg = "<msg type=\"groupchat\" channel=\"CCTV1\" from=\"" + id 
  118.         + "\" to=\"" + roomId + "\"><body>test</body></msg>"
  119.      
  120.         sendMsg(msg); 
  121.     } 
  122.     } 
  123.  
  124.     //調用寫入流方法 
  125.     private void sendMsg(String msg) { 
  126.     //new SendThread(msg).start(); 
  127.     writeMsg(msg); 
  128.     } 
  129.  
  130.     //將消息寫入流 
  131.     private void writeMsg(String msg) { 
  132.     try { 
  133.         if (out != null) { 
  134.         out.write(msg.getBytes("UTF-8")); 
  135.         out.flush(); 
  136.         } 
  137.     } catch (Exception e) { 
  138.         System.out.println("Exception=" + e); 
  139.     } 
  140.     } 
  141.  
  142.     //關閉客戶端 
  143.     private void sendEnd() { 
  144.     endFlag.set(false); 
  145.     try { 
  146.         client.close(); 
  147.     } catch (IOException e) { 
  148.         // TODO Auto-generated catch block 
  149.         e.printStackTrace(); 
  150.     } 
  151.     } 
  152.     /** 
  153.      * 接收消息線程類 
  154.      *  
  155.      * @author Administrator 
  156.      *  
  157.      */ 
  158.     private class ReceiveThread extends Thread { 
  159.     @Override 
  160.     public void run() { 
  161.         while (endFlag.get()) {// 循環接收消息 
  162.             try { 
  163.             int len = in.available(); 
  164.  
  165.             if(len>0){ 
  166.                 System.out.println(len); 
  167.                 byte[] bytes = new byte[len]; 
  168.                 in.read(bytes); 
  169.                 String result = new String(bytes); 
  170.                 System.out.println("接收到的消息:" + result); 
  171.  
  172.                 if(result != null && !"".equals(result)&& result.contains("res_bind")){ 
  173.                 getEleVal(result); 
  174.                 } 
  175.             } 
  176.             } catch (Exception e) { 
  177.             // TODO: handle exception 
  178.             } 
  179.         } 
  180.     } 
  181.     } 
  182.  
  183.  
  184. //======發送消息線程類(本代碼未用到)========= 
  185.     private class SendThread extends Thread { 
  186.     private String msg; 
  187.      
  188.     public SendThread(String msg) { 
  189.         this.msg = msg; 
  190.     } 
  191.      
  192.     @Override 
  193.     public void run() { 
  194.         if (connFlag.get()) { 
  195.         writeMsg(msg); 
  196.         } 
  197.     } 
  198.     } 
  199.  
  200. }//end for class Actions 

 

谷博濤

二零一二年 農曆臘月廿五 下午

http://blog.csdn.net/captain_gbt/article/details/7209570

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章