1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.event.runtime; 27 28 import jdk.jfr.Recording; 29 import jdk.jfr.consumer.RecordedEvent; 30 import jdk.test.lib.Asserts; 31 import jdk.test.lib.Platform; 32 import jdk.test.lib.jfr.EventNames; 33 import jdk.test.lib.jfr.Events; 34 35 import java.net.DatagramPacket; 36 import java.net.DatagramSocket; 37 import java.net.InetAddress; 38 import java.time.Duration; 39 import java.time.Instant; 40 import java.util.List; 41 import java.util.Map; 42 43 import static java.util.stream.Collectors.averagingLong; 44 import static java.util.stream.Collectors.groupingBy; 45 46 /** 47 * @test 48 * @key jfr 49 * @requires vm.hasJFR 50 * @library /test/lib 51 * 52 * @run main/othervm jdk.jfr.event.runtime.TestNetworkUtilizationEvent 53 */ 54 public class TestNetworkUtilizationEvent { 55 56 private static final long packetSendCount = 100; 57 58 public static void main(String[] args) throws Throwable { 59 testSimple(); 60 } 61 62 static void testSimple() throws Throwable { 63 64 Instant start = Instant.now(); 65 Recording recording = new Recording(); 66 recording.enable(EventNames.NetworkUtilization); 67 recording.start(); 68 69 DatagramSocket socket = new DatagramSocket(); 70 String msg = "hello!"; 71 byte[] buf = msg.getBytes(); 72 73 // Send a few packets both to the loopback address as well to an external 74 DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLoopbackAddress(), 12345); 75 for (int i = 0; i < packetSendCount; ++i) { 76 socket.send(packet); 77 } 78 packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("10.0.0.0"), 12345); 79 for (int i = 0; i < packetSendCount; ++i) { 80 socket.send(packet); 81 } 82 83 // Now there should have been traffic on at least two different interfaces 84 recording.stop(); 85 Duration runtime = Duration.between(start, Instant.now()); 86 List<RecordedEvent> events = Events.fromRecording(recording); 87 88 // Calculate the average write rate for each interface 89 Map<String, Double> writeRates = events.stream() 90 .collect(groupingBy(e -> Events.assertField(e, "networkInterface").getValue(), 91 averagingLong(e -> Events.assertField(e, "writeRate").getValue()))); 92 93 // Our test packets should have generated at least this much traffic per second 94 long expectedTraffic = (buf.length * packetSendCount) / Math.max(1, runtime.toSeconds()); 95 96 // Count the number of interfaces that have seen at least our test traffic 97 long interfacesWithTraffic = writeRates.values().stream() 98 .filter(d -> d >= expectedTraffic) 99 .count(); 100 101 if (Platform.isWindows() || Platform.isSolaris()) { 102 // Windows and Solaris do not track statistics for the loopback interface 103 Asserts.assertGreaterThanOrEqual(writeRates.size(), 1); 104 Asserts.assertGreaterThanOrEqual(interfacesWithTraffic, Long.valueOf(1)); 105 } else { 106 Asserts.assertGreaterThanOrEqual(writeRates.size(), 2); 107 Asserts.assertGreaterThanOrEqual(interfacesWithTraffic, Long.valueOf(2)); 108 } 109 } 110 }