Skip to content

Commit af8531d

Browse files
committed
2014040201
1 parent cf52bd7 commit af8531d

File tree

9 files changed

+1707
-0
lines changed

9 files changed

+1707
-0
lines changed

performance monitor/Chart.js

Lines changed: 1431 additions & 0 deletions
Large diffs are not rendered by default.

performance monitor/background.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
chrome.app.runtime.onLaunched.addListener(function() {
2+
var main_window = chrome.app.window.get('main');
3+
if(main_window){
4+
main_window.show();
5+
}
6+
else{
7+
chrome.app.window.create('main.html', {
8+
//'id': 'main',
9+
'bounds': {
10+
'width': 542,
11+
'height': 360
12+
},
13+
'resizable': false,
14+
'frame': 'none'
15+
});
16+
}
17+
});

performance monitor/control.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var current_window = chrome.app.window.current();
2+
3+
document.getElementById('minimize').onclick = function(){
4+
current_window.minimize();
5+
}
6+
7+
document.getElementById('close').onclick = function(){
8+
current_window.close();
9+
}
10.9 KB
Loading
3.29 KB
Loading
5.29 KB
Loading

performance monitor/main.html

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<html>
2+
<head>
3+
<title>Performance Monitor</title>
4+
<style>
5+
body {
6+
margin: 0;
7+
padding: 0;
8+
border: #EEE 1px solid;
9+
}
10+
11+
#title_bar {
12+
-webkit-app-region: drag;
13+
height: 22px;
14+
line-height: 22px;
15+
font-size: 16px;
16+
background: #EEE;
17+
padding: 0 10px;
18+
box-sizing: border-box;
19+
}
20+
21+
#title_bar a {
22+
-webkit-app-region: no-drag;
23+
display: inline-block;
24+
float: right;
25+
height: 12px;
26+
width: 12px;
27+
margin: 5px;
28+
border: gray 1px solid;
29+
box-sizing: border-box;
30+
border-radius: 6px;
31+
}
32+
33+
#title_bar a:hover {
34+
background: gray;
35+
}
36+
37+
#box_body {
38+
padding: 20px;
39+
}
40+
41+
.chart {
42+
margin-bottom: 20px;
43+
font-size: 0;
44+
}
45+
46+
.urge_item {
47+
color: gray;
48+
padding: 2px 0;
49+
font-size: 14px;
50+
border-bottom: #EEE 1px solid;
51+
margin-bottom: 4px;
52+
}
53+
</style>
54+
</head>
55+
<body>
56+
<div id="title_bar">Performance Monitor
57+
<a id="close" href="#"></a>
58+
<a id="minimize" href="#"></a>
59+
</div>
60+
<div id="box_body">
61+
<div class="urge_item">CPU Urge</div>
62+
<div class="chart">
63+
<canvas id="cpu_total" width="100" height="100"></canvas>
64+
<canvas id="cpu_history" width="400" height="100"></canvas>
65+
</div>
66+
<div class="urge_item">Memory Urge</div>
67+
<div class="chart">
68+
<canvas id="mem_total" width="100" height="100"></canvas>
69+
<canvas id="mem_history" width="400" height="100"></canvas>
70+
</div>
71+
</div>
72+
<script src="control.js"></script>
73+
<script src="Chart.js"></script>
74+
<script src="main.js"></script>
75+
</body>
76+
</html>

performance monitor/main.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
function getCpuUrge(callback){
2+
chrome.system.cpu.getInfo(function(info){
3+
var total = 0;
4+
var user = 0;
5+
var kernel = 0;
6+
for(var i=0; i<info.processors.length; i++){
7+
total += info.processors[i].usage.total - cpu_history.last_total[i];
8+
cpu_history.last_total[i] = info.processors[i].usage.total;
9+
user += info.processors[i].usage.user - cpu_history.last_user[i];
10+
cpu_history.last_user[i] = info.processors[i].usage.user;
11+
kernel += info.processors[i].usage.kernel - cpu_history.last_kernel[i];
12+
cpu_history.last_kernel[i] = info.processors[i].usage.kernel;
13+
}
14+
user = Math.round(user/total*100);
15+
kernel = Math.round(kernel/total*100);
16+
callback({user:user,kernel:kernel,total:user+kernel});
17+
});
18+
}
19+
20+
function getMemUrge(callback){
21+
chrome.system.memory.getInfo(function(info){
22+
callback(info);
23+
});
24+
}
25+
26+
function updateCpuHistory(){
27+
getCpuUrge(function(urge){
28+
cpu_history.user.shift();
29+
cpu_history.user.push(urge.user);
30+
cpu_history.kernel.shift();
31+
cpu_history.kernel.push(urge.kernel);
32+
cpu_history.total.shift();
33+
cpu_history.total.push(urge.total);
34+
showCpu();
35+
});
36+
}
37+
38+
function updateMemHistory(){
39+
getMemUrge(function(urge){
40+
mem_history.used.shift();
41+
mem_history.used.push(Math.round((urge.capacity-urge.availableCapacity)/urge.capacity*100));
42+
showMem();
43+
});
44+
}
45+
46+
function updateData(){
47+
updateCpuHistory();
48+
updateMemHistory();
49+
}
50+
51+
function showCpu(){
52+
var history = {
53+
labels : (function(){for(var i=0,labels=[];i<ponits_num;labels.push(''),i++);return labels;})(),
54+
datasets : [
55+
{
56+
fillColor : "rgba(220,220,220,0.5)",
57+
data : cpu_history.total
58+
},
59+
{
60+
fillColor : "rgba(90,140,255,0.5)",
61+
data : cpu_history.kernel
62+
},
63+
{
64+
fillColor : "rgba(255,90,90,0.5)",
65+
data : cpu_history.user
66+
}
67+
]
68+
};
69+
70+
var now = [
71+
{
72+
value: cpu_history.total[ponits_num-1],
73+
color:"rgba(220,220,220,0.7)"
74+
},
75+
{
76+
value : 100-cpu_history.total[ponits_num-1],
77+
color : "rgba(220,220,220,0.3)"
78+
}
79+
];
80+
var his_ctx = document.getElementById('cpu_history').getContext("2d");
81+
var now_ctx = document.getElementById("cpu_total").getContext("2d");
82+
new Chart(his_ctx).Line(history, {scaleFontSize:4,pointDot:false,animation:false});
83+
new Chart(now_ctx).Pie(now, {segmentShowStroke:false,animation:false});
84+
}
85+
86+
function showMem(){
87+
var history = {
88+
labels : (function(){for(var i=0,labels=[];i<ponits_num;labels.push(''),i++);return labels;})(),
89+
datasets : [
90+
{
91+
fillColor : "rgba(220,220,220,0.5)",
92+
data : mem_history.used
93+
}
94+
]
95+
};
96+
97+
var now = [
98+
{
99+
value: mem_history.used[ponits_num-1],
100+
color:"rgba(220,220,220,0.7)"
101+
},
102+
{
103+
value : 100-mem_history.used[ponits_num-1],
104+
color : "rgba(220,220,220,0.3)"
105+
}
106+
];
107+
var his_ctx = document.getElementById('mem_history').getContext("2d");
108+
var now_ctx = document.getElementById("mem_total").getContext("2d");
109+
new Chart(his_ctx).Line(history, {scaleFontSize:4,pointDot:false,animation:false});
110+
new Chart(now_ctx).Pie(now, {segmentShowStroke:false,animation:false});
111+
}
112+
113+
function init(){
114+
cpu_history = {
115+
user: [],
116+
kernel: [],
117+
total: [],
118+
last_user: [],
119+
last_kernel: [],
120+
last_total: []
121+
};
122+
mem_history = {
123+
used: []
124+
};
125+
init_cpu_history();
126+
}
127+
128+
function init_cpu_history(){
129+
for(var i=0; i<ponits_num; i++){
130+
cpu_history.user.push(0);
131+
cpu_history.kernel.push(0);
132+
cpu_history.total.push(0);
133+
}
134+
chrome.system.cpu.getInfo(function(info){
135+
for(var i=0; i<info.processors.length; i++){
136+
cpu_history.last_total.push(info.processors[i].usage.total);
137+
cpu_history.last_user.push(info.processors[i].usage.user);
138+
cpu_history.last_kernel.push(info.processors[i].usage.kernel);
139+
}
140+
init_mem_history();
141+
});
142+
}
143+
144+
function init_mem_history(){
145+
for(var i=0; i<ponits_num; i++){
146+
mem_history.used.push(0);
147+
}
148+
updateData();
149+
setInterval(updateData, 1000);
150+
}
151+
152+
var cpu_history, mem_history, ponits_num=20;
153+
154+
init();

performance monitor/manifest.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"app": {
3+
"background": {
4+
"scripts": ["background.js"]
5+
}
6+
},
7+
"manifest_version": 2,
8+
"name": "Performance Monitor",
9+
"version": "1.0",
10+
"description": "A performance monitor to show cpu and memory status.",
11+
"icons": {
12+
"16": "images/icon16.png",
13+
"48": "images/icon48.png",
14+
"128": "images/icon128.png"
15+
},
16+
"permissions": [
17+
"system.cpu",
18+
"system.memory"
19+
]
20+
}

0 commit comments

Comments
 (0)