| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library("chat_server"); | 5 #library("chat_server"); |
| 6 #import("dart:io"); | 6 #import("dart:io"); |
| 7 #import("dart:isolate"); | 7 #import("dart:isolate"); |
| 8 #import("dart:json"); | 8 #import("dart:json"); |
| 9 | 9 |
| 10 class ChatServer extends IsolatedServer { | 10 class ChatServer extends IsolatedServer { |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 // constructor arument specifies the time range in milliseconds. The | 628 // constructor arument specifies the time range in milliseconds. The |
| 629 // buckets are in the list _buckets organized at a circular buffer | 629 // buckets are in the list _buckets organized at a circular buffer |
| 630 // with _currentBucket marking the bucket where an event was last | 630 // with _currentBucket marking the bucket where an event was last |
| 631 // recorded. A current sum of the content of all buckets except the | 631 // recorded. A current sum of the content of all buckets except the |
| 632 // one pointed a by _currentBucket is kept in _sum. | 632 // one pointed a by _currentBucket is kept in _sum. |
| 633 class Rate { | 633 class Rate { |
| 634 Rate([int timeRange = 1000, int buckets = 10]) | 634 Rate([int timeRange = 1000, int buckets = 10]) |
| 635 : _timeRange = timeRange, | 635 : _timeRange = timeRange, |
| 636 _buckets = new List(buckets + 1), // Current bucket is not in the sum. | 636 _buckets = new List(buckets + 1), // Current bucket is not in the sum. |
| 637 _currentBucket = 0, | 637 _currentBucket = 0, |
| 638 _currentBucketTime = new Date.now().value, | 638 _currentBucketTime = new Date.now().millisecondsSinceEpoch, |
| 639 _sum = 0 { | 639 _sum = 0 { |
| 640 _bucketTimeRange = (_timeRange / buckets).toInt(); | 640 _bucketTimeRange = (_timeRange / buckets).toInt(); |
| 641 for (int i = 0; i < _buckets.length; i++) { | 641 for (int i = 0; i < _buckets.length; i++) { |
| 642 _buckets[i] = 0; | 642 _buckets[i] = 0; |
| 643 } | 643 } |
| 644 } | 644 } |
| 645 | 645 |
| 646 // Record the specified number of events. | 646 // Record the specified number of events. |
| 647 void record(int count) { | 647 void record(int count) { |
| 648 _timePassed(); | 648 _timePassed(); |
| 649 _buckets[_currentBucket] = _buckets[_currentBucket] + count; | 649 _buckets[_currentBucket] = _buckets[_currentBucket] + count; |
| 650 } | 650 } |
| 651 | 651 |
| 652 // Returns the current rate of events for the time range. | 652 // Returns the current rate of events for the time range. |
| 653 num get rate() { | 653 num get rate() { |
| 654 _timePassed(); | 654 _timePassed(); |
| 655 return _sum; | 655 return _sum; |
| 656 } | 656 } |
| 657 | 657 |
| 658 // Update the current sum as time passes. If time has passed by the | 658 // Update the current sum as time passes. If time has passed by the |
| 659 // current bucket add it to the sum and move forward to the bucket | 659 // current bucket add it to the sum and move forward to the bucket |
| 660 // matching the current time. Subtract all buckets vacated from the | 660 // matching the current time. Subtract all buckets vacated from the |
| 661 // sum as bucket for current time is located. | 661 // sum as bucket for current time is located. |
| 662 void _timePassed() { | 662 void _timePassed() { |
| 663 int time = new Date.now().value; | 663 int time = new Date.now().millisecondsSinceEpoch; |
| 664 if (time < _currentBucketTime + _bucketTimeRange) { | 664 if (time < _currentBucketTime + _bucketTimeRange) { |
| 665 // Still same bucket. | 665 // Still same bucket. |
| 666 return; | 666 return; |
| 667 } | 667 } |
| 668 | 668 |
| 669 // Add collected bucket to the sum. | 669 // Add collected bucket to the sum. |
| 670 _sum += _buckets[_currentBucket]; | 670 _sum += _buckets[_currentBucket]; |
| 671 | 671 |
| 672 // Find the bucket for the current time. Subtract all buckets | 672 // Find the bucket for the current time. Subtract all buckets |
| 673 // reused from the sum. | 673 // reused from the sum. |
| 674 while (time >= _currentBucketTime + _bucketTimeRange) { | 674 while (time >= _currentBucketTime + _bucketTimeRange) { |
| 675 _currentBucket = (_currentBucket + 1) % _buckets.length; | 675 _currentBucket = (_currentBucket + 1) % _buckets.length; |
| 676 _sum -= _buckets[_currentBucket]; | 676 _sum -= _buckets[_currentBucket]; |
| 677 _buckets[_currentBucket] = 0; | 677 _buckets[_currentBucket] = 0; |
| 678 _currentBucketTime += _bucketTimeRange; | 678 _currentBucketTime += _bucketTimeRange; |
| 679 } | 679 } |
| 680 } | 680 } |
| 681 | 681 |
| 682 int _timeRange; | 682 int _timeRange; |
| 683 List<int> _buckets; | 683 List<int> _buckets; |
| 684 int _currentBucket; | 684 int _currentBucket; |
| 685 int _currentBucketTime; | 685 int _currentBucketTime; |
| 686 num _bucketTimeRange; | 686 num _bucketTimeRange; |
| 687 int _sum; | 687 int _sum; |
| 688 } | 688 } |
| OLD | NEW |