setTimeout 에 파라미터를 넘기는 법

1. string로 만들어 호출하는 법
   setTimeout( fn 및 param에 대한 스트링, 딜레이시간)   

* 기본 호출

    setTimeout("updateTimeout('value')", 5000);

 * 반복 호출

   setTimeout("updateTimeout(0)", 5000);

   function updateTimeout(i) {

       if (i == 100) return;

       $("#updateVal").val(i++);

       setTimeout("updateTimeout('"+i+"')"5000);
   }

       
    
 2. 외부에서 파라미터 전달(전역변수 사용과 큰 차이가 없다)

 for (var i = 1; i < 100; i++) {

          (function(i) {
               setTimeout(function() {
                     $("#updateVal").val(i);
                }, i * 5000);
         }(i));
    }

 
3. setTimeout 을 재구현

   기본적으로 setTimeout  의 3번째 파라미터 부터는 setTimeout에서 호출하는 function 의 파라미터로 전달 된다. 단. IE 를 제외한 다른 브라우져에서는 전달이 가능하다.

즉 아래와 같은 형태가 된다.

  setTimeout(fn, delayTime , param1, param2 ...)  

  fn(param1, param2 ...)
  {
     내부 구현
  }


  IE 에서 이기능을 제공하기 위해서는  setTimeout 을 재구현하여 사용할 수가 있다.

  var orgTimeout = setTimeout;

   window.setTimeout = function() {
      var func, delay;
      func= arguments[0];
      delay= arguments[1];

      if (arguments.length > 2) {
         var arg = Array.prototype.slice.call(arguments, 2);
         return orgTimeout (function() {
                    func.apply(null, arg);
                }, delay);
       } else {
               return orgTimeout (func, delay);
      }
    };
   
    setTimeout(updateTimeout, 5000, 0);   // 호출가능 해짐


'IT > javascript' 카테고리의 다른 글

String fommat 2  (0) 2015.08.03
javascript 의 arguments  (0) 2014.12.03
javascript 에서 string.format 사용  (0) 2014.11.24
yuicommpressor (js /css 압축)  (0) 2014.11.24
setTimeout , setInterval , clearTimeout , clearInterval 사용  (0) 2014.11.22

기본 카운터의 구현은 이전 포스트에서 다른적이 있다. 

 

hadoop 에서는 이미 counter 를 많이 사용하고 있다.

hadoop 에 jar를 올려 실행하면 실행결과 최종에 나오는 값들이 모두 카운터를 통해 구현된 값이다. 

해당 jar 실행시 아래와 같은 최종 결과가 나온다

counter 는 총 43개 이며 해당 카운터에 mapreduce 프로그램이 동작할 사용한 주요 값들을 확인할 수 있다.

대표적으로 map, combine, reduce 에 input 된 record 수와 output된 record 수를 확인 할 수 있있으며, 실행시간 shuffle 된 byte 수도 확인이 가능한다.

이것을 통해 튜닝포인트를 잡을 수가 있게 된다.

 

기본 실행 결과 예시> combiner 를 구현하지 않은 경우

INFO mapreduce.Job: Counters: 43

        File System Counters

                FILE: Number of bytes read=37834707

                FILE: Number of bytes written=76663746

                FILE: Number of read operations=0

                FILE: Number of large read operations=0

                FILE: Number of write operations=0

                HDFS: Number of bytes read=1397535171

                HDFS: Number of bytes written=21

                HDFS: Number of read operations=36

                HDFS: Number of large read operations=0

                HDFS: Number of write operations=2

        Job Counters

                Launched map tasks=11

                Launched reduce tasks=1

                Data-local map tasks=11

                Total time spent by all maps in occupied slots (ms)=88360

                Total time spent by all reduces in occupied slots (ms)=11532

        Map-Reduce Framework

                Map input records=2007594

                Map output records=2007593

                Map output bytes=33819515

                Map output materialized bytes=37834767

                Input split bytes=1474

                Combine input records=0

                Combine output records=0

                Reduce input groups=4

                Reduce shuffle bytes=37834767

                Reduce input records=2007593

                Reduce output records=1

                Spilled Records=4015186

                Shuffled Maps =11

                Failed Shuffles=0

                Merged Map outputs=11

                GC time elapsed (ms)=1029

                CPU time spent (ms)=26570

                Physical memory (bytes) snapshot=10722308096

                Virtual memory (bytes) snapshot=23048646656

                Total committed heap usage (bytes)=12089032704

        Shuffle Errors

                BAD_ID=0

                CONNECTION=0

                IO_ERROR=0

                WRONG_LENGTH=0

                WRONG_MAP=0

                WRONG_REDUCE=0

        File Input Format Counters

                Bytes Read=1397533697

        File Output Format Counters

                Bytes Written=21

위의 결과를 보면 combiner 가 구현되지 않았기 때문에 combine input records 와 output records 가 0 이며,

reduce 에서 shuffle 된 bytes 와 records 수가 매우 높은 것을 알 수 있다.

그에 따라 reduce 에서 해당 값을 처리하기 위해 11532 ms 를 소모했다.

 

아래 결과는 combiner 를 구현한 경우이다.

INFO mapreduce.Job: Counters: 43

        File System Counters

                FILE: Number of bytes read=908

                FILE: Number of bytes written=998468

                FILE: Number of read operations=0

                FILE: Number of large read operations=0

                FILE: Number of write operations=0

                HDFS: Number of bytes read=1397535171

                HDFS: Number of bytes written=21

                HDFS: Number of read operations=36

                HDFS: Number of large read operations=0

                HDFS: Number of write operations=2

        Job Counters

                Launched map tasks=11

                Launched reduce tasks=1

                Data-local map tasks=11

                Total time spent by all maps in occupied slots (ms)=86586

                Total time spent by all reduces in occupied slots (ms)=5220

        Map-Reduce Framework

                Map input records=2007594

                Map output records=2007593

                Map output bytes=33819515

                Map output materialized bytes=968

                Input split bytes=1474

                Combine input records=2007593

                Combine output records=44

                Reduce input groups=4

                Reduce shuffle bytes=968

                Reduce input records=44

                Reduce output records=1

                Spilled Records=88

                Shuffled Maps =11

                Failed Shuffles=0

                Merged Map outputs=11

                GC time elapsed (ms)=1057

                CPU time spent (ms)=24930

                Physical memory (bytes) snapshot=10712588288

                Virtual memory (bytes) snapshot=23029968896

                Total committed heap usage (bytes)=12059148288

        Shuffle Errors

                BAD_ID=0

                CONNECTION=0

                IO_ERROR=0

                WRONG_LENGTH=0

                WRONG_MAP=0

                WRONG_REDUCE=0

        File Input Format Counters

                Bytes Read=1397533697

        File Output Format Counters

                Bytes Written=21

첫번째의 결과와 다른 것은 combiner (각 datanode 에서 동작하여 분산처리됨)의 구현을 통해 reduce 에서 취합되는 데이터의 양을 최소화 시켰다. 즉 combine output 은 44 records 로 줄였으며, 그로 인해 reduce 의 input shuffle 등의 값이 현저하게 주는 것을 확인할 수 있다.

결과를 보면 maps 의 소모시간은 조금 증가하였지만, reduce 의 소모시간은 반으로 준 것을 확인할 수 있다. 

이와 같이 counter 는 개발자의 데이터 공유뿐만 아니라 mapreduce 동작의 기본값을 확인 할 수 있기 때문에 활용도가 매우 높다.

이런 기본 counter 값도 reduce 동작중 획득하여 사용할 수 있다.

+ Recent posts