-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
206 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
benchmark-framework/src/main/java/io/openmessaging/benchmark/utils/UniformRateLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.openmessaging.benchmark.utils; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLongFieldUpdater; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
/** | ||
* Provides a next operation time for rate limited operation streams.<br> | ||
* The rate limiter is thread safe and can be shared by all threads. | ||
*/ | ||
public final class UniformRateLimiter { | ||
|
||
private static final AtomicLongFieldUpdater<UniformRateLimiter> V_TIME_UPDATER = | ||
AtomicLongFieldUpdater.newUpdater(UniformRateLimiter.class, "virtualTime"); | ||
private static final AtomicLongFieldUpdater<UniformRateLimiter> START_UPDATER = | ||
AtomicLongFieldUpdater.newUpdater(UniformRateLimiter.class, "start"); | ||
private static final double ONE_SEC_IN_NS = TimeUnit.SECONDS.toNanos(1); | ||
private volatile long start = Long.MIN_VALUE; | ||
private volatile long virtualTime; | ||
private final double opsPerSec; | ||
private final long intervalNs; | ||
|
||
public UniformRateLimiter(final double opsPerSec) { | ||
if (Double.isNaN(opsPerSec) || Double.isInfinite(opsPerSec)) { | ||
throw new IllegalArgumentException("opsPerSec cannot be Nan or Infinite"); | ||
} | ||
if (opsPerSec <= 0) { | ||
throw new IllegalArgumentException("opsPerSec must be greater then 0"); | ||
} | ||
this.opsPerSec = opsPerSec; | ||
intervalNs = Math.round(ONE_SEC_IN_NS / opsPerSec); | ||
} | ||
|
||
public double getOpsPerSec() { | ||
return opsPerSec; | ||
} | ||
|
||
public long getIntervalNs() { | ||
return intervalNs; | ||
} | ||
|
||
public long acquire() { | ||
final long currOpIndex = V_TIME_UPDATER.getAndIncrement(this); | ||
long start = this.start; | ||
if (start == Long.MIN_VALUE) { | ||
start = System.nanoTime(); | ||
if (!START_UPDATER.compareAndSet(this, Long.MIN_VALUE, start)) { | ||
start = this.start; | ||
assert start != Long.MIN_VALUE; | ||
} | ||
} | ||
return start + currOpIndex * intervalNs; | ||
} | ||
|
||
public static void uninterruptibleSleepNs(final long intendedTime) { | ||
long sleepNs; | ||
while ((sleepNs = (intendedTime - System.nanoTime())) > 0) { | ||
LockSupport.parkNanos(sleepNs); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.