VirtualBox

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

Last change on this file since 21898 was 21898, checked in by vboxsync, 15 years ago

Java WS glue: better struct treatment, allow using struct fields in natural way

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

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