<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模型的實現(xiàn),是不是也可以對比下?
    如:
    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
    :),新的語言更強調充分發(fā)揮多核的優(yōu)勢,提升語言本身在并行、并發(fā)方面的支持,盡管java也在提升,但畢竟java已經(jīng)龐大了,所以必然要遲緩一些。  回復  更多評論   

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

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

    公告

     









    feedsky
    抓蝦
    google reader
    鮮果

    導航

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

    統(tǒng)計

    隨筆分類

    隨筆檔案

    文章檔案

    Blogger's

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 女人让男人免费桶爽30分钟| 亚洲国产精品尤物YW在线观看| 一本天堂ⅴ无码亚洲道久久| 免费观看午夜在线欧差毛片| 国产午夜成人免费看片无遮挡 | jlzzjlzz亚洲jzjzjz| 午夜两性色视频免费网站| 一级毛片免费视频网站| 亚洲天天在线日亚洲洲精| 思思99re66在线精品免费观看| www在线观看播放免费视频日本| 亚洲av日韩av无码| 日本免费v片一二三区| 国产高清不卡免费视频| 一本色道久久综合亚洲精品蜜桃冫 | 亚洲国产av一区二区三区| 日本在线免费播放| 亚洲精品无码久久久久久| 久久91亚洲人成电影网站| 在线视频免费观看www动漫| 久操免费在线观看| 男男gvh肉在线观看免费| 亚洲一区二区电影| 免费人成在线观看视频播放| 51精品视频免费国产专区| 特级毛片全部免费播放a一级 | MM1313亚洲精品无码久久| 久久久无码精品亚洲日韩蜜桃| 日本高清免费中文字幕不卡| 久久99精品视免费看| 国产精品亚洲专区一区| 亚洲日韩乱码中文无码蜜桃| 亚洲综合色婷婷七月丁香| 免费涩涩在线视频网| 最近中文字幕电影大全免费版| 久久久受www免费人成| 亚洲av无码专区在线电影天堂| 亚洲日韩中文字幕天堂不卡 | 污网站在线免费观看| 亚洲一区二区三区四区视频| 亚洲国产另类久久久精品小说 |