|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import com.google.common.base.MoreObjects; |
| 20 | +import java.time.Duration; |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.PriorityQueue; |
| 24 | + |
| 25 | +/** |
| 26 | + * A simple moving window implementation which will keep a {@code window}s worth of Throughput |
| 27 | + * values and allow querying for the aggregate avg over that time window. |
| 28 | + */ |
| 29 | +final class ThroughputMovingWindow { |
| 30 | + |
| 31 | + private final Duration window; |
| 32 | + |
| 33 | + private final PriorityQueue<Entry> values; |
| 34 | + |
| 35 | + private ThroughputMovingWindow(Duration window) { |
| 36 | + this.window = window; |
| 37 | + this.values = new PriorityQueue<>(Entry.COMP); |
| 38 | + } |
| 39 | + |
| 40 | + void add(Instant now, Throughput value) { |
| 41 | + removeExpiredEntries(now); |
| 42 | + values.add(new Entry(now, value)); |
| 43 | + } |
| 44 | + |
| 45 | + Throughput avg(Instant now) { |
| 46 | + removeExpiredEntries(now); |
| 47 | + return values.stream() |
| 48 | + .map(Entry::getValue) |
| 49 | + .reduce( |
| 50 | + Throughput.zero(), |
| 51 | + (tp1, tp2) -> Throughput.of(tp1.getNumBytes() + tp2.getNumBytes(), window)); |
| 52 | + } |
| 53 | + |
| 54 | + private void removeExpiredEntries(Instant now) { |
| 55 | + Instant newMin = now.minus(window); |
| 56 | + values.removeIf(e -> lteq(e.getAt(), newMin)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String toString() { |
| 61 | + return MoreObjects.toStringHelper(this) |
| 62 | + .add("window", window) |
| 63 | + .add("values.size()", values.size()) |
| 64 | + .toString(); |
| 65 | + } |
| 66 | + |
| 67 | + static ThroughputMovingWindow of(Duration window) { |
| 68 | + return new ThroughputMovingWindow(window); |
| 69 | + } |
| 70 | + |
| 71 | + private static boolean lteq(Instant a, Instant b) { |
| 72 | + return a.equals(b) || a.isBefore(b); |
| 73 | + } |
| 74 | + |
| 75 | + private static final class Entry { |
| 76 | + private static final Comparator<Entry> COMP = Comparator.comparing(e -> e.at); |
| 77 | + private final Instant at; |
| 78 | + private final Throughput value; |
| 79 | + |
| 80 | + private Entry(Instant at, Throughput value) { |
| 81 | + this.at = at; |
| 82 | + this.value = value; |
| 83 | + } |
| 84 | + |
| 85 | + public Instant getAt() { |
| 86 | + return at; |
| 87 | + } |
| 88 | + |
| 89 | + public Throughput getValue() { |
| 90 | + return value; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String toString() { |
| 95 | + return MoreObjects.toStringHelper(this).add("at", at).add("value", value).toString(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments