io.micrometer.core 学习

package com.goods.stock.util;

import com.goods.stock.GoodsStockApplication;
import com.goods.stock.client.query.adapter.LockStockQueryDto;
import com.goods.stock.testtool.RestApiTool;
import com.google.common.collect.Lists;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONValue;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.concurrent.TimeUnit;

@SpringBootTest(classes = GoodsStockApplication.class)
@Slf4j
@WebAppConfiguration
@AutoConfigureMockMvc
public class TimerTest {

    @Autowired private MeterRegistry meterRegistry;
    private final String APP_NAME = "bs-goods-stock";
    final private MockMvc mockMvc;

    @Autowired
    TimerTest(MockMvc mockMvc) {
        this.mockMvc = mockMvc;
    }

    @Test
    void testTimer(){
        Timer timer = meterRegistry.timer(APP_NAME);
        timer.record(this::querySupplier);
        timer.record(this::querySupplier);
        timer.record(this::querySupplier);
        timer.record(this::querySupplier);
        timer.record(this::querySupplier);
        timer.record(this::querySupplier);
        log.info(timer.count()+"");
        log.info(timer.max(TimeUnit.MILLISECONDS)+"");
        log.info(timer.mean(TimeUnit.MILLISECONDS)+"");
    }

    private void querySupplier() {
        try {
            query();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void query() throws Exception{
        final LockStockQueryDto arg = new LockStockQueryDto();
        arg.setWarehouseCodes(Lists.newArrayList("ck01"));
        arg.setMerchantCodes(Lists.newArrayList("1"));
        arg.setSkuCodes(Lists.newArrayList("sku01"));
        arg.setPageSize(1);
        arg.setPageNum(1);

        final MockHttpServletRequestBuilder restBlock = MockMvcRequestBuilders
                .post("/query/adapter/queryByCondition")
                .content(JSONValue.toJSONString(arg))
                .contentType("application/json")
                ;
        MvcResult result=mockMvc.perform(restBlock).andReturn();
        RestApiTool.printResult(result);
        RestApiTool.assertResponseSuccess(result);
    }
}

你可能感兴趣的:(后端开发)