Skip to content

Commit 037c17e

Browse files
committed
性能监控
1 parent c9b9e6e commit 037c17e

File tree

6 files changed

+169
-137
lines changed

6 files changed

+169
-137
lines changed

doc/DemoWeb.PDM

+2-1
Original file line numberDiff line numberDiff line change
@@ -4515,9 +4515,10 @@ TableComment 0 新宋体,8,N</a:FontList>
45154515
<a:Code>id</a:Code>
45164516
<a:CreationDate>1474274153</a:CreationDate>
45174517
<a:Creator>Administrator</a:Creator>
4518-
<a:ModificationDate>1474274153</a:ModificationDate>
4518+
<a:ModificationDate>1474611802</a:ModificationDate>
45194519
<a:Modifier>Administrator</a:Modifier>
45204520
<a:DataType>bigint</a:DataType>
4521+
<a:Identity>1</a:Identity>
45214522
<a:Mandatory>1</a:Mandatory>
45224523
</o:Column>
45234524
<o:Column Id="o134">

doc/DemoWeb.pdb

+2-1
Original file line numberDiff line numberDiff line change
@@ -4515,9 +4515,10 @@ TableComment 0 新宋体,8,N</a:FontList>
45154515
<a:Code>id</a:Code>
45164516
<a:CreationDate>1474274153</a:CreationDate>
45174517
<a:Creator>Administrator</a:Creator>
4518-
<a:ModificationDate>1474274153</a:ModificationDate>
4518+
<a:ModificationDate>1474611802</a:ModificationDate>
45194519
<a:Modifier>Administrator</a:Modifier>
45204520
<a:DataType>bigint</a:DataType>
4521+
<a:Identity>1</a:Identity>
45214522
<a:Mandatory>1</a:Mandatory>
45224523
</o:Column>
45234524
<o:Column Id="o134">

doc/mysql/1_init_table_mysql.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*==============================================================*/
22
/* DBMS name: MySQL 5.0 */
3-
/* Created on: 2016-09-19 星期一 17:14:49 */
3+
/* Created on: 2016-09-23 星期五 14:26:00 */
44
/*==============================================================*/
55

66

@@ -58,7 +58,7 @@ create table buss
5858
/*==============================================================*/
5959
create table capability
6060
(
61-
id bigint not null,
61+
id bigint not null auto_increment,
6262
ip varchar(64) not null,
6363
type varchar(16) not null,
6464
subType varchar(64),

src/main/java/com/caozj/framework/util/data/MathUtil.java

+143-133
Original file line numberDiff line numberDiff line change
@@ -5,149 +5,159 @@
55
import java.util.regex.Pattern;
66

77
public final strictfp class MathUtil {
8-
private MathUtil() {
9-
}
8+
private MathUtil() {}
109

11-
// 默认运算精度
12-
private static int DEF_SCALE = 10;
10+
// 默认运算精度
11+
private static int DEF_SCALE = 10;
1312

14-
/**
15-
* 提供数据类型转换为BigDecimal
16-
*
17-
* @param object 原始数据
18-
* @return BigDecimal
19-
*/
20-
public static final BigDecimal bigDecimal(Object object) {
21-
if (object == null) {
22-
throw new NullPointerException();
23-
}
24-
BigDecimal result;
25-
try {
26-
result = new BigDecimal(String.valueOf(object).replaceAll(",", ""));
27-
} catch (NumberFormatException e) {
28-
throw new NumberFormatException("Please give me a numeral.Not " + object);
29-
}
30-
return result;
31-
}
13+
/**
14+
* 提供数据类型转换为BigDecimal
15+
*
16+
* @param object 原始数据
17+
* @return BigDecimal
18+
*/
19+
public static final BigDecimal bigDecimal(Object object) {
20+
if (object == null) {
21+
throw new NullPointerException();
22+
}
23+
BigDecimal result;
24+
try {
25+
result = new BigDecimal(String.valueOf(object).replaceAll(",", ""));
26+
} catch (NumberFormatException e) {
27+
throw new NumberFormatException("Please give me a numeral.Not " + object);
28+
}
29+
return result;
30+
}
3231

33-
/**
34-
* 提供(相对)精确的加法运算。
35-
*
36-
* @param num1 被加数
37-
* @param num2 加数
38-
* @return 两个参数的和
39-
*/
40-
public static final Double add(Object num1, Object num2) {
41-
BigDecimal result = bigDecimal(num1).add(bigDecimal(num2));
42-
return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
43-
}
32+
/**
33+
* 提供(相对)精确的加法运算。
34+
*
35+
* @param num1 被加数
36+
* @param num2 加数
37+
* @return 两个参数的和
38+
*/
39+
public static final Double add(Object num1, Object num2) {
40+
BigDecimal result = bigDecimal(num1).add(bigDecimal(num2));
41+
return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
42+
}
4443

45-
/**
46-
* 提供(相对)精确的减法运算。
47-
*
48-
* @param num1 被减数
49-
* @param num2 减数
50-
* @return 两个参数的差
51-
*/
52-
public static final Double subtract(Object num1, Object num2) {
53-
BigDecimal result = bigDecimal(num1).subtract(bigDecimal(num2));
54-
return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
55-
}
44+
/**
45+
* 提供(相对)精确的减法运算。
46+
*
47+
* @param num1 被减数
48+
* @param num2 减数
49+
* @return 两个参数的差
50+
*/
51+
public static final Double subtract(Object num1, Object num2) {
52+
BigDecimal result = bigDecimal(num1).subtract(bigDecimal(num2));
53+
return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
54+
}
5655

57-
/**
58-
* 提供(相对)精确的乘法运算。
59-
*
60-
* @param num1 被乘数
61-
* @param num2 乘数
62-
* @return 两个参数的积
63-
*/
64-
public static final Double multiply(Object num1, Object num2) {
65-
BigDecimal result = bigDecimal(num1).multiply(bigDecimal(num2));
66-
return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
67-
}
56+
/**
57+
* 提供(相对)精确的乘法运算。
58+
*
59+
* @param num1 被乘数
60+
* @param num2 乘数
61+
* @return 两个参数的积
62+
*/
63+
public static final Double multiply(Object num1, Object num2) {
64+
BigDecimal result = bigDecimal(num1).multiply(bigDecimal(num2));
65+
return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
66+
}
6867

69-
/**
70-
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精度为10位,以后的数字四舍五入。
71-
*
72-
* @param num1 被除数
73-
* @param num2 除数
74-
* @return 两个参数的商
75-
*/
76-
public static final Double divide(Object num1, Object num2) {
77-
return divide(num1, num2, DEF_SCALE);
78-
}
68+
/**
69+
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精度为10位,以后的数字四舍五入。
70+
*
71+
* @param num1 被除数
72+
* @param num2 除数
73+
* @return 两个参数的商
74+
*/
75+
public static final Double divide(Object num1, Object num2) {
76+
return divide(num1, num2, DEF_SCALE);
77+
}
7978

80-
/**
81-
* 提供(相对)精确的除法运算。 当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入。
82-
*
83-
* @param num1 被除数
84-
* @param num2 除数
85-
* @param scale 表示表示需要精确到小数点以后几位。
86-
* @return 两个参数的商
87-
*/
88-
public static final Double divide(Object num1, Object num2, Integer scale) {
89-
if (scale == null) {
90-
scale = DEF_SCALE;
91-
}
92-
num2 = num2 == null || Math.abs(new Double(num2.toString())) == 0 ? 1 : num2;
93-
if (scale < 0) {
94-
throw new IllegalArgumentException("The scale must be a positive integer or zero");
95-
}
96-
BigDecimal result = bigDecimal(num1).divide(bigDecimal(num2), scale, BigDecimal.ROUND_HALF_UP);
97-
return result.doubleValue();
98-
}
79+
/**
80+
* 提供(相对)精确的除法运算。 当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入。
81+
*
82+
* @param num1 被除数
83+
* @param num2 除数
84+
* @param scale 表示表示需要精确到小数点以后几位。
85+
* @return 两个参数的商
86+
*/
87+
public static final Double divide(Object num1, Object num2, Integer scale) {
88+
if (scale == null) {
89+
scale = DEF_SCALE;
90+
}
91+
num2 = num2 == null || Math.abs(new Double(num2.toString())) == 0 ? 1 : num2;
92+
if (scale < 0) {
93+
throw new IllegalArgumentException("The scale must be a positive integer or zero");
94+
}
95+
BigDecimal result = bigDecimal(num1).divide(bigDecimal(num2), scale, BigDecimal.ROUND_HALF_UP);
96+
return result.doubleValue();
97+
}
9998

100-
/**
101-
* 提供精确的小数位四舍五入处理。
102-
*
103-
* @param num 需要四舍五入的数字
104-
* @param scale 小数点后保留几位
105-
* @return 四舍五入后的结果
106-
*/
107-
public static final Double round(Object num, int scale) {
108-
if (scale < 0) {
109-
throw new IllegalArgumentException("The scale must be a positive integer or zero");
110-
}
111-
BigDecimal result = bigDecimal(num).divide(bigDecimal("1"), scale, BigDecimal.ROUND_HALF_UP);
112-
return result.doubleValue();
113-
}
99+
/**
100+
* 提供精确的小数位四舍五入处理。
101+
*
102+
* @param num 需要四舍五入的数字
103+
* @param scale 小数点后保留几位
104+
* @return 四舍五入后的结果
105+
*/
106+
public static final Double round(Object num, int scale) {
107+
if (scale < 0) {
108+
throw new IllegalArgumentException("The scale must be a positive integer or zero");
109+
}
110+
BigDecimal result = bigDecimal(num).divide(bigDecimal("1"), scale, BigDecimal.ROUND_HALF_UP);
111+
return result.doubleValue();
112+
}
114113

115-
/**
116-
* 获取start到end区间的随机数,不包含start+end
117-
*
118-
* @param start
119-
* @param end
120-
* @return
121-
*/
122-
public static final BigDecimal getRandom(int start, int end) {
123-
return new BigDecimal(start + Math.random() * end);
124-
}
114+
/**
115+
* 获取start到end区间的随机数,不包含start+end
116+
*
117+
* @param start
118+
* @param end
119+
* @return
120+
*/
121+
public static final BigDecimal getRandom(int start, int end) {
122+
return new BigDecimal(start + Math.random() * end);
123+
}
125124

126-
/**
127-
* 格式化
128-
*
129-
* @param obj
130-
* @param pattern
131-
* @return
132-
*/
133-
public static final String format(Object obj, String pattern) {
134-
if (obj == null) {
135-
return null;
136-
}
137-
if (pattern == null || "".equals(pattern)) {
138-
pattern = "#";
139-
}
140-
DecimalFormat format = new DecimalFormat(pattern);
141-
return format.format(bigDecimal(obj));
142-
}
125+
/**
126+
* 格式化
127+
*
128+
* @param obj
129+
* @param pattern
130+
* @return
131+
*/
132+
public static final String format(Object obj, String pattern) {
133+
if (obj == null) {
134+
return null;
135+
}
136+
if (pattern == null || "".equals(pattern)) {
137+
pattern = "#";
138+
}
139+
DecimalFormat format = new DecimalFormat(pattern);
140+
return format.format(bigDecimal(obj));
141+
}
143142

144-
/** 是否数字 */
145-
public static final boolean isNumber(Object object) {
146-
Pattern pattern = Pattern.compile("\\d+(.\\d+)?$");
147-
return pattern.matcher(object.toString()).matches();
148-
}
143+
/**
144+
* 保留有效小数
145+
*
146+
* @param value 数字
147+
* @param count 小数位
148+
* @return
149+
*/
150+
public static final double scale(double value, int count) {
151+
return new BigDecimal(value).setScale(count, BigDecimal.ROUND_HALF_UP).doubleValue();
152+
}
149153

150-
public static final void main(String[] args) {
151-
System.out.println(add(1.000001, 2.10));
152-
}
154+
/** 是否数字 */
155+
public static final boolean isNumber(Object object) {
156+
Pattern pattern = Pattern.compile("\\d+(.\\d+)?$");
157+
return pattern.matcher(object.toString()).matches();
158+
}
159+
160+
public static final void main(String[] args) {
161+
System.out.println(add(1.000001, 2.10));
162+
}
153163
}

src/main/java/com/caozj/sigar/service/impl/CapabilityServiceImpl.java

+7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import java.util.List;
55
import java.util.Map;
66

7+
import org.apache.commons.lang3.StringUtils;
78
import org.hyperic.sigar.SigarException;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.stereotype.Service;
1011
import org.springframework.transaction.annotation.Transactional;
1112

1213
import com.caozj.framework.util.common.SystemUtil;
14+
import com.caozj.framework.util.data.MathUtil;
1315
import com.caozj.framework.util.date.DateUtil;
1416
import com.caozj.framework.util.jdbc.Pager;
1517
import com.caozj.framework.util.sigar.SigarUtil;
@@ -182,6 +184,7 @@ private void collectNetworkData(String now, String ip) throws SigarException {
182184
private void collectDiskData(String now, String ip) throws SigarException {
183185
Map<String, Double> valueMap = SigarUtil.getInstance().getFileSystemUsedPercent();
184186
valueMap.forEach((key, value) -> {
187+
value = MathUtil.scale(value, 2);
185188
Capability capability = new Capability();
186189
capability.setIp(ip);
187190
capability.setDateTime(now);
@@ -200,11 +203,13 @@ private void collectDiskData(String now, String ip) throws SigarException {
200203
*/
201204
private void collectMemoryData(String now, String ip) throws SigarException {
202205
double value = SigarUtil.getInstance().getMemoryUsedPercent();
206+
value = MathUtil.scale(value, 2);
203207
Capability capability = new Capability();
204208
capability.setIp(ip);
205209
capability.setDateTime(now);
206210
capability.setType(CapabilityType.MEMORY.getName());
207211
capability.setValue(value);
212+
capability.setSubType(StringUtils.EMPTY);
208213
this.add(capability);
209214
}
210215

@@ -217,11 +222,13 @@ private void collectMemoryData(String now, String ip) throws SigarException {
217222
*/
218223
private void collectCpuData(String now, String ip) throws SigarException {
219224
double value = SigarUtil.getInstance().getCpuUsedPercent();
225+
value = MathUtil.scale(value, 2);
220226
Capability capability = new Capability();
221227
capability.setIp(ip);
222228
capability.setDateTime(now);
223229
capability.setType(CapabilityType.CPU.getName());
224230
capability.setValue(value);
231+
capability.setSubType(StringUtils.EMPTY);
225232
this.add(capability);
226233
}
227234

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.caozj.taglib;
2+
3+
import com.caozj.framework.util.data.MathUtil;
4+
5+
public class TD {
6+
7+
public static void main(String[] args) {
8+
double a = 123;
9+
double b = MathUtil.scale(a, 2);
10+
System.out.println(b);
11+
}
12+
13+
}

0 commit comments

Comments
 (0)