JVMMetricsServiceHandlerTestCase.java 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

P
pengys5 已提交
19 20 21 22
package org.skywalking.apm.collector.agentjvm.grpc.handler;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
23 24
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
P
pengys5 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import org.skywalking.apm.network.proto.CPU;
import org.skywalking.apm.network.proto.GC;
import org.skywalking.apm.network.proto.GCPhrase;
import org.skywalking.apm.network.proto.JVMMetric;
import org.skywalking.apm.network.proto.JVMMetrics;
import org.skywalking.apm.network.proto.JVMMetricsServiceGrpc;
import org.skywalking.apm.network.proto.Memory;
import org.skywalking.apm.network.proto.MemoryPool;
import org.skywalking.apm.network.proto.PoolType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author pengys5
 */
public class JVMMetricsServiceHandlerTestCase {

    private final Logger logger = LoggerFactory.getLogger(JVMMetricsServiceHandlerTestCase.class);

P
peng-yongsheng 已提交
44
    private static JVMMetricsServiceGrpc.JVMMetricsServiceBlockingStub STUB;
P
pengys5 已提交
45

46
    public static void main(String[] args) {
P
pengys5 已提交
47
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 11800).usePlaintext(true).build();
P
peng-yongsheng 已提交
48
        STUB = JVMMetricsServiceGrpc.newBlockingStub(channel);
P
pengys5 已提交
49

50 51 52 53 54
        final long timeInterval = 1;
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> multiInstanceJvmSend(), 1, timeInterval, TimeUnit.SECONDS);
    }

    public static void multiInstanceJvmSend() {
55 56 57 58
        buildJvmMetric(2);
        buildJvmMetric(3);
    }

59
    private static void buildJvmMetric(int instanceId) {
P
pengys5 已提交
60
        JVMMetrics.Builder jvmMetricsBuilder = JVMMetrics.newBuilder();
61
        jvmMetricsBuilder.setApplicationInstanceId(instanceId);
P
pengys5 已提交
62 63 64 65 66 67 68 69 70

        JVMMetric.Builder jvmMetric = JVMMetric.newBuilder();
        jvmMetric.setTime(System.currentTimeMillis());
        buildCpuMetric(jvmMetric);
        buildMemoryMetric(jvmMetric);
        buildMemoryPoolMetric(jvmMetric);
        buildGcMetric(jvmMetric);

        jvmMetricsBuilder.addMetrics(jvmMetric.build());
P
peng-yongsheng 已提交
71
        STUB.collect(jvmMetricsBuilder.build());
P
pengys5 已提交
72 73
    }

74
    private static void buildCpuMetric(JVMMetric.Builder jvmMetric) {
P
pengys5 已提交
75 76 77 78 79
        CPU.Builder cpuBuilder = CPU.newBuilder();
        cpuBuilder.setUsagePercent(70);
        jvmMetric.setCpu(cpuBuilder);
    }

80
    private static void buildMemoryMetric(JVMMetric.Builder jvmMetric) {
P
peng-yongsheng 已提交
81 82 83 84 85 86 87
        Memory.Builder builderHeap = Memory.newBuilder();
        builderHeap.setIsHeap(true);
        builderHeap.setInit(20);
        builderHeap.setMax(100);
        builderHeap.setUsed(50);
        builderHeap.setCommitted(30);
        jvmMetric.addMemory(builderHeap.build());
P
pengys5 已提交
88

P
peng-yongsheng 已提交
89 90 91 92 93 94 95
        Memory.Builder builderNonHeap = Memory.newBuilder();
        builderNonHeap.setIsHeap(false);
        builderNonHeap.setInit(200);
        builderNonHeap.setMax(1000);
        builderNonHeap.setUsed(500);
        builderNonHeap.setCommitted(300);
        jvmMetric.addMemory(builderNonHeap.build());
P
pengys5 已提交
96 97
    }

98
    private static void buildMemoryPoolMetric(JVMMetric.Builder jvmMetric) {
P
pengys5 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.NEWGEN_USAGE, true).build());
        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.NEWGEN_USAGE, false).build());

        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.OLDGEN_USAGE, true).build());
        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.OLDGEN_USAGE, false).build());

        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.METASPACE_USAGE, true).build());
        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.METASPACE_USAGE, false).build());

        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.PERMGEN_USAGE, true).build());
        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.PERMGEN_USAGE, false).build());

        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.SURVIVOR_USAGE, true).build());
        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.SURVIVOR_USAGE, false).build());

        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.CODE_CACHE_USAGE, true).build());
        jvmMetric.addMemoryPool(buildMemoryPoolMetric(PoolType.CODE_CACHE_USAGE, false).build());
    }

    private static MemoryPool.Builder buildMemoryPoolMetric(PoolType poolType, boolean isHeap) {
        MemoryPool.Builder builder = MemoryPool.newBuilder();
        builder.setType(poolType);
        builder.setInit(20);
        builder.setMax(100);
        builder.setUsed(50);
        builder.setCommited(30);
        return builder;
P
pengys5 已提交
126 127
    }

128
    private static void buildGcMetric(JVMMetric.Builder jvmMetric) {
129 130 131 132 133 134 135 136 137 138 139
        GC.Builder newGcBuilder = GC.newBuilder();
        newGcBuilder.setPhrase(GCPhrase.NEW);
        newGcBuilder.setCount(2);
        newGcBuilder.setTime(100);
        jvmMetric.addGc(newGcBuilder.build());

        GC.Builder oldGcBuilder = GC.newBuilder();
        oldGcBuilder.setPhrase(GCPhrase.OLD);
        oldGcBuilder.setCount(2);
        oldGcBuilder.setTime(100);
        jvmMetric.addGc(oldGcBuilder.build());
P
pengys5 已提交
140 141
    }
}