VirtualBox

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

Last change on this file since 18609 was 18609, checked in by vboxsync, 16 years ago

SDK: versioning in samples, cleanup

  • Property svn:eol-style set to native
File size: 6.7 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 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*;
22import org.virtualbox{VBOX_API_SUFFIX}.*;
23
24import java.util.*;
25import javax.xml.ws.Holder;
26
27class PerformanceData
28{
29 public String name;
30 public IUnknown object;
31 public String unit;
32 public Long scale;
33 public Long sequenceNumber;
34 public List<Long> samples;
35
36 public String getFormattedSamples()
37 {
38 String out = "[";
39 String separator = "";
40
41 if (scale != 1)
42 {
43 for (Long sample : samples)
44 {
45 out += separator + (sample.doubleValue() / scale) + " " + unit;
46 separator = ", ";
47 }
48 }
49 else
50 {
51 for (Long sample : samples)
52 {
53 out += separator + sample.toString() + " " + unit;
54 separator = ", ";
55 }
56 }
57 out += "]";
58 return out;
59 }
60}
61
62class PerformanceCollector
63{
64 private IVirtualBox _vbox;
65 private IPerformanceCollector _collector;
66
67 public PerformanceCollector(IVirtualBox vbox)
68 {
69 _vbox = vbox;
70 _collector = vbox.getPerformanceCollector();
71 }
72
73 public void cleanup()
74 {
75 _collector.releaseRemote();
76 }
77
78 public List<IPerformanceMetric> setup(List<String> metricNames, List<IUnknown> objects, Long period, Long samples)
79 {
80 return _collector.setupMetrics(metricNames, objects, period, samples);
81 }
82
83 public List<IPerformanceMetric> enable(List<String> metricNames, List<IUnknown> objects)
84 {
85 return _collector.enableMetrics(metricNames, objects);
86 }
87
88 public List<IPerformanceMetric> disable(List<String> metricNames, List<IUnknown> objects)
89 {
90 return _collector.disableMetrics(metricNames, objects);
91 }
92
93 public List<PerformanceData> query(List<String> filterMetrics, List<IUnknown> filterObjects)
94 {
95 Holder<List<String>> names = new Holder<List<String>>();
96 Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>();
97 Holder<List<String>> units = new Holder<List<String>>();
98 Holder<List<Long>> scales = new Holder<List<Long>>();
99 Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
100 Holder<List<Long>> indices = new Holder<List<Long>>();
101 Holder<List<Long>> lengths = new Holder<List<Long>>();
102 List<Integer> values =
103 _collector.queryMetricsData(filterMetrics, filterObjects,
104 names, objects, units, scales, sequenceNumbers, indices, lengths);
105 List<PerformanceData> data = new ArrayList<PerformanceData>(names.value.size());
106 for (int i = 0; i < names.value.size(); i++)
107 {
108 PerformanceData singleMetricData = new PerformanceData();
109 singleMetricData.name = names.value.get(i);
110 singleMetricData.object = objects.value.get(i);
111 singleMetricData.unit = units.value.get(i);
112 singleMetricData.scale = scales.value.get(i);
113 singleMetricData.sequenceNumber = sequenceNumbers.value.get(i);
114 List<Long> samples = new ArrayList<Long>(lengths.value.get(i).intValue());
115 for (int j = 0; j < lengths.value.get(i); j++)
116 {
117 samples.add(values.get(indices.value.get(i).intValue() + j).longValue());
118 }
119 singleMetricData.samples = samples;
120 data.add(singleMetricData);
121 }
122
123 return data;
124 }
125}
126
127public class metrictest implements Runnable
128{
129 IVirtualBox vbox;
130 IWebsessionManager mgr;
131 PerformanceCollector perf;
132
133 public metrictest()
134 {
135 vbox = VirtualBox.connect("http://localhost:18083/");
136 mgr = new IWebsessionManager(vbox.getRemoteWSPort());
137 System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
138 perf = new PerformanceCollector(vbox);
139 }
140
141 private String getObjectName(IUnknown object)
142 {
143 try
144 {
145 String machineName = object.getRemoteWSPort().iMachineGetName(object.getRef());
146 return machineName;
147 } catch (Exception e)
148 {
149 }
150 return new String("host");
151 }
152
153 public void setup()
154 {
155 perf.setup(Arrays.asList(new String[]{"*"}),
156 new ArrayList<IUnknown>(),
157 new Long(1), new Long(5));
158 }
159
160 public void collect()
161 {
162 try
163 {
164 List<IUnknown> allObjects = new ArrayList<IUnknown>();
165 List<PerformanceData> metricData = perf.query(Arrays.asList(new String[]{"*"}),
166 allObjects);
167 for (PerformanceData md : metricData)
168 {
169 System.out.println("(" + getObjectName(md.object) + ") " +
170 md.name + " " + md.getFormattedSamples());
171 }
172 }
173 catch (Exception e)
174 {
175 e.printStackTrace();
176 }
177 }
178
179 public void run()
180 {
181 // Clean up
182 try
183 {
184 if (perf != null)
185 {
186 perf.cleanup();
187 perf = null;
188 }
189 if (vbox != null)
190 {
191 mgr.logoff(vbox);
192 vbox = null;
193 mgr = null;
194 System.out.println("Logged off.");
195 }
196 }
197 catch (Exception e)
198 {
199 e.printStackTrace();
200 }
201 }
202
203 public static void main(String[] args) throws InterruptedException
204 {
205 metrictest c = new metrictest();
206 // Add a shutdown handle to clean up
207 Runtime.getRuntime().addShutdownHook(new Thread(c));
208 // Start metric collection
209 c.setup();
210 // Obtain and print out stats continuosly until ctrl-C is pressed
211 while (true)
212 {
213 Thread.sleep(1000); // Sleep for a second
214 c.collect();
215 }
216 }
217}
Note: See TracBrowser for help on using the repository browser.

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