Performance

All tests are run on 64bit Windows 10, with Java 19. As 64bit is popular now, this book assumes all code are running on 64bit JVM

JMH is used to do benchmark.

Currently, only string GET and SET commands are tested. there are two kinds of data:

  • value with 1K size (testStringGetSetSmallSize)
  • value with 10K size (testStringGetSet)

In a simple benchmark, you may find that BIO has best throughput and average time, but NIO has better sample time.

That may be because for each round, NIO server may sleep 1ms, as the operation read data is async, it’s possible that a request is executed and responsed in next round.

    public void start() {
        try {
            startServer();

            while (!stop.get()) {
                preSleep();
                if (selector.select(selectTimeout) > 0) {
                    process(selector.selectedKeys().iterator());
                }
            }
            preShutDown();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

Performance result:

BIO

Benchmark                            Mode     Cnt     Score     Error  Units
testStringGetSet                    thrpt       5  1804.792 ± 185.597  ops/s
testStringGetSetSmallSize           thrpt       5  4830.196 ±  98.344  ops/s
testStringGetSet                     avgt       5     0.002 ±   0.001   s/op
testStringGetSetSmallSize            avgt       5     0.001 ±   0.001   s/op
testStringGetSet                   sample   73744     0.003 ±   0.001   s/op
testStringGetSet·p0.00             sample             0.002             s/op
testStringGetSet·p0.50             sample             0.003             s/op
testStringGetSet·p0.90             sample             0.003             s/op
testStringGetSet·p0.95             sample             0.004             s/op
testStringGetSet·p0.99             sample             0.005             s/op
testStringGetSet·p0.999            sample             0.014             s/op
testStringGetSet·p0.9999           sample             0.031             s/op
testStringGetSet·p1.00             sample             0.073             s/op
testStringGetSetSmallSize          sample  203342     0.001 ±   0.001   s/op
testStringGetSetSmallSize·p0.00    sample             0.001             s/op
testStringGetSetSmallSize·p0.50    sample             0.001             s/op
testStringGetSetSmallSize·p0.90    sample             0.001             s/op
testStringGetSetSmallSize·p0.95    sample             0.002             s/op
testStringGetSetSmallSize·p0.99    sample             0.002             s/op
testStringGetSetSmallSize·p0.999   sample             0.003             s/op
testStringGetSetSmallSize·p0.9999  sample             0.021             s/op
testStringGetSetSmallSize·p1.00    sample             0.056             s/op
testStringGetSet                       ss             0.016             s/op
testStringGetSetSmallSize              ss             0.006             s/op

NIO

Benchmark                            Mode     Cnt     Score     Error  Units
testStringGetSet                    thrpt       5  1153.641 ±  35.590  ops/s
testStringGetSetSmallSize           thrpt       5  3509.546 ± 173.972  ops/s
testStringGetSet                     avgt       5     0.003 ±   0.001   s/op
testStringGetSetSmallSize            avgt       5     0.001 ±   0.001   s/op
testStringGetSet                   sample   49342     0.004 ±   0.001   s/op
testStringGetSet·p0.00             sample             0.002             s/op
testStringGetSet·p0.50             sample             0.004             s/op
testStringGetSet·p0.90             sample             0.005             s/op
testStringGetSet·p0.95             sample             0.005             s/op
testStringGetSet·p0.99             sample             0.006             s/op
testStringGetSet·p0.999            sample             0.010             s/op
testStringGetSet·p0.9999           sample             0.015             s/op
testStringGetSet·p1.00             sample             0.021             s/op
testStringGetSetSmallSize          sample  160461     0.001 ±   0.001   s/op
testStringGetSetSmallSize·p0.00    sample             0.001             s/op
testStringGetSetSmallSize·p0.50    sample             0.001             s/op
testStringGetSetSmallSize·p0.90    sample             0.002             s/op
testStringGetSetSmallSize·p0.95    sample             0.002             s/op
testStringGetSetSmallSize·p0.99    sample             0.002             s/op
testStringGetSetSmallSize·p0.999   sample             0.003             s/op
testStringGetSetSmallSize·p0.9999  sample             0.015             s/op
testStringGetSetSmallSize·p1.00    sample             0.045             s/op
testStringGetSet                       ss             0.016             s/op
testStringGetSetSmallSize              ss             0.007             s/op

Multi Thread NIO

Benchmark                            Mode     Cnt     Score    Error  Units
testStringGetSet                    thrpt       5  1006.490 ± 63.553  ops/s
testStringGetSetSmallSize           thrpt       5  3007.335 ± 58.951  ops/s
testStringGetSet                     avgt       5     0.004 ±  0.001   s/op
testStringGetSetSmallSize            avgt       5     0.001 ±  0.001   s/op
testStringGetSet                   sample   50118     0.004 ±  0.001   s/op
testStringGetSet·p0.00             sample             0.002            s/op
testStringGetSet·p0.50             sample             0.004            s/op
testStringGetSet·p0.90             sample             0.004            s/op
testStringGetSet·p0.95             sample             0.005            s/op
testStringGetSet·p0.99             sample             0.006            s/op
testStringGetSet·p0.999            sample             0.016            s/op
testStringGetSet·p0.9999           sample             0.018            s/op
testStringGetSet·p1.00             sample             0.020            s/op
testStringGetSetSmallSize          sample  148449     0.001 ±  0.001   s/op
testStringGetSetSmallSize·p0.00    sample             0.001            s/op
testStringGetSetSmallSize·p0.50    sample             0.001            s/op
testStringGetSetSmallSize·p0.90    sample             0.002            s/op
testStringGetSetSmallSize·p0.95    sample             0.002            s/op
testStringGetSetSmallSize·p0.99    sample             0.002            s/op
testStringGetSetSmallSize·p0.999   sample             0.004            s/op
testStringGetSetSmallSize·p0.9999  sample             0.025            s/op
testStringGetSetSmallSize·p1.00    sample             0.047            s/op
testStringGetSet                       ss             0.020            s/op
testStringGetSetSmallSize              ss             0.010            s/op

Multi Thread NIO with async write

Benchmark                            Mode     Cnt     Score     Error  Units
testStringGetSet                    thrpt       5  1017.982 ±  63.628  ops/s
testStringGetSetSmallSize           thrpt       5  3080.229 ± 396.287  ops/s
testStringGetSet                     avgt       5     0.004 ±   0.001   s/op
testStringGetSetSmallSize            avgt       5     0.001 ±   0.001   s/op
testStringGetSet                   sample   49264     0.004 ±   0.001   s/op
testStringGetSet·p0.00             sample             0.003             s/op
testStringGetSet·p0.50             sample             0.004             s/op
testStringGetSet·p0.90             sample             0.005             s/op
testStringGetSet·p0.95             sample             0.005             s/op
testStringGetSet·p0.99             sample             0.006             s/op
testStringGetSet·p0.999            sample             0.016             s/op
testStringGetSet·p0.9999           sample             0.019             s/op
testStringGetSet·p1.00             sample             0.022             s/op
testStringGetSetSmallSize          sample  159120     0.001 ±   0.001   s/op
testStringGetSetSmallSize·p0.00    sample             0.001             s/op
testStringGetSetSmallSize·p0.50    sample             0.001             s/op
testStringGetSetSmallSize·p0.90    sample             0.002             s/op
testStringGetSetSmallSize·p0.95    sample             0.002             s/op
testStringGetSetSmallSize·p0.99    sample             0.002             s/op
testStringGetSetSmallSize·p0.999   sample             0.003             s/op
testStringGetSetSmallSize·p0.9999  sample             0.015             s/op
testStringGetSetSmallSize·p1.00    sample             0.046             s/op
testStringGetSet                       ss             0.029             s/op
testStringGetSetSmallSize              ss             0.007             s/op

Virtual Thread NIO

Benchmark                            Mode     Cnt     Score    Error  Units
testStringGetSet                    thrpt       5  1003.841 ± 59.447  ops/s
testStringGetSetSmallSize           thrpt       5  2939.325 ± 92.823  ops/s
testStringGetSet                     avgt       5     0.004 ±  0.001   s/op
testStringGetSetSmallSize            avgt       5     0.001 ±  0.001   s/op
testStringGetSet                   sample   47991     0.004 ±  0.001   s/op
testStringGetSet·p0.00             sample             0.003            s/op
testStringGetSet·p0.50             sample             0.004            s/op
testStringGetSet·p0.90             sample             0.005            s/op
testStringGetSet·p0.95             sample             0.005            s/op
testStringGetSet·p0.99             sample             0.006            s/op
testStringGetSet·p0.999            sample             0.016            s/op
testStringGetSet·p0.9999           sample             0.028            s/op
testStringGetSet·p1.00             sample             0.029            s/op
testStringGetSetSmallSize          sample  133109     0.002 ±  0.001   s/op
testStringGetSetSmallSize·p0.00    sample             0.001            s/op
testStringGetSetSmallSize·p0.50    sample             0.001            s/op
testStringGetSetSmallSize·p0.90    sample             0.002            s/op
testStringGetSetSmallSize·p0.95    sample             0.002            s/op
testStringGetSetSmallSize·p0.99    sample             0.002            s/op
testStringGetSetSmallSize·p0.999   sample             0.004            s/op
testStringGetSetSmallSize·p0.9999  sample             0.016            s/op
testStringGetSetSmallSize·p1.00    sample             0.045            s/op
testStringGetSet                       ss             0.033            s/op
testStringGetSetSmallSize              ss             0.013            s/op