VirtualBox

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

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

fixed webservice copyright

  • 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.virtualbox.org. 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.*;
22import java.util.*;
23import javax.xml.ws.Holder;
24import org.virtualbox.*;
25
26class PerformanceData
27{
28 public String name;
29 public IUnknown object;
30 public String unit;
31 public Long scale;
32 public Long sequenceNumber;
33 public List<Long> samples;
34
35 public String getFormattedSamples()
36 {
37 String out = "[";
38 String separator = "";
39
40 if (scale != 1)
41 {
42 for (Long sample : samples)
43 {
44 out += separator + (sample.doubleValue() / scale) + " " + unit;
45 separator = ", ";
46 }
47 }
48 else
49 {
50 for (Long sample : samples)
51 {
52 out += separator + sample.toString() + " " + unit;
53 separator = ", ";
54 }
55 }
56 out += "]";
57 return out;
58 }
59}
60
61class PerformanceCollector
62{
63 private IVirtualBox _vbox;
64 private IPerformanceCollector _collector;
65
66 public PerformanceCollector(IVirtualBox vbox)
67 {
68 _vbox = vbox;
69 _collector = vbox.getPerformanceCollector();
70 }
71
72 public void cleanup()
73 {
74 _collector.releaseRemote();
75 }
76
77 public List<IPerformanceMetric> setup(List<String> metricNames, List<IUnknown> objects, Long period, Long samples)
78 {
79 return _collector.setupMetrics(metricNames, objects, period, samples);
80 }
81
82 public List<IPerformanceMetric> enable(List<String> metricNames, List<IUnknown> objects)
83 {
84 return _collector.enableMetrics(metricNames, objects);
85 }
86
87 public List<IPerformanceMetric> disable(List<String> metricNames, List<IUnknown> objects)
88 {
89 return _collector.disableMetrics(metricNames, objects);
90 }
91
92 public List<PerformanceData> query(List<String> filterMetrics, List<IUnknown> filterObjects)
93 {
94 Holder<List<String>> names = new Holder<List<String>>();
95 Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>();
96 Holder<List<String>> units = new Holder<List<String>>();
97 Holder<List<Long>> scales = new Holder<List<Long>>();
98 Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
99 Holder<List<Long>> indices = new Holder<List<Long>>();
100 Holder<List<Long>> lengths = new Holder<List<Long>>();
101 List<Integer> values =
102 _collector.queryMetricsData(filterMetrics, filterObjects,
103 names, objects, units, scales, sequenceNumbers, indices, lengths);
104 List<PerformanceData> data = new ArrayList<PerformanceData>(names.value.size());
105 for (int i = 0; i < names.value.size(); i++)
106 {
107 PerformanceData singleMetricData = new PerformanceData();
108 singleMetricData.name = names.value.get(i);
109 singleMetricData.object = objects.value.get(i);
110 singleMetricData.unit = units.value.get(i);
111 singleMetricData.scale = scales.value.get(i);
112 singleMetricData.sequenceNumber = sequenceNumbers.value.get(i);
113 List<Long> samples = new ArrayList<Long>(lengths.value.get(i).intValue());
114 for (int j = 0; j < lengths.value.get(i); j++)
115 {
116 samples.add(values.get(indices.value.get(i).intValue() + j).longValue());
117 }
118 singleMetricData.samples = samples;
119 data.add(singleMetricData);
120 }
121
122 return data;
123 }
124}
125
126public class metrictest implements Runnable
127{
128 IVirtualBox vbox;
129 IWebsessionManager mgr;
130 PerformanceCollector perf;
131
132 public metrictest()
133 {
134 vbox = VirtualBox.connect("http://localhost:18083/");
135 mgr = new IWebsessionManager(vbox.getRemoteWSPort());
136 System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
137 perf = new PerformanceCollector(vbox);
138 }
139
140 private String getObjectName(IUnknown object)
141 {
142 try
143 {
144 String machineName = object.getRemoteWSPort().iMachineGetName(object.getRef());
145 return machineName;
146 } catch (Exception e)
147 {
148 }
149 return new String("host");
150 }
151
152 public void setup()
153 {
154 perf.setup(Arrays.asList(new String[]{"*"}),
155 new ArrayList<IUnknown>(),
156 new Long(1), new Long(5));
157 }
158
159 public void collect()
160 {
161 try
162 {
163 List<IUnknown> allObjects = new ArrayList<IUnknown>();
164 List<PerformanceData> metricData = perf.query(Arrays.asList(new String[]{"*"}),
165 allObjects);
166 for (PerformanceData md : metricData)
167 {
168 System.out.println("(" + getObjectName(md.object) + ") " +
169 md.name + " " + md.getFormattedSamples());
170 }
171 }
172 catch (Exception e)
173 {
174 e.printStackTrace();
175 }
176 }
177
178 public void run()
179 {
180 // Clean up
181 try
182 {
183 if (perf != null)
184 {
185 perf.cleanup();
186 perf = null;
187 }
188 if (vbox != null)
189 {
190 mgr.logoff(vbox);
191 vbox = null;
192 mgr = null;
193 System.out.println("Logged off.");
194 }
195 }
196 catch (Exception e)
197 {
198 e.printStackTrace();
199 }
200 }
201
202 public static void main(String[] args) throws InterruptedException
203 {
204 metrictest c = new metrictest();
205 // Add a shutdown handle to clean up
206 Runtime.getRuntime().addShutdownHook(new Thread(c));
207 // Start metric collection
208 c.setup();
209 // Obtain and print out stats continuosly until ctrl-C is pressed
210 while (true)
211 {
212 Thread.sleep(1000); // Sleep for a second
213 c.collect();
214 }
215 }
216}
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