-
Notifications
You must be signed in to change notification settings - Fork 382
/
ApplicationTest.java
124 lines (108 loc) · 3.39 KB
/
ApplicationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
/*
* Copyright (C) 2020 The zfoo Authors
*
* 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.
*/
package com.zfoo.monitor;
import com.zfoo.monitor.util.JvmUtils;
import com.zfoo.monitor.util.MonitorUtils;
import com.zfoo.monitor.util.OSUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.ThreadUtils;
import org.junit.Assert;
import org.junit.Test;
import oshi.SystemInfo;
/**
* @author godotg
*/
public class ApplicationTest {
/**
* 仿Linux的uptime指令,可以用来监控cpu的负载,windows中无法统计,所以在windows返回的结果默认是-1
*/
@Test
public void uptimeTest() {
var vo = OSUtils.uptime();
System.out.println(JsonUtils.object2String(vo));
System.out.println(vo.pressure());
}
/**
* 仿Linux的df指令,可以用来监控硬盘容量
*/
@Test
public void dfTest() {
var df = OSUtils.df();
df.forEach(it -> {
System.out.println(JsonUtils.object2String(it.toGB()));
System.out.println(it.pressure());
});
}
/**
* 仿Linux的free指令,可以用来监控内存占用
*/
@Test
public void freeTest() {
var free = OSUtils.free();
System.out.println(JsonUtils.object2String(free.toGB()));
System.out.println(free.pressure());
}
/**
* 仿Linux的sar指令,可以用来监控网络IO
*/
@Test
public void sarTest() {
var sar = OSUtils.sar();
sar.forEach(it -> {
System.out.println(JsonUtils.object2String(it));
System.out.println(it.pressure());
});
}
/**
* cpu的tick大小测试
*/
@Test
public void cpuTest() {
var systemInfo = new SystemInfo();
var hardware = systemInfo.getHardware();
var os = systemInfo.getOperatingSystem();
for (int i = 0; i < 5; i++) {
var oldTicks = hardware.getProcessor().getSystemCpuLoadTicks();
ThreadUtils.sleep(1000);
var usage = hardware.getProcessor().getSystemCpuLoadBetweenTicks(oldTicks);
System.out.println(usage);
}
}
@Test
public void toPercentTest() {
var num = 0.123456D;
var str = OSUtils.toPercent(num);
Assert.assertEquals(str, "12.35%");
}
@Test
public void monitorTest() {
var monitor = MonitorUtils.monitor();
System.out.println(monitor);
ThreadUtils.sleep(1000);
monitor = MonitorUtils.monitor();
System.out.println(monitor);
monitor = MonitorUtils.maxMonitor();
System.out.println(monitor);
}
@Test
public void JvmTest() {
JvmUtils.getJvmInfo().forEach(a -> {
System.out.println(a.toString());
});
}
@Test
public void osTest() {
var os = OSUtils.os();
System.out.println(JsonUtils.object2StringPrettyPrinter(os));
}
}