VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/samples/java/jax-ws/metrictest.java

最後變更 在這個檔案是 106061,由 vboxsync 提交於 4 月 前

Copyright year updates by scm.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 7.1 KB
 
1/* $Id: metrictest.java 106061 2024-09-16 14:03:52Z vboxsync $ */
2/*!file
3 * Sample of performance API usage, written in Java.
4 *
5 * Don't forget to run VBOX webserver
6 * with 'vboxwebsrv -t 1000' command, to calm down watchdog thread.
7 *
8 * The following license applies to this file only:
9 */
10
11/*
12 * Copyright (C) 2008-2024 Oracle and/or its affiliates.
13 *
14 * Permission is hereby granted, free of charge, to any person
15 * obtaining a copy of this software and associated documentation
16 * files (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use,
18 * copy, modify, merge, publish, distribute, sublicense, and/or sell
19 * copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following
21 * conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*;
37
38import java.util.*;
39import javax.xml.ws.Holder;
40
41class PerformanceData
42{
43 public String name;
44 public IUnknown object;
45 public String unit;
46 public Long scale;
47 public Long sequenceNumber;
48 public List<Long> samples;
49
50 public String getFormattedSamples()
51 {
52 String out = "[";
53 String separator = "";
54
55 if (scale != 1)
56 {
57 for (Long sample : samples)
58 {
59 out += separator + (sample.doubleValue() / scale) + " " + unit;
60 separator = ", ";
61 }
62 }
63 else
64 {
65 for (Long sample : samples)
66 {
67 out += separator + sample.toString() + " " + unit;
68 separator = ", ";
69 }
70 }
71 out += "]";
72 return out;
73 }
74}
75
76class PerformanceCollector
77{
78 private IVirtualBox _vbox;
79 private IPerformanceCollector _collector;
80
81 public PerformanceCollector(IVirtualBox vbox)
82 {
83 _vbox = vbox;
84 _collector = vbox.getPerformanceCollector();
85 }
86
87 public void cleanup()
88 {
89 _collector.releaseRemote();
90 }
91
92 public List<IPerformanceMetric> setup(List<String> metricNames, List<IUnknown> objects, Long period, Long samples)
93 {
94 return _collector.setupMetrics(metricNames, objects, period, samples);
95 }
96
97 public List<IPerformanceMetric> enable(List<String> metricNames, List<IUnknown> objects)
98 {
99 return _collector.enableMetrics(metricNames, objects);
100 }
101
102 public List<IPerformanceMetric> disable(List<String> metricNames, List<IUnknown> objects)
103 {
104 return _collector.disableMetrics(metricNames, objects);
105 }
106
107 public List<PerformanceData> query(List<String> filterMetrics, List<IUnknown> filterObjects)
108 {
109 Holder<List<String>> names = new Holder<List<String>>();
110 Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>();
111 Holder<List<String>> units = new Holder<List<String>>();
112 Holder<List<Long>> scales = new Holder<List<Long>>();
113 Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
114 Holder<List<Long>> indices = new Holder<List<Long>>();
115 Holder<List<Long>> lengths = new Holder<List<Long>>();
116 List<Integer> values =
117 _collector.queryMetricsData(filterMetrics, filterObjects,
118 names, objects, units, scales, sequenceNumbers, indices, lengths);
119 List<PerformanceData> data = new ArrayList<PerformanceData>(names.value.size());
120 for (int i = 0; i < names.value.size(); i++)
121 {
122 PerformanceData singleMetricData = new PerformanceData();
123 singleMetricData.name = names.value.get(i);
124 singleMetricData.object = objects.value.get(i);
125 singleMetricData.unit = units.value.get(i);
126 singleMetricData.scale = scales.value.get(i);
127 singleMetricData.sequenceNumber = sequenceNumbers.value.get(i);
128 List<Long> samples = new ArrayList<Long>(lengths.value.get(i).intValue());
129 for (int j = 0; j < lengths.value.get(i); j++)
130 {
131 samples.add(values.get(indices.value.get(i).intValue() + j).longValue());
132 }
133 singleMetricData.samples = samples;
134 data.add(singleMetricData);
135 }
136
137 return data;
138 }
139}
140
141public class metrictest implements Runnable
142{
143 IVirtualBox vbox;
144 IWebsessionManager mgr;
145 PerformanceCollector perf;
146
147 public metrictest()
148 {
149 mgr = new IWebsessionManager("http://localhost:18083/");
150 vbox = mgr.logon("test", "test");
151 System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
152 perf = new PerformanceCollector(vbox);
153 }
154
155 private String getObjectName(IUnknown object)
156 {
157 try
158 {
159 String machineName = object.getRemoteWSPort().iMachineGetName(object.getRef());
160 return machineName;
161 } catch (Exception e)
162 {
163 }
164 return new String("host");
165 }
166
167 public void setup()
168 {
169 perf.setup(Arrays.asList(new String[]{"*"}),
170 new ArrayList<IUnknown>(),
171 new Long(1), new Long(5));
172 }
173
174 public void collect()
175 {
176 try
177 {
178 List<IUnknown> allObjects = new ArrayList<IUnknown>();
179 List<PerformanceData> metricData = perf.query(Arrays.asList(new String[]{"*"}),
180 allObjects);
181 for (PerformanceData md : metricData)
182 {
183 System.out.println("(" + getObjectName(md.object) + ") " +
184 md.name + " " + md.getFormattedSamples());
185 }
186 }
187 catch (Exception e)
188 {
189 e.printStackTrace();
190 }
191 }
192
193 public void run()
194 {
195 // Clean up
196 try
197 {
198 if (perf != null)
199 {
200 perf.cleanup();
201 perf = null;
202 }
203 if (vbox != null)
204 {
205 mgr.logoff(vbox);
206 vbox = null;
207 mgr = null;
208 System.out.println("Logged off.");
209 }
210 }
211 catch (Exception e)
212 {
213 e.printStackTrace();
214 }
215 }
216
217 public static void main(String[] args) throws InterruptedException
218 {
219 metrictest c = new metrictest();
220 // Add a shutdown handle to clean up
221 Runtime.getRuntime().addShutdownHook(new Thread(c));
222 // Start metric collection
223 c.setup();
224 // Obtain and print out stats continuously until ctrl-C is pressed
225 while (true)
226 {
227 Thread.sleep(1000); // Sleep for a second
228 c.collect();
229 }
230 }
231}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette