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:async'; | 6 import 'dart:async'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
10 import 'dart:math'; | 10 import 'dart:math'; |
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
636 // with _currentBucket marking the bucket where an event was last | 636 // with _currentBucket marking the bucket where an event was last |
637 // recorded. A current sum of the content of all buckets except the | 637 // recorded. A current sum of the content of all buckets except the |
638 // one pointed a by _currentBucket is kept in _sum. | 638 // one pointed a by _currentBucket is kept in _sum. |
639 class Rate { | 639 class Rate { |
640 Rate([int timeRange = 1000, int buckets = 10]) | 640 Rate([int timeRange = 1000, int buckets = 10]) |
641 : _timeRange = timeRange, | 641 : _timeRange = timeRange, |
642 _buckets = new List.fixedLength(buckets + 1), // Current bucket is not in the sum. | 642 _buckets = new List.fixedLength(buckets + 1), // Current bucket is not in the sum. |
643 _currentBucket = 0, | 643 _currentBucket = 0, |
644 _currentBucketTime = new Date.now().millisecondsSinceEpoch, | 644 _currentBucketTime = new Date.now().millisecondsSinceEpoch, |
645 _sum = 0 { | 645 _sum = 0 { |
646 _bucketTimeRange = (_timeRange / buckets).toInt(); | 646 _bucketTimeRange = (_timeRange / buckets).truncate(); |
Lasse Reichstein Nielsen
2013/01/04 10:29:42
What happens if _timeRange is negative?
This shoul
| |
647 for (int i = 0; i < _buckets.length; i++) { | 647 for (int i = 0; i < _buckets.length; i++) { |
648 _buckets[i] = 0; | 648 _buckets[i] = 0; |
649 } | 649 } |
650 } | 650 } |
651 | 651 |
652 // Record the specified number of events. | 652 // Record the specified number of events. |
653 void record(int count) { | 653 void record(int count) { |
654 _timePassed(); | 654 _timePassed(); |
655 _buckets[_currentBucket] = _buckets[_currentBucket] + count; | 655 _buckets[_currentBucket] = _buckets[_currentBucket] + count; |
656 } | 656 } |
(...skipping 28 matching lines...) Expand all Loading... | |
685 } | 685 } |
686 } | 686 } |
687 | 687 |
688 int _timeRange; | 688 int _timeRange; |
689 List<int> _buckets; | 689 List<int> _buckets; |
690 int _currentBucket; | 690 int _currentBucket; |
691 int _currentBucketTime; | 691 int _currentBucketTime; |
692 num _bucketTimeRange; | 692 num _bucketTimeRange; |
693 int _sum; | 693 int _sum; |
694 } | 694 } |
OLD | NEW |