JavaFx lineChart real-time Monitor

JavaFx lineChart real-time Monitor   about memory

  1 public class EffectTest extends Application {
  2 
  3 StackPane root;
  4 private static int MAX_DATA_POINTS = 20;
  5 private static int Y_DATA_RANGE = 10;
  6 private static int TICK_UNIT = 10;
  7 
  8 private static int UPDATE_INTERVAL_MS = 1000; 
  9 private LineChart.Series<Number, Number> series1;
 10 private LineChart<Number, Number> lineChart;
 11 private NumberAxis xAxis = new NumberAxis();
 12 private NumberAxis yAxis = new NumberAxis();
 13 
 14 private SequentialTransition animation;
 15 private Paint paintXticklabel; 
 16 private double nextX = 0;
 17 
 18 Random rnd = new Random();
 19 double currentMemBAK=0;
 20 public EffectTest() {
 21 
 22 Timeline timeline = new Timeline();
 23 timeline.getKeyFrames().add(new KeyFrame(Duration.millis(UPDATE_INTERVAL_MS*3), new EventHandler<ActionEvent>() {
 24 @Override
 25 public void handle(ActionEvent actionEvent) {
 26 
 27 // update chart data
 28 // note that we add one data point and remove one data point in this simple example. 
 29 // in a production environment you'd have to add multiple and remove multiple data points
 30 
 31 double currentMem= Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory(); 
 32 double drawy =(currentMem-currentMemBAK)/1000000;
 33 currentMemBAK=currentMem;
 34 System.out.println("currentMem:"+currentMem);
 35 // add new points
 36 //series1.getData().add(new XYChart.Data<Number, Number>(nextX, Math.cos(Math.toRadians(nextX)) * Y_DATA_RANGE));
 37 
 38 //mem
 39 series1.getData().add(new XYChart.Data<Number, Number>(nextX, drawy+10));
 40 
 41 //cpu
 42 //series1.getData().add(new XYChart.Data<Number, Number>(nextX, getCpuRatioForWindows()));
 43 
 44 
 45 // remove points that shouldn't be visible anymore
 46 if (series1.getData().size() > MAX_DATA_POINTS) {
 47 series1.getData().remove(0);
 48 
 49 }
 50 System.out.println("node size:"+series1.getData().size());
 51 
 52 nextX += 1;
 53 
 54 // update using series 1 as reference
 55 // series 2 contains same amount of data; if it doesn't for your case,
 56 // you need to adapt this here and calculate the proper range
 57 List<Data<Number, Number>> data = series1.getData();
 58 xAxis.setLowerBound(data.get(0).getXValue().doubleValue());
 59 xAxis.setUpperBound(data.get(data.size() - 1).getXValue().doubleValue());
 60 
 61 //////// 
 62 lineChart.getXAxis().setTickLabelFill(paintXticklabel);
 63 
 64 // xAxis.setTickUnit(1); 
 65 root.setTranslateX(-20);
 66 
 67 }
 68 }));
 69 timeline.setCycleCount(Animation.INDEFINITE);
 70 
 71 animation = new SequentialTransition();
 72 animation.getChildren().addAll(timeline);
 73 }
 74 
 75 public Parent createContent() {
 76 
 77 xAxis = new NumberAxis();
 78 xAxis.setForceZeroInRange(false);
 79 xAxis.setAutoRanging(false);
 80 xAxis.setTickLabelsVisible(false);
 81 xAxis.setTickMarkVisible(false);
 82 xAxis.setMinorTickVisible(false);
 83 xAxis=new NumberAxis(0, Y_DATA_RANGE+90, TICK_UNIT/10);
 84 
 85 // set Axis property 
 86 // final NumberAxis yAxis = new NumberAxis(1, 21, 0.1); 
 87 // xAxis.setTickUnit(1); 
 88 // xAxis.setPrefWidth(35); 
 89 // xAxis.setMinorTickCount(10); 
 90 // xAxis.setSide(Side.RIGHT); 
 91 // xAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(xAxis) { 
 92 // @Override 
 93 // public String toString(Number object) { 
 94 // String label; 
 95 // label = String.format("%7.2f", object.floatValue()); 
 96 // return label; 
 97 // } 
 98 // }); 
 99 
100 
101 //yAxis = new NumberAxis(-Y_DATA_RANGE, Y_DATA_RANGE, TICK_UNIT);
102 yAxis = new NumberAxis(0, Y_DATA_RANGE+90, TICK_UNIT/10);
103 yAxis.setAutoRanging(false);
104 
105 lineChart = new LineChart<>(xAxis, yAxis);
106 lineChart.setAnimated(false);
107 lineChart.setLegendVisible(false); 
108 
109 //set if dont want symbols on the point
110 lineChart.setCreateSymbols(false);
111 series1 = new LineChart.Series<>();
112 // series1.getData().add(new LineChart.Data<Number, Number>(0d, 0d));
113 lineChart.getData().add(series1);
114 
115 //save ticklabe
116 paintXticklabel=xAxis.getTickLabelFill();
117 return lineChart;
118 }
119 
120 public void play() {
121 animation.play();
122 }
123 
124 @Override
125 public void stop() {
126 animation.pause();
127 }
128 
129 @Override
130 public void start(Stage primaryStage) throws Exception {
131 
132 primaryStage.setTitle("Drawing Operations Test");
133 
134 root = new StackPane(); 
135 root.getChildren().add(createContent()); 
136 
137 Scene s= new Scene(root); 
138 primaryStage.setScene(s);
139 primaryStage.show(); 
140 
141 play();
142 }
143 
144 public static void main(String[] args) {
145 launch(args);
146 // getCpuRatioForWindows() ;
147 System.out.println(getCpuRatioForWindows());
148 
149 }
150 
151 private static final int CPUTIME = 500; 
152 private static final int PERCENT = 100; 
153 
154 public static double getCpuRatioForWindows() 
155 { 
156 try 
157 { 
158 String procCmd = 
159 System.getenv("windir") 
160 + "\system32\wbem\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount"; 
161 
162 long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd)); 
163 Thread.sleep(CPUTIME); 
164 long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd)); 
165 if (c0 != null && c1 != null) 
166 { 
167 long idletime = c1[0] - c0[0]; 
168 long busytime = c1[1] - c0[1]; 
169 return Double.valueOf(PERCENT * (busytime) * 1.0 / (busytime + idletime)).intValue() ; 
170 } 
171 else 
172 { 
173 return 0 ; 
174 } 
175 } 
176 catch (Exception ex) 
177 { 
178 ex.printStackTrace(); 
179 return 0 ; 
180 } 
181 } 
182 private static final int FAULTLENGTH = 10;
183 private static long[] readCpu(final Process proc) 
184 { 
185 long[] retn = new long[2]; 
186 try 
187 { 
188 proc.getOutputStream().close(); 
189 InputStreamReader ir = new InputStreamReader(proc.getInputStream()); 
190 LineNumberReader input = new LineNumberReader(ir); 
191 String line = input.readLine(); 
192 if (line == null || line.length() < FAULTLENGTH) 
193 { 
194 return null; 
195 } 
196 int capidx = line.indexOf("Caption"); 
197 int cmdidx = line.indexOf("CommandLine"); 
198 int rocidx = line.indexOf("ReadOperationCount"); 
199 int umtidx = line.indexOf("UserModeTime"); 
200 int kmtidx = line.indexOf("KernelModeTime"); 
201 int wocidx = line.indexOf("WriteOperationCount"); 
202 long idletime = 0; 
203 long kneltime = 0; 
204 long usertime = 0; 
205 while ((line = input.readLine()) != null) 
206 { 
207 if (line.length() < wocidx) 
208 { 
209 continue; 
210 } 
211 //Caption,CommandLine,KernelModeTime,ReadOperationCount, 
212 // ThreadCount,UserModeTime,WriteOperation 
213 String caption = substring(line, capidx, cmdidx - 1).trim(); 
214 String cmd = substring(line, cmdidx, kmtidx - 1).trim(); 
215 if (cmd.indexOf("wmic.exe") >= 0) 
216 { 
217 continue; 
218 } 
219 String s1 = substring(line, kmtidx, rocidx - 1).trim(); 
220 String s2 = substring(line, umtidx, wocidx - 1).trim(); 
221 if (caption.equals("System Idle Process") || caption.equals("System")) 
222 { 
223 if (s1.length() > 0) 
224 idletime += Long.valueOf(s1).longValue(); 
225 if (s2.length() > 0) 
226 idletime += Long.valueOf(s2).longValue(); 
227 continue; 
228 } 
229 if (s1.length() > 0) 
230 kneltime += Long.valueOf(s1).longValue(); 
231 if (s2.length() > 0) 
232 usertime += Long.valueOf(s2).longValue(); 
233 } 
234 retn[0] = idletime; 
235 retn[1] = kneltime + usertime; 
236 return retn; 
237 } 
238 catch (Exception ex) 
239 { 
240 ex.printStackTrace(); 
241 } 
242 finally 
243 { 
244 try 
245 { 
246 proc.getInputStream().close(); 
247 } 
248 catch (Exception e) 
249 { 
250 e.printStackTrace(); 
251 } 
252 } 
253 return null; 
254 } 
255 private static String substring(String src, int start_idx, int end_idx) 
256 { 
257 byte[] b = src.getBytes(); 
258 String tgt = ""; 
259 for (int i = start_idx; i <= end_idx; i++) 
260 { 
261 tgt += (char)b[i]; 
262 } 
263 return tgt; 
264 } 
265 }
原文地址:https://www.cnblogs.com/rojas/p/4720109.html