The following code fragment prints out the returned performance data. This example uses CSV formatted data. The code fragment performs these tasks:
- Loop through the list of PerfEntityMetricBase objects returned by the QueryPerf method (retrievedStats).
- Cast the PerfEntityMetricBase object to a PerfEntityMetricCSV object to handle the CSV output specified in the PerfQuerySpec.
- Retrieve the sampled values.
- Retrieve the interval information (csvTimeInfoAboutStats). The sampleInfoCSV string (PerfEntityMetricCSV.sampleInfoCSV) is PerfSampleInfo data formatted as interval,time pairs separated by commas – interval-1,time-1,interval-2,time-2. The list of pairs embedded in the string corresponds to the list of sampled values (PerfEntityMetricCSV.value[]).
- Print the time and interval information.
- Loop through the sampled values (metricsValues).
- Use the counter metadata to print out identifying information about the counter along with the returned sampled value for the counter.
- Use the countersInfoMap to translate the counter ID returned in the PerfMetricSeriesCSV object into the corresponding PerfCounterInfo object.
/*
* Cycle through the PerfEntityMetricBase objects. Each object contains
* a set of statistics for a single ManagedEntity.
*/
for(PerfEntityMetricBase singleEntityPerfStats : retrievedStats) {
/*
* Cast the base type (PerfEntityMetricBase) to the csv-specific sub-class.
*/
PerfEntityMetricCSV entityStatsCsv = (PerfEntityMetricCSV)singleEntityPerfStats;
/* Retrieve the list of sampled values. */
List<PerfMetricSeriesCSV> metricsValues = entityStatsCsv.getValue();
if(metricsValues.isEmpty()) {
System.out.println("No stats retrieved. " +
"Check whether the virtual machine is powered on.");
throw new Exception();
}
/*
* Retrieve time interval information (PerfEntityMetricCSV.sampleInfoCSV).
*/
String csvTimeInfoAboutStats = entityStatsCsv.getSampleInfoCSV();
/* Print the time and interval information. */
System.out.println("Collection: interval (seconds),time (yyyy-mm-ddThh:mm:ssZ)");
System.out.println(csvTimeInfoAboutStats);
/*
* Cycle through the PerfMetricSeriesCSV objects. Each object contains
* statistics for a single counter on the ManagedEntity.
*/
for(PerfMetricSeriesCSV csv : metricsValues) {
/*
* Use the counterId to obtain the associated PerfCounterInfo object
*/
PerfCounterInfo pci = countersInfoMap.get(csv.getId().getCounterId());
/* Print out the metadata for the counter. */
System.out.println("----------------------------------------");
System.out.println(pci.getGroupInfo().getKey() + "."
+ pci.getNameInfo().getKey() + "."
+ pci.getRollupType() + " - "
+ pci.getUnitInfo().getKey());
System.out.println("Instance: "+csv.getId().getInstance());
System.out.println("Values: " + csv.getValue());
}
}