'Method queries the real-time stat values for a VM.
:param vm_mobj: managed object of the VM for which stats are needed
:param counter_id: id of the perf counter in VC
:param device_name: name of the device for which stats are to be
queried. For aggregate counters pass empty string ("").
For device counters pass "*", if stats are required over all
devices.
:param duration: in seconds from current time,
over which the stat value was applicable
:return: a map containing the stat values keyed by the device ID/name'

def _query_vm_perf_stats(self, vm_mobj, counter_id, device_name, duration):

    session = self._api_session
    client_factory = session.vim.client.factory
    metric_id = client_factory.create('ns0:PerfMetricId')
    metric_id.counterId = counter_id
    metric_id.instance = device_name
    query_spec = client_factory.create('ns0:PerfQuerySpec')
    query_spec.entity = vm_mobj
    query_spec.metricId = [metric_id]
    query_spec.intervalId = VC_REAL_TIME_SAMPLING_INTERVAL
    samples_cnt = (int((duration / VC_REAL_TIME_SAMPLING_INTERVAL)) if (duration and (duration >= VC_REAL_TIME_SAMPLING_INTERVAL)) else 1)
    query_spec.maxSample = samples_cnt
    perf_manager = session.vim.service_content.perfManager
    perf_stats = session.invoke_api(session.vim, 'QueryPerf', perf_manager, querySpec=[query_spec])
    stat_values = {}
    if perf_stats:
        entity_metric = perf_stats[0]
        sample_infos = entity_metric.sampleInfo
        if (len(sample_infos) > 0):
            for metric_series in entity_metric.value:
                filtered = [i for i in metric_series.value if (i != (-1))]
                stat_value = (float(sum(filtered)) / len(filtered))
                device_id = metric_series.id.instance
                stat_values[device_id] = stat_value
    return stat_values
