Skip to content

Commit

Permalink
Merge pull request #1439 from hanbingleixue/cooperate-with-gala-gophe…
Browse files Browse the repository at this point in the history
…r-alpha

修改性能测试的问题
  • Loading branch information
Sherlockhan authored Feb 6, 2024
2 parents b5bbe32 + 5e69eea commit d9c8c2b
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 128 deletions.
6 changes: 0 additions & 6 deletions sermant-plugins/sermant-metrics/metrics-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
<artifactId>sermant-agentcore-core</artifactId>
<scope>provided</scope>
</dependency>
<!--SQL解析第三方依赖-->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>${jsqlparser.version}</version>
</dependency>
<!--test依赖-->
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ public class Constants {
*/
public static final byte[] DUBBO_SERVER_ERROR = {31, 80, 50, 60, 70, 100};

/**
* 时延范围纳秒
*/
public static final long[] LATENCY_RANGE = {3000000L, 10000000L, 50000000L, 100000000L, 500000000L, 1000000000L,
10000000000L};

/**
* 时延统计的key
*/
public static final String[] LATENCY_COUNT_KEY = {"COUNT_ZERO_TO_THREE", "COUNT_ZERO_TO_TEN",
"COUNT_ZERO_TO_FIFTY", "COUNT_ZERO_TO_ONE_HUNDRED", "COUNT_ZERO_TO_FIVE_HUNDRED",
"COUNT_ZERO_TO_ONE_THOUSAND", "COUNT_ZERO_TO_TEN_THOUSAND"};

/**
* 空格字符
*/
public static final String SPACE = " ";

private Constants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.huawei.metrics.entity;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -40,7 +40,7 @@ public class MetricsRpcInfo extends MetricsInfo {

private AtomicInteger serverErrorCount = new AtomicInteger();

private List<Long> latencyList = new CopyOnWriteArrayList<>();
private Map<String, AtomicInteger> latencyCounts = new ConcurrentHashMap<>();

public AtomicInteger getReqCount() {
return reqCount;
Expand Down Expand Up @@ -74,12 +74,12 @@ public void setReqErrorCount(AtomicInteger reqErrorCount) {
this.reqErrorCount = reqErrorCount;
}

public List<Long> getLatencyList() {
return latencyList;
public Map<String, AtomicInteger> getLatencyCounts() {
return latencyCounts;
}

public void setLatencyList(List<Long> latencyList) {
this.latencyList = latencyList;
public void setLatencyCounts(Map<String, AtomicInteger> latencyCounts) {
this.latencyCounts = latencyCounts;
}

public AtomicInteger getClientErrorCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Metrics管理类
Expand Down Expand Up @@ -50,14 +51,35 @@ public static Map<String, MetricsRpcInfo> getRpcInfoMap() {
* @param metricsRpcInfo RPC信息
*/
public static void saveRpcInfo(MetricsRpcInfo metricsRpcInfo) {
String key = getKey(metricsRpcInfo);
MetricsRpcInfo rpcInfo = METRICS_RPC_INFO_MAP.computeIfAbsent(key, s -> metricsRpcInfo);
if (rpcInfo != metricsRpcInfo) {
rpcInfo.getReqCount().getAndAdd(metricsRpcInfo.getReqCount().get());
rpcInfo.getResponseCount().getAndAdd(metricsRpcInfo.getResponseCount().get());
rpcInfo.getSumLatency().getAndAdd(metricsRpcInfo.getSumLatency().get());
rpcInfo.getLatencyList().addAll(metricsRpcInfo.getLatencyList());
rpcInfo.getReqErrorCount().getAndAdd(metricsRpcInfo.getReqErrorCount().get());
MetricsRpcInfo rpcInfo = METRICS_RPC_INFO_MAP.computeIfAbsent(getKey(metricsRpcInfo), key -> metricsRpcInfo);
if (rpcInfo == metricsRpcInfo) {
return;
}
rpcInfo.getReqCount().getAndAdd(metricsRpcInfo.getReqCount().get());
rpcInfo.getResponseCount().getAndAdd(metricsRpcInfo.getResponseCount().get());
long latency = metricsRpcInfo.getSumLatency().get();
rpcInfo.getSumLatency().getAndAdd(latency);
rpcInfo.getReqErrorCount().getAndAdd(metricsRpcInfo.getReqErrorCount().get());
for (int index = 0; index < Constants.LATENCY_RANGE.length; index++) {
if (latency < Constants.LATENCY_RANGE[index]) {
modifyLatencyCountsByRange(index, rpcInfo);
break;
}
}
}

/**
* 根据时延范围下标修改时延统计数量。时延统计数量对应的时延范围包含该时延范围时,时延统计数量+1,
* 由于时延统计数量自增排列,下标大于该时延范围下标的对应的时延范围都包含该时延范围时。
*
* @param rangeIndex 时延所属范围的数组下标
* @param rpcInfo 指标信息
*/
private static void modifyLatencyCountsByRange(int rangeIndex, MetricsRpcInfo rpcInfo) {
for (int index = rangeIndex; index < Constants.LATENCY_RANGE.length; index++) {
AtomicInteger atomicInteger = rpcInfo.getLatencyCounts()
.computeIfAbsent(Constants.LATENCY_COUNT_KEY[index], key -> new AtomicInteger());
atomicInteger.getAndIncrement();
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@
import com.huawei.metrics.common.Constants;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.utils.StringUtils;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;

import java.util.Locale;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* SQL解析工具类
Expand All @@ -39,9 +34,16 @@
public class SqlParseUtil {
private static final Logger LOGGER = LoggerFactory.getLogger();

private static final String STATEMENT_CLASS_NAME_SUFFIX = "Statement";
private static final Pattern COMMAND_TYPE_PATTERN =
Pattern.compile("^*(INSERT\\b|UPDATE\\b|DELETE\\b|CREATE\\b|ALTER\\b|DROP\\b|TRUNCATE\\b|SET\\b"
+ "|COMMIT\\b|SHOW\\b|USE\\b|SELECT\\b)",
Pattern.CASE_INSENSITIVE);

private static final String STATEMENT_CLASS_NAME_PREFIX = "Plain";
private static final Pattern TABLE_PATTERN =
Pattern.compile("(?i)(?:CREATE\\s+INDEX\\s+\\$\\{\\w+\\}\\s+ON|SELECT\\s+\\*\\s+FROM|DROP\\s+INDEX\\s"
+ "+\\w+\\s+ON|CREATE\\s+INDEX\\s+\\w+\\s+ON|ALTER\\s+TABLE|INSERT\\s+INTO|UPDATE|TRUNCATE\\s+TABLE"
+ "|CREATE\\s+TABLE|DELETE\\s+FROM|DROP\\s+"
+ "TABLE)\\s+`*(\\w+)`*", Pattern.CASE_INSENSITIVE);

private SqlParseUtil() {
}
Expand All @@ -55,21 +57,18 @@ private SqlParseUtil() {
public static String getApi(String sql) {
StringBuilder stringBuilder = new StringBuilder();
try {
Statement statement = CCJSqlParserUtil.parse(sql);
String className = statement.getClass().getSimpleName();
String commandType = className.replace(STATEMENT_CLASS_NAME_PREFIX, StringUtils.EMPTY)
.replace(STATEMENT_CLASS_NAME_SUFFIX, StringUtils.EMPTY)
.toLowerCase(Locale.ROOT);
stringBuilder.append(commandType);
MysqlTablesNameFinder tablesNamesFinder = new MysqlTablesNameFinder();
Set<String> tables = tablesNamesFinder.getTables(statement);
if (tables != null) {
for (String table : tables) {
stringBuilder.append(Constants.CONNECT).append(table.toLowerCase(Locale.ROOT));
}
Matcher commandTypeMatcher = COMMAND_TYPE_PATTERN.matcher(sql);
if (commandTypeMatcher.find()) {
stringBuilder.append(commandTypeMatcher.group());
}
Matcher tableMatcher = TABLE_PATTERN.matcher(sql);
while (tableMatcher.find()) {
String group = tableMatcher.group();
String[] groupStr = group.split(Constants.SPACE);
stringBuilder.append(Constants.CONNECT).append(groupStr[groupStr.length - 1]);
}
return stringBuilder.toString();
} catch (JSQLParserException | UnsupportedOperationException e) {
} catch (UnsupportedOperationException e) {
LOGGER.log(Level.INFO, "There is currently no table data in SQL.");
return stringBuilder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public ExecuteContext after(ExecuteContext context) {
metricsRpcInfo.getResponseCount().incrementAndGet();
long latency = System.nanoTime() - (long) startTimeObject;
metricsRpcInfo.getSumLatency().addAndGet(latency);
metricsRpcInfo.getLatencyList().add(latency);
MetricsManager.saveRpcInfo(metricsRpcInfo);
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public MetricsRpcInfo createRpcInfo(String sql, ExecuteContext context, long lat
MetricsRpcInfo metricsRpcInfo = initMetricsRpcInfo(context, enableSsl, protocol.getHost(), protocol.getPort(),
sql);
metricsRpcInfo.getSumLatency().getAndAdd(latency);
metricsRpcInfo.getLatencyList().add(latency);
return metricsRpcInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public ExecuteContext saveMetricInfo(ExecuteContext context, String sql, long la
HostAddress hostAddress = client.getHostAddress();
MetricsRpcInfo metricsRpcInfo = initMetricsRpcInfo(context, enableSsl, hostAddress.host, hostAddress.port, sql);
metricsRpcInfo.getSumLatency().getAndAdd(latency);
metricsRpcInfo.getLatencyList().add(latency);
MetricsManager.saveRpcInfo(metricsRpcInfo);
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public ExecuteContext collectMetrics(ExecuteContext context) {
url.getHost(), url.getPort(), sql);
long latency = System.nanoTime() - (long) context.getLocalFieldValue(Constants.START_TIME_KEY);
metricsRpcInfo.getSumLatency().getAndAdd(latency);
metricsRpcInfo.getLatencyList().add(latency);
MetricsManager.saveRpcInfo(metricsRpcInfo);
return context;
} catch (SQLException | URISyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public ExecuteContext collectMetrics(ExecuteContext context) {
url.getHost(), url.getPort(), sql);
long latency = System.nanoTime() - (long) context.getLocalFieldValue(Constants.START_TIME_KEY);
metricsRpcInfo.getSumLatency().getAndAdd(latency);
metricsRpcInfo.getLatencyList().add(latency);
MetricsManager.saveRpcInfo(metricsRpcInfo);
return context;
} catch (SQLException | URISyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public ExecuteContext collectMetrics(ExecuteContext context) {
hostInfo.getHost(), hostInfo.getPort(), sql);
long latency = System.nanoTime() - (long) startTime;
metricsRpcInfo.getSumLatency().getAndAdd(latency);
metricsRpcInfo.getLatencyList().add(latency);
MetricsManager.saveRpcInfo(metricsRpcInfo);
return context;
}
Expand Down
Loading

0 comments on commit d9c8c2b

Please sign in to comment.