Search in sources :

Example 1 with StackSampleElement

use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.

the class ConverterTest method test.

@Test
public void test() {
    SampleNode testSample = testSample();
    final List<StackSampleElement> samples = new ArrayList<>();
    Converters.convert(Methods.ROOT, testSample, -1, 0, samples::add);
    SampleNode back = Converter.convert(samples.iterator());
    Assert.assertEquals(testSample, back);
}
Also used : ArrayList(java.util.ArrayList) SampleNode(org.spf4j.stackmonitor.SampleNode) StackSampleElement(org.spf4j.base.avro.StackSampleElement) Test(org.junit.Test)

Example 2 with StackSampleElement

use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.

the class Converter method saveLabeledDumps.

public static void saveLabeledDumps(final File file, final Map<String, SampleNode> pcollected) throws IOException {
    try (OutputStream bos = newOutputStream(file)) {
        final SpecificDatumWriter<StackSampleElement> writer = new SpecificDatumWriter<>(StackSampleElement.SCHEMA$);
        final BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
        encoder.writeMapStart();
        final Map<String, SampleNode> collected = pcollected.entrySet().stream().filter((e) -> e.getValue() != null).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()));
        encoder.setItemCount(collected.size());
        for (Map.Entry<String, SampleNode> entry : collected.entrySet()) {
            encoder.startItem();
            encoder.writeString(entry.getKey());
            encoder.writeArrayStart();
            Converters.convert(Methods.ROOT, entry.getValue(), -1, 0, (final StackSampleElement object) -> {
                try {
                    encoder.setItemCount(1L);
                    encoder.startItem();
                    writer.write(object, encoder);
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            });
            encoder.writeArrayEnd();
        }
        encoder.writeMapEnd();
        encoder.flush();
    }
}
Also used : TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) URLDecoder(java.net.URLDecoder) TIntObjectMap(gnu.trove.map.TIntObjectMap) PushbackInputStream(java.io.PushbackInputStream) HashMap(java.util.HashMap) MemorizingBufferedInputStream(org.spf4j.io.MemorizingBufferedInputStream) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) BufferedOutputStream(java.io.BufferedOutputStream) Decoder(org.apache.avro.io.Decoder) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) SampleNode(org.spf4j.stackmonitor.SampleNode) Nullable(javax.annotation.Nullable) EncoderFactory(org.apache.avro.io.EncoderFactory) OutputStream(java.io.OutputStream) WillNotClose(javax.annotation.WillNotClose) Iterator(java.util.Iterator) Files(java.nio.file.Files) Methods(org.spf4j.base.Methods) BinaryDecoder(org.apache.avro.io.BinaryDecoder) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) Converters(org.spf4j.base.avro.Converters) UncheckedIOException(java.io.UncheckedIOException) Consumer(java.util.function.Consumer) BinaryEncoder(org.apache.avro.io.BinaryEncoder) URLEncoder(java.net.URLEncoder) Method(org.spf4j.base.avro.Method) StackSampleElement(org.spf4j.base.avro.StackSampleElement) GZIPOutputStream(java.util.zip.GZIPOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DecoderFactory(org.apache.avro.io.DecoderFactory) InputStream(java.io.InputStream) ProfileFileFormat(org.spf4j.stackmonitor.ProfileFileFormat) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) BinaryEncoder(org.apache.avro.io.BinaryEncoder) SampleNode(org.spf4j.stackmonitor.SampleNode) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) TIntObjectMap(gnu.trove.map.TIntObjectMap) HashMap(java.util.HashMap) Map(java.util.Map) StackSampleElement(org.spf4j.base.avro.StackSampleElement)

Example 3 with StackSampleElement

use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.

the class Converter method save.

public static void save(final File file, final SampleNode collected) throws IOException {
    try (OutputStream bos = newOutputStream(file)) {
        final SpecificDatumWriter<StackSampleElement> writer = new SpecificDatumWriter<>(StackSampleElement.getClassSchema());
        final BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
        Converters.convert(Methods.ROOT, collected, -1, 0, (StackSampleElement object) -> {
            try {
                writer.write(object, encoder);
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
        });
        encoder.flush();
    }
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StackSampleElement(org.spf4j.base.avro.StackSampleElement) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 4 with StackSampleElement

use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.

the class Converter method load.

@Nullable
public static SampleNode load(@WillNotClose final InputStream fis) throws IOException {
    try (MemorizingBufferedInputStream bis = new MemorizingBufferedInputStream(fis)) {
        final PushbackInputStream pis = new PushbackInputStream(bis);
        final SpecificDatumReader<StackSampleElement> reader = new SpecificDatumReader<>(StackSampleElement.getClassSchema());
        final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(pis, null);
        return convert(new Iterator<StackSampleElement>() {

            @Override
            public boolean hasNext() {
                try {
                    int read = pis.read();
                    pis.unread(read);
                    return read >= 0;
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }

            @Override
            @SuppressFBWarnings
            public StackSampleElement next() {
                try {
                    return reader.read(null, decoder);
                } catch (IOException ex) {
                    NoSuchElementException e = new NoSuchElementException();
                    e.addSuppressed(ex);
                    throw e;
                }
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        });
    }
}
Also used : MemorizingBufferedInputStream(org.spf4j.io.MemorizingBufferedInputStream) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BinaryDecoder(org.apache.avro.io.BinaryDecoder) PushbackInputStream(java.io.PushbackInputStream) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) StackSampleElement(org.spf4j.base.avro.StackSampleElement) NoSuchElementException(java.util.NoSuchElementException) Nullable(javax.annotation.Nullable)

Example 5 with StackSampleElement

use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.

the class Converter method loadLabels.

/**
 * Load the labels from a ssdump3 file.
 * @param file the ssdump3 file.
 * @throws IOException
 */
public static void loadLabels(final File file, final Consumer<String> labalsConsumer) throws IOException {
    try (InputStream bis = newInputStream(file)) {
        final SpecificDatumReader<StackSampleElement> reader = new SpecificDatumReader<>(StackSampleElement.SCHEMA$);
        final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(bis, null);
        long nrItems = decoder.readMapStart();
        StackSampleElement asmp = new StackSampleElement();
        while (nrItems > 0) {
            for (int i = 0; i < nrItems; i++) {
                String key = decoder.readString();
                labalsConsumer.accept(key);
                skipDump(decoder, reader, asmp);
            }
            nrItems = decoder.mapNext();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) PushbackInputStream(java.io.PushbackInputStream) MemorizingBufferedInputStream(org.spf4j.io.MemorizingBufferedInputStream) InputStream(java.io.InputStream) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) StackSampleElement(org.spf4j.base.avro.StackSampleElement) BinaryDecoder(org.apache.avro.io.BinaryDecoder)

Aggregations

StackSampleElement (org.spf4j.base.avro.StackSampleElement)11 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)7 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)6 BufferedInputStream (java.io.BufferedInputStream)5 InputStream (java.io.InputStream)5 PushbackInputStream (java.io.PushbackInputStream)5 BinaryDecoder (org.apache.avro.io.BinaryDecoder)5 MemorizingBufferedInputStream (org.spf4j.io.MemorizingBufferedInputStream)5 SampleNode (org.spf4j.stackmonitor.SampleNode)5 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)4 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 Nullable (javax.annotation.Nullable)4 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 HashMap (java.util.HashMap)2 NoSuchElementException (java.util.NoSuchElementException)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 BinaryEncoder (org.apache.avro.io.BinaryEncoder)2