xx:+uselwpsynchronization”会对windows操作系统上的stamplock产生负面影响?

x3naxklr  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(450)

我想用java实现一个简单的速率限制器来学习如何使用jmh。一个简单的github项目在'https://github.com/william1104/rate-limiter'
有趣的是,当启用'-xx:+uselwpsynchronization'选项时,某些实现(使用stamplock)的吞吐量会受到影响。基准测试是在windows机器上执行的,我希望它对非solaris系统没有影响。然而,测试结果却有不同的表现。我能知道有谁能帮我弄清楚到底发生了什么吗?
以下是我机器上的测试结果作为参考:
带选项: -server, -XX:+UnlockDiagnosticVMOptions, -XX:+UseNUMA 基准(ratelimitertype)模式CntScoreErrorUnitsRaterLimiterBenchMark.thread\u 1stamplocklongarrayratelimiterthrpt9021487.385▒ 1082.163ops/msraterlimiterbenchmark.thread\u 1stamplockinstantarrayratelimiterthrpt9013162.330▒ 1585.555ops/msraterlimiterbenchmark.thread\u 1synchronizedlongarrayratelimitterthrpt9015362.934同步阵列卫星▒ 227.704ops/msraterlimiterbenchmark.thread\u 1同步标准阵列速率限制器hrpt9017281.675▒ 2148.057ops/msraterlimiterbenchmark.thread\u 10stamplocklongarrayratelimitterthrpt906868.653▒ 146.372ops/msraterlimiterbenchmark.thread\u 10stamplockinstantarrayratelimiterthrpt908189.747螺纹▒ 335.517ops/msraterlimiterbenchmark.thread\u 10同步长阵列卫星限制器thrpt906643.004▒ 103.568ops/msraterlimiterbenchmark.thread\u 10同步标准速率限制器hrpt905252.975▒ 190.363ops/msraterlimiterbenchmark.thread d 100stamplocklongarrayratelimitterthrpt907352.890▒ 2109.446 ops/msraterlimiterbenchmark.thread\u 100stamplockinstantarrayratelimiterthrpt908675.814线程▒ 922.653ops/msraterlimiterbenchmark.thread\u 100同步长阵列卫星限制器thrpt906509.368▒ 157.212ops/msraterlimiterbenchmark.thread\u 100同步标准速率限制器hrpt905042.867▒ 192.971ps/ms
带选项: -server, -XX:+UnlockDiagnosticVMOptions, -XX:+UseNUMA, -XX:+UseLWPSynchronization 基准(ratelimitertype)模式CntScoreErrorUnitsRaterLimiterBenchmark.thread\u 1stamplocklongarrayratelimiterthrpt9011383.198▒ 353.921ops/msraterlimiterbenchmark.thread\u 1stamplockinstantarrayratelimiterthrpt9011666.918螺纹▒ 842.426ops/msraterlimiterbenchmark.thread\u 1synchronizedlongarrayratelimitterthrpt9015696.852线程▒ 371.078ops/msraterlimiterbenchmark.thread\u 1同步标准阵列速率限制器hrpt9015357.617▒ 650.846ops/msraterlimiterbenchmark.thread\u 10stamplocklongarrayratelimitterthrpt906937.050▒ 130.727ops/msraterlimiterbenchmark.thread\u 10stamplockinstantarrayratelimiterthrpt908268.909线程▒ 291.471ops/msraterlimiterbenchmark.thread\u 10同步长阵列卫星限制器thrpt909134.319▒ 1208.998ops/msraterlimiterbenchmark.thread\u 10同步标准速率限制器hrpt905294.341▒ 225.995ops/msraterlimiterbenchmark.thread d 100stamplocklongarrayratelimitterthrpt908453.825▒ 1075.312ops/msraterlimiterbenchmark.thread\u 100stamplockinstantarrayratelimiterthrpt9016297.921螺纹▒ 611.255ops/msraterlimiterbenchmark.thread\u 100同步长阵列卫星限制器thrpt9012536.378▒ 974.951ops/msraterlimiterbenchmark.thread\u 100同步标准速率限制器hrpt909051.560▒ 1303.856操作/毫秒
有StampLockLongArrayLatelimeter和SynchronizedLongArrayLatelimeter的实现:

package one.williamwong.ratelimiter;

import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.locks.StampedLock;

public class StampLockLongArrayRateLimiter implements IRateLimiter {

    private final long duration;
    private final long[] records;
    private final StampedLock lock;
    private int pointer;

    public StampLockLongArrayRateLimiter(int maxInvokes, Duration duration) {
        this.duration = duration.toNanos();
        this.records = new long[maxInvokes];
        this.lock = new StampedLock();
        this.pointer = 0;
    }

    @Override public void acquire() {
        final long stamp = lock.writeLock();
        try {
            final long now = System.nanoTime();
            if (records[pointer] != 0) {
                final long awayFromHead = now - records[pointer];
                if (awayFromHead < duration) {
                    handleExcessLimit(records.length, Duration.ofNanos(awayFromHead));
                }
            }
            records[pointer] = now;
            pointer = (pointer + 1) % records.length;
        } finally {
            lock.unlockWrite(stamp);
        }
    }

    @Override public void reset() {
        final long stamp = lock.writeLock();
        try {
            Arrays.fill(records, 0);
            this.pointer = 0;
        } finally {
            lock.unlockWrite(stamp);
        }
    }

}
package one.williamwong.ratelimiter;

import java.time.Duration;
import java.util.Arrays;

public class SynchronizedLongArrayRateLimiter implements IRateLimiter {

    private final long duration;
    private final long[] records;
    private final Object lock;
    private int pointer;

    public SynchronizedLongArrayRateLimiter(int maxInvokes, Duration duration) {
        this.duration = duration.toNanos();
        this.records = new long[maxInvokes];
        this.lock = new Object();
        this.pointer = 0;
    }

    @Override
    public void acquire() {
        synchronized (lock) {
            final long now = System.nanoTime();
            if (records[pointer] != 0) {
                final long awayFromHead = now - records[pointer];
                if (awayFromHead < duration) {
                    handleExcessLimit(records.length, Duration.ofNanos(awayFromHead));
                }
            }
            records[pointer] = now;
            pointer = (pointer + 1) % records.length;
        }
    }

    @Override public void reset() {
        synchronized (lock) {
            Arrays.fill(records, 0);
            this.pointer = 0;
        }
    }

}
qkf9rpyu

qkf9rpyu1#

谢谢你的评论。我用不同的设置重新运行基准测试。如果我们在每次迭代中以1秒的时间再次执行jmh,我会得到以下结果:
选项:-server、-xx:+unlockdiagnosticvmoptions、-xx:+usenuma、-xx:-uselwpsynchronization
基准(ratelimitertype)模式CntScoreErrorUnitsRaterLimiterBenchMark.thread\u 1stamplocklongarrayratelimiterthrpt9023573.282▒ 364.739ops/msraterlimiterbenchmark.thread\u 1stamplockinstantarrayratelimiterthrpt9023062.260线程▒ 1035.395ops/msraterlimiterbenchmark.thread\u 1synchronizedlongarrayratelimitterthrpt9034667.411同步阵列卫星▒ 246.003ops/msraterlimiterbenchmark.thread\u 1synchronizedinstantarrayrayratelimiterthrpt9036426.369同步▒ 1248.360ops/msraterlimiterbenchmark.thread\u 10stamplocklongarrayratelimitterthrpt9013592.158▒ 76.319ops/msraterlimiterbenchmark.thread\u 10stamplockinstantarrayratelimiterthrpt9014564.306螺纹▒ 474.613ops/msraterlimiterbenchmark.thread\u 10同步长阵列卫星限制器thrpt9013524.610▒ 155.850ops/msraterlimiterbenchmark.thread\u 10同步标准速率限制器hrpt9013080.967▒ 309.736ops/msraterlimiterbenchmark.thread d 100stamplocklongarrayratelimitterthrpt9013224.529▒ 459.035ops/msraterlimiterbenchmark.thread\u 100stamplockinstantarrayratelimiterthrpt9013890.278螺纹▒ 456.182ops/msraterlimiterbenchmark.thread\u 100同步长阵列卫星限制器thrpt9012672.925▒ 314.118ops/msraterlimiterbenchmark.thread\u 100同步标准速率限制器hrpt9012245.120▒ 296.395操作/毫秒
选项:-server、-xx:+unlockdiagnosticvmoptions、-xx:+usenuma、-xx:+uselwpsynchronization
基准(ratelimitertype)模式CntScoreErrorUnitsRaterLimiterBenchMark.thread\u 1stamplocklongarrayratelimiterthrpt9024842.514▒ 372.521ops/msraterlimiterbenchmark.thread\u 1stamplockinstantarrayratelimiterthrpt9024327.864线程▒ 322.659ops/msraterlimiterbenchmark.thread\u 1synchronizedlongarrayratelimitterthrpt9034490.411▒ 330.288ops/msraterlimiterbenchmark.thread\u 1synchronizedinstantarrayrayratelimiterthrpt90383.257线程▒ 654.269ops/msraterlimiterbenchmark.thread\u 10stamplocklongarrayratelimitterthrpt9013536.284▒ 74.613ops/msraterlimiterbenchmark.thread\u 10stamplockinstantarrayratelimiterthrpt9013702.022线程▒ 289.616ops/msraterlimiterbenchmark.thread\u 10同步长阵列卫星限制器thrpt9012530.107▒ 243.471ops/msraterlimiterbenchmark.thread\u 10同步标准速率限制器hrpt9010795.833▒ 158.400ops/msraterlimiterbenchmark.thread d 100stamplocklongarrayratelimitterthrpt9013204.275▒ 200.937ops/msraterlimiterbenchmark.thread\u 100stamplockinstantarrayratelimiterthrpt9011606.823线程▒ 224.213ops/msraterlimiterbenchmark.thread\u 100同步长阵列卫星限制器thrpt9011504.124▒ 107.543ops/msraterlimiterbenchmark.thread\u 100同步标准速率限制器hrpt9010732.451▒ 118.753ops/毫秒
当启用或不启用“uselwpsynchronization”时,我没有观察到巨大的性能差异。我遇到的问题与不稳定的jmh设置有关。

相关问题