TestNetworkUtilizationEvent.java 3.9 KB
Newer Older
A
apetushkov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.jfr.event.runtime;

28 29 30 31 32 33 34
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

A
apetushkov 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.test.lib.Asserts;
import jdk.test.lib.Platform;
import jdk.test.lib.jfr.EventNames;
import jdk.test.lib.jfr.Events;

/**
 * @test
 * @key jfr
 *
 * @library /lib /
 *
 * @run main/othervm jdk.jfr.event.runtime.TestNetworkUtilizationEvent
 */
public class TestNetworkUtilizationEvent {

    private static final long packetSendCount = 100;

    public static void main(String[] args) throws Throwable {

        Recording recording = new Recording();
57
        recording.enable(EventNames.NetworkUtilization).with("period", "endChunk");
A
apetushkov 已提交
58 59 60 61 62
        recording.start();

        DatagramSocket socket = new DatagramSocket();
        String msg = "hello!";
        byte[] buf = msg.getBytes();
63 64 65
        forceEndChunk();
        // Send a few packets both to the loopback address as well to an
        // external
A
apetushkov 已提交
66 67 68 69 70 71 72 73 74
        DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLoopbackAddress(), 12345);
        for (int i = 0; i < packetSendCount; ++i) {
            socket.send(packet);
        }
        packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("10.0.0.0"), 12345);
        for (int i = 0; i < packetSendCount; ++i) {
            socket.send(packet);
        }

75 76 77 78
        forceEndChunk();
        socket.close();
        // Now there should have been traffic on at least two different
        // interfaces
A
apetushkov 已提交
79
        recording.stop();
80
        Set<String> networkInterfaces = new HashSet<>();
A
apetushkov 已提交
81
        List<RecordedEvent> events = Events.fromRecording(recording);
82 83 84 85 86 87 88 89 90 91
        Events.hasEvents(events);
        for (RecordedEvent event : events) {
            System.out.println(event);
            Events.assertField(event, "writeRate").atLeast(0L).atMost(1000L * Integer.MAX_VALUE);
            Events.assertField(event, "readRate").atLeast(0L).atMost(1000L * Integer.MAX_VALUE);
            Events.assertField(event, "networkInterface").notNull();
            if (event.getLong("writeRate") > 0) {
                networkInterfaces.add(event.getString("networkInterface"));
            }
        }
A
apetushkov 已提交
92 93

        if (Platform.isWindows() || Platform.isSolaris()) {
94 95 96
            // Windows and Solaris do not track statistics for the loopback
            // interface
            Asserts.assertGreaterThanOrEqual(networkInterfaces.size(), 1);
A
apetushkov 已提交
97
        } else {
98 99 100 101 102 103 104 105
            Asserts.assertGreaterThanOrEqual(networkInterfaces.size(), 2);
        }
    }

    private static void forceEndChunk() {
        try(Recording r = new Recording()) {
            r.start();
            r.stop();
A
apetushkov 已提交
106 107 108
        }
    }
}