Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Java/Openjdk/test/jdk/jdk/jfr/jmx/streaming/   (Sun/Oracle ©)  Datei vom 13.11.2022 mit Größe 4 kB image not shown  

Quelle  TestMaxSize.java   Sprache: JAVA

 
/*
 * Copyright (c) 2020, 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.
 *
 * 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.jmx.streaming;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.management.MBeanServerConnection;

import jdk.jfr.Event;
import jdk.management.jfr.RemoteRecordingStream;

/**
 * @test
 * @key jfr
 * @summary Tests that max size can be set for a RemoteRecordingStream
 * @requires vm.hasJFR
 * @library /test/lib /test/jdk
 * @run main/othervm jdk.jfr.jmx.streaming.TestMaxSize
 */

public class TestMaxSize {

    static class Monkey extends Event {
    }

    public static void main(String... args) throws Exception {
        MBeanServerConnection conn = ManagementFactory.getPlatformMBeanServer();
        Path dir = Files.createDirectories(Paths.get("max-size-" + System.currentTimeMillis()));
        System.out.println(dir);
        AtomicBoolean finished = new AtomicBoolean();
        try (RemoteRecordingStream e = new RemoteRecordingStream(conn, dir)) {
            e.startAsync();
            e.onEvent(ev -> {
                if (finished.get()) {
                    return;
                }
                // Consume some events, but give back control
                // to stream so it can be closed.
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e1) {
                    // ignore
                }
            });
            while (directorySize(dir) < 50_000_000) {
                emitEvents(500_000);
            }
            System.out.println("Before setMaxSize(1_000_000)");
            fileCount(dir);
            e.setMaxSize(1_000_000);
            System.out.println("After setMaxSize(1_000_000)");
            long count = fileCount(dir);
            if (count > 3) {
                // Three files can happen when:
                // File 1: Header of new chunk is written to disk
                // File 2: Previous chunk is not yet finalized and added to list of DiskChunks
                // File 3: Previous previous file is in the list of DiskChunks.
                throw new Exception("Expected at most three chunks with setMaxSize(1_000_000). Found " + count);
            }
            finished.set(true);
        }
    }

    private static void emitEvents(int count) throws InterruptedException {
        for (int i = 0; i < count; i++) {
            Monkey m = new Monkey();
            m.commit();
        }
        System.out.println("Emitted " + count + " events");
        Thread.sleep(1000);
    }

    private static int fileCount(Path dir) throws IOException {
        System.out.println("Files:");
        AtomicInteger count = new AtomicInteger();
        Files.list(dir).forEach(p -> {
            System.out.println(p + " " + fileSize(p));
            count.incrementAndGet();
        });
        return count.get();
    }

    private static long directorySize(Path dir) throws IOException {
        long p = Files.list(dir).mapToLong(f -> fileSize(f)).sum();
        System.out.println("Directory size: " + p);
        return p;
    }

    private static long fileSize(Path p) {
        try {
            return Files.size(p);
        } catch (IOException e) {
            System.out.println("Could not determine file size for " + p);
            return 0;
        }
    }
}

Messung V0.5
C=96 H=89 G=92

¤ Dauer der Verarbeitung: 0.0 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.