Advertisement
Guest User

benchmark.js

a guest
Feb 1st, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Benchmark = new Object();
  2. Benchmark.listeners = new Array();
  3.  
  4. Benchmark.listener = function() {
  5.     this.startTimes = new Array();
  6.     this.endTimes = new Array();
  7.    
  8.     this.getSec = function() {
  9.         var d = new Date();
  10.         return d.getTime();
  11.     };
  12.    
  13.     this.setStartTime = function() {
  14.         var d = new Date();
  15.         this.startTimes.push(d.getTime());
  16.     };
  17.     this.setEndTime = function() {
  18.         var d = new Date();
  19.         this.endTimes.push(d.getTime());
  20.     };
  21.    
  22.     this.getDelay1 = function(i) {
  23.         var a = this.startTimes.length;
  24.         var b = this.endTimes.length;
  25.         if(!i || i>=a || i>=b) var i = (a<=b?a:b)-1;
  26.         return this.endTimes[i]-this.startTimes[i];
  27.     }; 
  28.     this.getDelay2 = function(i) {
  29.         var a = this.startTimes.length;
  30.         if(!i || i>=a) i=a-1;
  31.         return this.startTimes[i]-this.startTimes[i-1];
  32.     };
  33.     this.getDelay3 = function(i) {
  34.         var b = this.endTimes.length;
  35.         if(!i || i>=b) i=b-1;
  36.         return this.endTimes[i]-this.endTimes[i-1];
  37.     };
  38.     this.getAvgDelay1 = function(n) {
  39.         var a = this.startTimes.length;
  40.         var b = this.endTimes.length;
  41.         var e = (a<=b?a:b); //end index
  42.        
  43.         var c=0;//counter
  44.         var s=0;//summa
  45.        
  46.         if(!n || n>= e || n<0) var n = e;
  47.         var i = e-n; // start index
  48.         while(i<e) {
  49.             if(i>0) {
  50.                 s+=(this.endTimes[i]-this.startTimes[i]);
  51.                 ++c;
  52.             }
  53.             ++i;
  54.         }  
  55.        
  56.         return (s/c);
  57.     };
  58.    
  59.     this.getAvgDelay2 = function(n) {
  60.         var a = this.startTimes.length;
  61.         var e = a; //end index
  62.        
  63.         var c=0;//counter
  64.         var s=0;//summa
  65.        
  66.         if(!n || n>= e || n<0) var n = e;
  67.         var i = e-n; // start index
  68.         while(i<e) {
  69.             if(i>0) {
  70.                 s+=(this.startTimes[i]-this.startTimes[i-1]);
  71.                 ++c;
  72.             }
  73.             ++i;
  74.         }  
  75.        
  76.         return 1/(s/c/1000);
  77.     };
  78.    
  79.    
  80.     this.getAvgDelay3 = function(n) {
  81.         var b = this.startTimes.length;
  82.         var e = b; //end index
  83.        
  84.         var c=0;//counter
  85.         var s=0;//summa
  86.        
  87.         if(!n || n>= e || n<0) var n = e;
  88.         var i = e-n; // start index
  89.         while(i<e) {
  90.             if(i>0) {
  91.                 s+=(this.endTimes[i]-this.endTimes[i-1]);
  92.                 ++c;
  93.             }
  94.             ++i;
  95.         }  
  96.        
  97.         return s/c;
  98.     };
  99. }
  100.  
  101. Benchmark.addListener = function() {
  102.     var listener = new Benchmark.listener()
  103.     Benchmark.listeners.push(listener);
  104.     return listener;
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement