Update NetUtilBenchmark (#7826)

Motivation:
NetUtilBenchmark is using out of date data, throws an exception in the benchmark, and allocates a Set on each run.

Modifications:
- Update the benchmark and reduce each run's overhead

Result:
NetUtilBenchmark is updated.
This commit is contained in:
Scott Mitchell 2018-03-30 23:27:08 -07:00 committed by Norman Maurer
parent 8d78893a76
commit 9d51a40df0

View File

@ -17,10 +17,6 @@ package io.netty.microbenchmark.common;
import io.netty.microbench.util.AbstractMicrobenchmark; import io.netty.microbench.util.AbstractMicrobenchmark;
import io.netty.util.NetUtil; import io.netty.util.NetUtil;
import java.util.HashMap;
import java.util.Map;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Measurement; import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Threads; import org.openjdk.jmh.annotations.Threads;
@ -32,195 +28,314 @@ import org.openjdk.jmh.annotations.Warmup;
public class NetUtilBenchmark extends AbstractMicrobenchmark { public class NetUtilBenchmark extends AbstractMicrobenchmark {
@Benchmark @Benchmark
public void useGetByNameIpv4() { public int useGetByNameIpv4() {
for (String testEntry : invalidIpV4Hosts.keySet()) { int invalidCount = 0;
if (NetUtil.getByName(testEntry, true) != null) { for (String testEntry : invalidIpV4Hosts) {
throw new RuntimeException("error"); if (NetUtil.getByName(testEntry) == null) {
++invalidCount;
} }
} }
return invalidCount;
} }
@Benchmark @Benchmark
public void useGetByNameIpv6() { public int useGetByNameIpv6() {
for (String testEntry : invalidIpV6Hosts.keySet()) { int invalidCount = 0;
if (NetUtil.getByName(testEntry, true) != null) { for (String testEntry : invalidIpV6Hosts) {
throw new RuntimeException("error"); if (NetUtil.getByName(testEntry) == null) {
++invalidCount;
} }
} }
return invalidCount;
} }
@Benchmark @Benchmark
public void useIsValidIpv6() { public int useIsValidIpv6() {
for (String host : invalidIpV6Hosts.keySet()) { int invalidCount = 0;
if (NetUtil.isValidIpV6Address(host)) { for (String host : invalidIpV6Hosts) {
throw new RuntimeException("error"); if (!NetUtil.isValidIpV6Address(host)) {
++invalidCount;
} }
} }
return invalidCount;
} }
@Benchmark @Benchmark
public void useIsValidIpv4() { public int useIsValidIpv4() {
for (String host : invalidIpV4Hosts.keySet()) { int invalidCount = 0;
if (NetUtil.isValidIpV4Address(host)) { for (String host : invalidIpV4Hosts) {
throw new RuntimeException("error"); if (!NetUtil.isValidIpV4Address(host)) {
++invalidCount;
} }
} }
return invalidCount;
} }
private static final Map<String, byte[]> invalidIpV4Hosts = new HashMap<String, byte[]>() { private static final String[] invalidIpV4Hosts = {
private static final long serialVersionUID = 1299215199895717282L; "1.256.3.4",
{ "256.0.0.1",
put("1.256.3.4", null); "1.1.1.1.1",
put("256.0.0.1", null); "x.255.255.255",
put("1.1.1.1.1", null); "0.1:0.0",
put("x.255.255.255", null); "0.1.0.0:",
put("0.1:0.0", null); "127.0.0.",
put("0.1.0.0:", null); "1.2..4",
put("127.0.0.", null); "192.0.1",
put("1.2..4", null); "192.0.1.1.1",
put("192.0.1", null); "192.0.1.a",
put("192.0.1.1.1", null); "19a.0.1.1",
put("192.0.1.a", null); "a.0.1.1",
put("19a.0.1.1", null); ".0.1.1",
put("a.0.1.1", null); "127.0.0",
put(".0.1.1", null); "192.0.1.256",
put("...", null); "0.0.200.259",
} "1.1.-1.1",
"1.1. 1.1",
"1.1.1.1 ",
"1.1.+1.1",
"0.0x1.0.255",
"0.01x.0.255",
"0.x01.0.255",
"0.-.0.0",
"0..0.0",
"0.A.0.0",
"0.1111.0.0",
"..."
}; };
private static final Map<String, byte[]> invalidIpV6Hosts = new HashMap<String, byte[]>() { private static final String[] invalidIpV6Hosts = {
private static final long serialVersionUID = -5870810805409009696L; // Test method with garbage.
{ "Obvious Garbage",
// Test method with garbage. // Test method with preferred style, too many :
put("Obvious Garbage", null); "0:1:2:3:4:5:6:7:8",
// Test method with preferred style, too many : // Test method with preferred style, not enough :
put("0:1:2:3:4:5:6:7:8", null); "0:1:2:3:4:5:6",
// Test method with preferred style, not enough : // Test method with preferred style, bad digits.
put("0:1:2:3:4:5:6", null); "0:1:2:3:4:5:6:x",
// Test method with preferred style, bad digits. // Test method with preferred style, adjacent :
put("0:1:2:3:4:5:6:x", null); "0:1:2:3:4:5:6::7",
// Test method with preferred style, adjacent : // Too many : separators trailing
put("0:1:2:3:4:5:6::7", null); "0:1:2:3:4:5:6:7::",
// Too many : separators trailing // Too many : separators leading
put("0:1:2:3:4:5:6:7::", null); "::0:1:2:3:4:5:6:7",
// Too many : separators leading // Too many : separators trailing
put("::0:1:2:3:4:5:6:7", null); "1:2:3:4:5:6:7:",
// Too many : separators trailing // Too many : separators leading
put("1:2:3:4:5:6:7:", null); ":1:2:3:4:5:6:7",
// Too many : separators leading // Compression with : separators trailing
put(":1:2:3:4:5:6:7", null); "0:1:2:3:4:5::7:",
// Too many : separators leading 0 "0:1:2:3:4::7:",
put("0::1:2:3:4:5:6:7", null); "0:1:2:3::7:",
// Test method with preferred style, too many digits. "0:1:2::7:",
put("0:1:2:3:4:5:6:789abcdef", null); "0:1::7:",
// Test method with compressed style, bad digits. "0::7:",
put("0:1:2:3::x", null); // Compression at start with : separators trailing
// Test method with compressed style, too many adjacent : "::0:1:2:3:4:5:7:",
put("0:1:2:::3", null); "::0:1:2:3:4:7:",
// Test method with compressed style, too many digits. "::0:1:2:3:7:",
put("0:1:2:3::abcde", null); "::0:1:2:7:",
// Test method with preferred style, too many : "::0:1:7:",
put("0:1:2:3:4:5:6:7:8", null); "::7:",
// Test method with compressed style, not enough : // The : separators leading and trailing
put("0:1", null); ":1:2:3:4:5:6:7:",
// Test method with ipv4 style, bad ipv6 digits. ":1:2:3:4:5:6:",
put("0:0:0:0:0:x:10.0.0.1", null); ":1:2:3:4:5:",
// Test method with ipv4 style, bad ipv4 digits. ":1:2:3:4:",
put("0:0:0:0:0:0:10.0.0.x", null); ":1:2:3:",
// Test method with ipv4 style, adjacent : ":1:2:",
put("0:0:0:0:0::0:10.0.0.1", null); ":1:",
// Test method with ipv4 style, too many ipv6 digits. // Compression with : separators leading
put("0:0:0:0:0:00000:10.0.0.1", null); ":1::2:3:4:5:6:7",
// Test method with ipv4 style, too many : ":1::3:4:5:6:7",
put("0:0:0:0:0:0:0:10.0.0.1", null); ":1::4:5:6:7",
// Test method with ipv4 style, not enough : ":1::5:6:7",
put("0:0:0:0:0:10.0.0.1", null); ":1::6:7",
// Test method with ipv4 style, too many . ":1::7",
put("0:0:0:0:0:0:10.0.0.0.1", null); ":1:2:3:4:5:6::7",
// Test method with ipv4 style, not enough . ":1:3:4:5:6::7",
put("0:0:0:0:0:0:10.0.1", null); ":1:4:5:6::7",
// Test method with ipv4 style, adjacent . ":1:5:6::7",
put("0:0:0:0:0:0:10..0.0.1", null); ":1:6::7",
// Test method with ipv4 style, leading . ":1::",
put("0:0:0:0:0:0:.0.0.1", null); // Compression trailing with : separators leading
// Test method with ipv4 style, leading . ":1:2:3:4:5:6:7::",
put("0:0:0:0:0:0:.10.0.0.1", null); ":1:3:4:5:6:7::",
// Test method with ipv4 style, trailing . ":1:4:5:6:7::",
put("0:0:0:0:0:0:10.0.0.", null); ":1:5:6:7::",
// Test method with ipv4 style, trailing . ":1:6:7::",
put("0:0:0:0:0:0:10.0.0.1.", null); ":1:7::",
// Test method with compressed ipv4 style, bad ipv6 digits. // Double compression
put("::fffx:192.168.0.1", null); "1::2:3:4:5:6::",
// Test method with compressed ipv4 style, bad ipv4 digits. "::1:2:3:4:5::6",
put("::ffff:192.168.0.x", null); "::1:2:3:4:5:6::",
// Test method with compressed ipv4 style, too many adjacent : "::1:2:3:4:5::",
put(":::ffff:192.168.0.1", null); "::1:2:3:4::",
// Test method with compressed ipv4 style, too many ipv6 digits. "::1:2:3::",
put("::fffff:192.168.0.1", null); "::1:2::",
// Test method with compressed ipv4 style, too many ipv4 digits. "::0::",
put("::ffff:1923.168.0.1", null); "12::0::12",
// Test method with compressed ipv4 style, not enough : // Too many : separators leading 0
put(":ffff:192.168.0.1", null); "0::1:2:3:4:5:6:7",
// Test method with compressed ipv4 style, too many . // Test method with preferred style, too many digits.
put("::ffff:192.168.0.1.2", null); "0:1:2:3:4:5:6:789abcdef",
// Test method with compressed ipv4 style, not enough . // Test method with compressed style, bad digits.
put("::ffff:192.168.0", null); "0:1:2:3::x",
// Test method with compressed ipv4 style, adjacent . // Test method with compressed style, too many adjacent :
put("::ffff:192.168..0.1", null); "0:1:2:::3",
// Test method, garbage. // Test method with compressed style, too many digits.
put("absolute, and utter garbage", null); "0:1:2:3::abcde",
// Test method, bad ipv6 digits. // Test method with compressed style, not enough :
put("x:0:0:0:0:0:10.0.0.1", null); "0:1",
// Test method, bad ipv4 digits. // Test method with ipv4 style, bad ipv6 digits.
put("0:0:0:0:0:0:x.0.0.1", null); "0:0:0:0:0:x:10.0.0.1",
// Test method, too many ipv6 digits. // Test method with ipv4 style, bad ipv4 digits.
put("00000:0:0:0:0:0:10.0.0.1", null); "0:0:0:0:0:0:10.0.0.x",
// Test method, too many ipv4 digits. // Test method with ipv4 style, too many ipv6 digits.
put("0:0:0:0:0:0:10.0.0.1000", null); "0:0:0:0:0:00000:10.0.0.1",
// Test method, too many : // Test method with ipv4 style, too many :
put("0:0:0:0:0:0:0:10.0.0.1", null); "0:0:0:0:0:0:0:10.0.0.1",
// Test method, not enough : // Test method with ipv4 style, not enough :
put("0:0:0:0:0:10.0.0.1", null); "0:0:0:0:0:10.0.0.1",
// Test method, out of order trailing : // Test method with ipv4 style, too many .
put("0:0:0:0:0:10.0.0.1:", null); "0:0:0:0:0:0:10.0.0.0.1",
// Test method, out of order leading : // Test method with ipv4 style, not enough .
put(":0:0:0:0:0:10.0.0.1", null); "0:0:0:0:0:0:10.0.1",
// Test method, out of order leading : // Test method with ipv4 style, adjacent .
put("0:0:0:0::10.0.0.1:", null); "0:0:0:0:0:0:10..0.0.1",
// Test method, out of order trailing : // Test method with ipv4 style, leading .
put(":0:0:0:0::10.0.0.1", null); "0:0:0:0:0:0:.0.0.1",
// Test method, too many . // Test method with ipv4 style, leading .
put("0:0:0:0:0:0:10.0.0.0.1", null); "0:0:0:0:0:0:.10.0.0.1",
// Test method, not enough . // Test method with ipv4 style, trailing .
put("0:0:0:0:0:0:10.0.1", null); "0:0:0:0:0:0:10.0.0.",
// Test method, adjacent . // Test method with ipv4 style, trailing .
put("0:0:0:0:0:0:10.0.0..1", null); "0:0:0:0:0:0:10.0.0.1.",
// Double compression symbol // Test method with compressed ipv4 style, bad ipv6 digits.
put("::0::", null); "::fffx:192.168.0.1",
// Empty contents // Test method with compressed ipv4 style, bad ipv4 digits.
put("", null); "::ffff:192.168.0.x",
// Trailing : (max number of : = 8) // Test method with compressed ipv4 style, too many adjacent :
put("2001:0:4136:e378:8000:63bf:3fff:fdd2:", null); ":::ffff:192.168.0.1",
// Leading : (max number of : = 8) // Test method with compressed ipv4 style, too many ipv6 digits.
put(":aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222", null); "::fffff:192.168.0.1",
// Invalid character // Test method with compressed ipv4 style, too many ipv4 digits.
put("1234:2345:3456:4567:5678:6789::X890", null); "::ffff:1923.168.0.1",
// Trailing . in IPv4 // Test method with compressed ipv4 style, not enough :
put("::ffff:255.255.255.255.", null); ":ffff:192.168.0.1",
// To many characters in IPv4 // Test method with compressed ipv4 style, too many .
put("::ffff:0.0.1111.0", null); "::ffff:192.168.0.1.2",
// Test method, adjacent . // Test method with compressed ipv4 style, not enough .
put("::ffff:0.0..0", null); "::ffff:192.168.0",
// Not enough IPv4 entries trailing . // Test method with compressed ipv4 style, adjacent .
put("::ffff:127.0.0.", null); "::ffff:192.168..0.1",
// Not enough IPv4 entries no trailing . // Test method, bad ipv6 digits.
put("::ffff:1.2.4", null); "x:0:0:0:0:0:10.0.0.1",
// Extra IPv4 entry // Test method, bad ipv4 digits.
put("::ffff:192.168.0.1.255", null); "0:0:0:0:0:0:x.0.0.1",
// Not enough IPv6 content // Test method, too many ipv6 digits.
put(":ffff:192.168.0.1.255", null); "00000:0:0:0:0:0:10.0.0.1",
// Intermixed IPv4 and IPv6 symbols // Test method, too many ipv4 digits.
put("::ffff:255.255:255.255.", null); "0:0:0:0:0:0:10.0.0.1000",
} // Test method, too many :
"0:0:0:0:0:0:0:10.0.0.1",
// Test method, not enough :
"0:0:0:0:0:10.0.0.1",
// Test method, out of order trailing :
"0:0:0:0:0:10.0.0.1:",
// Test method, out of order leading :
":0:0:0:0:0:10.0.0.1",
// Test method, out of order leading :
"0:0:0:0::10.0.0.1:",
// Test method, out of order trailing :
":0:0:0:0::10.0.0.1",
// Test method, too many .
"0:0:0:0:0:0:10.0.0.0.1",
// Test method, not enough .
"0:0:0:0:0:0:10.0.1",
// Test method, adjacent .
"0:0:0:0:0:0:10.0.0..1",
// Empty contents
"",
// Invalid single compression
":",
":::",
// Trailing : (max number of : = 8)
"2001:0:4136:e378:8000:63bf:3fff:fdd2:",
// Leading : (max number of : = 8)
":aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222",
// Invalid character
"1234:2345:3456:4567:5678:6789::X890",
// Trailing . in IPv4
"::ffff:255.255.255.255.",
// To many characters in IPv4
"::ffff:0.0.1111.0",
// Test method, adjacent .
"::ffff:0.0..0",
// Not enough IPv4 entries trailing .
"::ffff:127.0.0.",
// Invalid trailing IPv4 character
"::ffff:127.0.0.a",
// Invalid leading IPv4 character
"::ffff:a.0.0.1",
// Invalid middle IPv4 character
"::ffff:127.a.0.1",
// Invalid middle IPv4 character
"::ffff:127.0.a.1",
// Not enough IPv4 entries no trailing .
"::ffff:1.2.4",
// Extra IPv4 entry
"::ffff:192.168.0.1.255",
// Not enough IPv6 content
":ffff:192.168.0.1.255",
// Intermixed IPv4 and IPv6 symbols
"::ffff:255.255:255.255.",
// Invalid IPv4 mapped address - invalid ipv4 separator
"0:0:0::0:0:00f.0.0.1",
// Invalid IPv4 mapped address - not enough f's
"0:0:0:0:0:fff:1.0.0.1",
// Invalid IPv4 mapped address - not IPv4 mapped, not IPv4 compatible
"0:0:0:0:0:ff00:1.0.0.1",
// Invalid IPv4 mapped address - not IPv4 mapped, not IPv4 compatible
"0:0:0:0:0:ff:1.0.0.1",
// Invalid IPv4 mapped address - too many f's
"0:0:0:0:0:fffff:1.0.0.1",
// Invalid IPv4 mapped address - too many bytes (too many 0's)
"0:0:0:0:0:0:ffff:1.0.0.1",
// Invalid IPv4 mapped address - too many bytes (too many 0's)
"::0:0:0:0:0:ffff:1.0.0.1",
// Invalid IPv4 mapped address - too many bytes (too many 0's)
"0:0:0:0:0:0::1.0.0.1",
// Invalid IPv4 mapped address - too many bytes (too many 0's)
"0:0:0:0:0:00000:1.0.0.1",
// Invalid IPv4 mapped address - too few bytes (not enough 0's)
"0:0:0:0:ffff:1.0.0.1",
// Invalid IPv4 mapped address - too few bytes (not enough 0's)
"ffff:192.168.0.1",
// Invalid IPv4 mapped address - 0's after the mapped ffff indicator
"0:0:0:0:0:ffff::10.0.0.1",
// Invalid IPv4 mapped address - 0's after the mapped ffff indicator
"0:0:0:0:ffff::10.0.0.1",
// Invalid IPv4 mapped address - 0's after the mapped ffff indicator
"0:0:0:ffff::10.0.0.1",
// Invalid IPv4 mapped address - 0's after the mapped ffff indicator
"0:0:ffff::10.0.0.1",
// Invalid IPv4 mapped address - 0's after the mapped ffff indicator
"0:ffff::10.0.0.1",
// Invalid IPv4 mapped address - 0's after the mapped ffff indicator
"ffff::10.0.0.1",
// Invalid IPv4 mapped address - not all 0's before the mapped separator
"1:0:0:0:0:ffff:10.0.0.1",
// Address that is similar to IPv4 mapped, but is invalid
"0:0:0:0:ffff:ffff:1.0.0.1",
// Valid number of separators, but invalid IPv4 format
"::1:2:3:4:5:6.7.8.9",
// Too many digits
"0:0:0:0:0:0:ffff:10.0.0.1",
// Invalid IPv4 format
":1.2.3.4",
// Invalid IPv4 format
"::.2.3.4",
// Invalid IPv4 format
"::ffff:0.1.2."
}; };
} }