50879
|
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 |
* @library /test/lib
|
|
50 |
*
|
|
51 |
* @run main/othervm jdk.jfr.event.runtime.TestNetworkUtilizationEvent
|
|
52 |
*/
|
|
53 |
public class TestNetworkUtilizationEvent {
|
|
54 |
|
|
55 |
private static final long packetSendCount = 100;
|
|
56 |
|
|
57 |
public static void main(String[] args) throws Throwable {
|
|
58 |
testSimple();
|
|
59 |
}
|
|
60 |
|
|
61 |
static void testSimple() throws Throwable {
|
|
62 |
|
|
63 |
Instant start = Instant.now();
|
|
64 |
Recording recording = new Recording();
|
|
65 |
recording.enable(EventNames.NetworkUtilization);
|
|
66 |
recording.start();
|
|
67 |
|
|
68 |
DatagramSocket socket = new DatagramSocket();
|
|
69 |
String msg = "hello!";
|
|
70 |
byte[] buf = msg.getBytes();
|
|
71 |
|
|
72 |
// Send a few packets both to the loopback address as well to an external
|
|
73 |
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLoopbackAddress(), 12345);
|
|
74 |
for (int i = 0; i < packetSendCount; ++i) {
|
|
75 |
socket.send(packet);
|
|
76 |
}
|
|
77 |
packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("10.0.0.0"), 12345);
|
|
78 |
for (int i = 0; i < packetSendCount; ++i) {
|
|
79 |
socket.send(packet);
|
|
80 |
}
|
|
81 |
|
|
82 |
// Now there should have been traffic on at least two different interfaces
|
|
83 |
recording.stop();
|
|
84 |
Duration runtime = Duration.between(start, Instant.now());
|
|
85 |
List<RecordedEvent> events = Events.fromRecording(recording);
|
|
86 |
|
|
87 |
// Calculate the average write rate for each interface
|
|
88 |
Map<String, Double> writeRates = events.stream()
|
|
89 |
.collect(groupingBy(e -> Events.assertField(e, "networkInterface").getValue(),
|
|
90 |
averagingLong(e -> Events.assertField(e, "writeRate").getValue())));
|
|
91 |
|
|
92 |
// Our test packets should have generated at least this much traffic per second
|
|
93 |
long expectedTraffic = (buf.length * packetSendCount) / Math.max(1, runtime.toSeconds());
|
|
94 |
|
|
95 |
// Count the number of interfaces that have seen at least our test traffic
|
|
96 |
long interfacesWithTraffic = writeRates.values().stream()
|
|
97 |
.filter(d -> d >= expectedTraffic)
|
|
98 |
.count();
|
|
99 |
|
|
100 |
if (Platform.isWindows() || Platform.isSolaris()) {
|
|
101 |
// Windows and Solaris do not track statistics for the loopback interface
|
|
102 |
Asserts.assertGreaterThanOrEqual(writeRates.size(), 1);
|
|
103 |
Asserts.assertGreaterThanOrEqual(interfacesWithTraffic, Long.valueOf(1));
|
|
104 |
} else {
|
|
105 |
Asserts.assertGreaterThanOrEqual(writeRates.size(), 2);
|
|
106 |
Asserts.assertGreaterThanOrEqual(interfacesWithTraffic, Long.valueOf(2));
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|