<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Simple Scala actor Vs java Thread Vs Kilim Test

    In this blog,I'll test the coroutine supported on jvm,now famous is scala actor & kilim,this blog show the program reliazed with scala actor/kilim/java,let's compare these three program performance.

    Test scene:
    1.start cpu+1 threads;
    2.these threads notify processor,repeat (cpu+1)*100 times,every time notify sleep random(5) ms;
    3.processor asynchronous handle the message,the handler process is create a List(1000),and add element to this list,then sleep random(2) ms,repeat the process 100 times,then finish the processor.

    Test Machine I: 2 core CPU(Intel(R) Xeon(R) CPU  E5410  @ 2.33GHz) 1.5G memory
    Test Env: JDK 1.6.0_07、linux kernel 2.6.9
    Execute result:
    1. java version: 8397ms    cpu us: about 84%  sy: about 8% 
    2. scala version: 12403ms   cpu us: about 70%  sy: about 13%
    3. kilim version: 10959ms   cpu us: about 70%  sy: about 14%

    Test Machine II: 8 core CPU(Intel(R) Xeon(R) CPU  E5410  @ 2.33GHz) 4G memory
    Test Env: JDK 1.6.0_14、linux kernel 2.6.9
    Execute result:
    1. java version: 36797ms    cpu us: about 80%  sy: about 3% 
    2. scala version: 19722ms   cpu us: about 70%  sy: about 1.5%
    3. kilim version: 16008ms   cpu us: about 85%  sy: about 1.8%

    Execute summary:
    When cpu core and thread adds, scala & kilim performance will be better than java version, It show scala & kilim can use multicore cpu more effectivly, kilim performance sees better than scala in this scene.

    If you have some tunning suggest,pls let me know,thks.

    java version code:
    public class MicroBenchmark {

        
    private static final int processorCount=Runtime.getRuntime().availableProcessors()+1;
        
        
    private static Processor processor=new Processor();
        
        
    private static CountDownLatch latch=null;
        
        
    private static Random random=new Random();
        
        
    private static int executeTimes=processorCount*100;
        
        
    public static void main(String[] args) throws Exception{
            
    long beginTime=System.currentTimeMillis();
            latch
    =new CountDownLatch(executeTimes*processorCount);
            
    for (int j = 0; j < processorCount; j++) {
                
    new Thread(new NotifyProcessorTask()).start();
            }
            latch.await();
            
    long endTime=System.currentTimeMillis();
            System.out.println(
    "execute time: "+(endTime-beginTime)+" ms");
        }
        
        
    static class NotifyProcessorTask implements Runnable{

            
    public void run() {
                
    for (int i = 0; i < executeTimes; i++) {
                    processor.messageReceived(latch);
                    
    try {
                        Thread.sleep(random.nextInt(
    5));
                    } 
                    
    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            
        }

    }

    public class Processor {

        
    private static final int maxThread=(Runtime.getRuntime().availableProcessors()+1)*100;
        
        
    private static ThreadPoolExecutor executor=null;
        
        
    private Random random=new Random();
        
        
    public Processor(){
            executor
    =new ThreadPoolExecutor(10,maxThread,5,TimeUnit.SECONDS,new SynchronousQueue<Runnable>(),new ThreadPoolExecutor.AbortPolicy());
        }
        
        
    public void messageReceived(final CountDownLatch latch){
            Runnable task
    =new Runnable(){

                
    public void run() {
                    
    try {
                        handle();
                    } 
                    
    catch (Exception e) {
                        e.printStackTrace();
                    }
                    latch.countDown();
                }
                
            };
            executor.execute(task);
        }
        
        
    private void handle() throws Exception{
            
    int counter=0;
            
    while(counter<100){
                List
    <String> list=new ArrayList<String>();
                
    for (int i = 0; i < 1000; i++) {
                    list.add(String.valueOf(i));
                }
                counter
    ++;
                Thread.sleep(random.nextInt(
    2));
            }
        }
        
    }

    scala version code:
    object MicroBenchmark {
      
      val processorCount
    =Runtime.getRuntime.availableProcessors+1
      val executeTimes
    =processorCount*100
      
      def main(args : Array[String]) : Unit 
    = {
        val beginTime
    =System.currentTimeMillis
        val latchCount
    =executeTimes*processorCount
        var latch
    =new CountDownLatch(latchCount)
        var i
    =0
        
    while(i<processorCount){
          
    new Thread(new NotifyProcessorTask(latch)).start
          i
    +=1
        }
        latch.await()
        val endTime
    =System.currentTimeMillis;
        println(
    "execute time: "+(endTime-beginTime)+"ms")
        exit
      }
    }

    class NotifyProcessorTask(latch:CountDownLatch) extends Runnable{
        val executeTimes
    =(Runtime.getRuntime.availableProcessors+1)*100
        val random
    =new Random
        val actor
    =new Processor
        
        def run() {
          var i
    =0
          actor.start
          
    while(i < executeTimes){
            actor 
    ! TaskMessage.TaskExecuted(latch,0)    
            i
    +=1
            Thread.sleep(random.nextInt(
    5))
          }
        }
    }

    abstract sealed class TaskMessage
    object TaskMessage {
      
    case class TaskExecuted(latch: CountDownLatch, counter:Int) extends TaskMessage
    }

    class Processor extends Actor{

      var random
    =new Random
      
      def act(){
        loop {
          react {
            
    case TaskMessage.TaskExecuted(latch,counter) => handle(latch,counter)
          }
        }
      }
      
      def handle(latch: CountDownLatch,counter: Int){
        val list
    =new Array[String](1000)
        var i
    =0
        
    for(i<-0 to 999){
          list(i)
    =i.toString
        }
        var selfCounter
    =counter;
        
    if(selfCounter<99){
            selfCounter
    +=1
            
    new Sleeper(random.nextInt(2),this,selfCounter,latch).start
        }
        
    else{
            latch.countDown()
        }
      }
      
    }

    class Sleeper(time: Long, origin:Actor, counter:Int, latch:CountDownLatch) extends Actor {
        def act() {
            reactWithin(time) {
                
    case TIMEOUT =>
                     origin 
    ! TaskMessage.TaskExecuted(latch,counter)
              }
        }
    }

    kilim code version:
    public class MicroBenchmark {

        
    private static final int processorCount=Runtime.getRuntime().availableProcessors()+1;
        
        
    private static CountDownLatch latch=null;
        
        
    private static Random random=new Random();
        
        
    private static int executeTimes=processorCount*100;
        
        
    public static void main(String[] args) throws Exception{
            
    long beginTime=System.currentTimeMillis();
            latch
    =new CountDownLatch(executeTimes*processorCount);
            
    for (int j = 0; j < processorCount; j++) {
                
    new Thread(new NotifyProcessorTask()).start();
            }
            latch.await();
            
    long endTime=System.currentTimeMillis();
            System.out.println(
    "execute time: "+(endTime-beginTime)+" ms");
            System.exit(
    0);
        }
        
        
    static class NotifyProcessorTask implements Runnable{

            
    public void run() {
                String threadName
    =Thread.currentThread().getName();
                
    for (int i = 0; i < executeTimes; i++) {
                    Mailbox
    <String> messages=new Mailbox<String>();
                    
    new Processor(messages,latch).start();
                    messages.putnb(threadName
    +String.valueOf(i));
                    
    try {
                        Thread.sleep(random.nextInt(
    5));
                    } 
                    
    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            
        }

    }

    public class Processor extends Task{
        
        
    private Random random=new Random();
        
        
    private Mailbox<String> messages;
        
        
    private CountDownLatch latch;
        
        
    public Processor(Mailbox<String> messages,CountDownLatch latch){
            
    this.messages=messages;
            
    this.latch=latch;
        }
        
        
    public void execute() throws Pausable, Exception {
            messages.get();
            
    int counter=0;
            
    while(counter<100){
                List
    <String> list=new ArrayList<String>();
                
    for (int i = 0; i < 1000; i++) {
                    list.add(String.valueOf(i));
                }
                counter
    ++;
                Task.sleep(random.nextInt(
    2));
            }
            latch.countDown();
            Task.exit(
    0);
        }
        
    }

    posted on 2009-11-25 17:23 BlueDavy 閱讀(7739) 評論(7)  編輯  收藏 所屬分類: Java

    評論

    # re: 簡單的scala actor和java多線程處理的測試 2009-11-26 10:10 lazycai

    kersterl? kestrel?
    文章不錯!  回復  更多評論   

    # re: 簡單的scala actor和java多線程處理的測試 2009-11-26 10:15 xz

    java下其實也有基于actor模型的實現,是不是也可以對比下?
    如:
    http://www.infoq.com/cn/news/2008/07/kilim-message-passing-in-java  回復  更多評論   

    # re: 簡單的scala actor和java多線程處理的測試 2009-11-26 10:20 lazycai

    順便,邀請一下:https://groups.google.com/group/scalacn  回復  更多評論   

    # re: 簡單的scala actor和java多線程處理的測試 2009-11-26 10:25 BlueDavy

    @xz
    恩,好,之后折騰下。  回復  更多評論   

    # re: 簡單的scala actor和java多線程處理的測試 2009-11-26 11:08 dd_macle

    真是長江后浪推前浪  回復  更多評論   

    # re: 簡單的scala actor和java多線程處理的測試 2009-11-26 11:43 BlueDavy

    @dd_macle
    :),新的語言更強調充分發揮多核的優勢,提升語言本身在并行、并發方面的支持,盡管java也在提升,但畢竟java已經龐大了,所以必然要遲緩一些。  回復  更多評論   

    # re: Simple Scala actor Vs java Thread Vs Kilim Test 2009-11-26 16:45 BlueDavy

    @xz
    呵呵,增加了kilim的測試,性能表現竟然超過了scala.  回復  更多評論   

    公告

     









    feedsky
    抓蝦
    google reader
    鮮果

    導航

    <2009年11月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    統計

    隨筆分類

    隨筆檔案

    文章檔案

    Blogger's

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 97公开免费视频| 亚洲国产高清在线一区二区三区| 亚洲熟妇成人精品一区| 免费v片在线观看品善网| 中文字幕无码毛片免费看| 久久久亚洲裙底偷窥综合| 日韩在线视频免费看| 国产免费内射又粗又爽密桃视频| 久久精品国产亚洲AV大全| 日本免费一区二区三区最新 | 亚洲国产女人aaa毛片在线| 久久久久免费看黄A片APP| 无码日韩人妻AV一区免费l| 666精品国产精品亚洲| 四虎影院永久免费观看| 午夜免费福利视频| 日韩色视频一区二区三区亚洲| 亚洲国产精品一区二区成人片国内 | 夜夜爽妓女8888视频免费观看| 亚洲国产精品lv| 亚洲AV日韩精品一区二区三区 | 国产一区二区三区免费| 亚洲国产精品美女久久久久| 亚洲国产日韩在线视频| 日日AV拍夜夜添久久免费| 久9这里精品免费视频| 免费毛片毛片网址| 中文文字幕文字幕亚洲色| 亚洲国产精品福利片在线观看| 国产免费久久精品| 在线观看日本免费a∨视频| 国产免费爽爽视频在线观看| 精品女同一区二区三区免费播放| 亚洲色成人网一二三区| 亚洲人成网站在线观看播放| 宅男666在线永久免费观看 | 国产亚洲精品激情都市| 国产福利免费在线观看| 免费中文熟妇在线影片| 永久黄色免费网站| 久久国产免费一区二区三区|