Mendapatkan Data Performa Statistik

Di WMI, Anda dapat menentukan data performa statistik berdasarkan data dalam kelas performa yang diformat yang berasal dari Win32_PerfFormattedData. Statistik yang tersedia adalah rata-rata, minimum, maksimum, rentang, dan varians, seperti yang didefinisikan dalam Jenis Penghitung Statistik.

Daftar berikut mencakup jenis penghitung statistik khusus:

Contoh berikut menunjukkan cara:

  • Buat file MOF yang menentukan kelas data terhitung.
  • Tulis skrip yang membuat instans kelas, dan secara berkala merefresh data dalam instans dengan nilai statistik yang dihitung ulang.

MOF File

Contoh kode MOF berikut membuat kelas data terhitung baru bernama Win32_PerfFormattedData_AvailableMBytes. Kelas ini berisi data dari properti AvailableMBytes dari Win32_PerfRawData_PerfOS_Memory kelas mentah. Kelas Win32_PerfFormattedData_AvailableBytes menentukan properti Average, Min, Max, Range, dan Variance.

File MOF menggunakan Kualifikasi Properti untuk Kelas Penghitung Kinerja Terformat untuk menentukan sumber data properti dan rumus perhitungan.

  • Properti Rata-rata memperoleh data mentah dari Win32_PerfRawData_PerfOS_Memory. Properti AvailableMBytes .
  • Penghitung kualifikasi untuk properti Rata-rata menentukan sumber data mentah.
  • Kualifikasi CookingType menentukan rumus COOKER_MIN untuk menghitung data.
  • Kualifikasi SampleWindow menentukan berapa banyak sampel yang harus diambil sebelum melakukan penghitungan.
// Store the new Win32_PerfFormattedData_MemoryStatistics
//     class in the Root\Cimv2 namespace
#pragma autorecover
#pragma namespace("\\\\.\\Root\\CimV2")

qualifier vendor:ToInstance;
qualifier guid:ToInstance;
qualifier displayname:ToInstance;
qualifier perfindex:ToInstance;
qualifier helpindex:ToInstance;
qualifier perfdetail:ToInstance;
qualifier countertype:ToInstance;
qualifier perfdefault:ToInstance;
qualifier defaultscale:ToInstance;

qualifier dynamic:ToInstance;
qualifier hiperf:ToInstance;
qualifier AutoCook:ToInstance;
qualifier AutoCook_RawClass:ToInstance;
qualifier CookingType:ToInstance;
qualifier Counter:ToInstance;


// Define the Win32_PerFormattedData_MemoryStatistics
//     class, derived from Win32_PerfFormattedData
[
  dynamic,
  // Name of formatted data provider: "WMIPerfInst" for Vista 
  //   and later
  provider("HiPerfCooker_v1"), 
  // Text that will identify new counter in Perfmon
  displayname("My Calculated Counter"),                            
  // A high performance class     
  Hiperf,
  // Contains calculated data 
  Cooked, 
  // Value must be 1 
  AutoCook(1), 
  // Raw performance class to get data for calculations
  AutoCook_RawClass("Win32_PerfRawData_PerfOS_Memory"),
  // Value must be 1        
  AutoCook_RawDefault(1),
  // Name of raw class property to use for timestamp in formulas 
  PerfSysTimeStamp("Timestamp_PerfTime"), 
  // Name of raw class property to use for frequency in formulas
  PerfSysTimeFreq("Frequency_PerfTime"), 
  // Name of raw class property to use for timestamp in formulas
  Perf100NSTimeStamp("Timestamp_Sys100NS"),
  // Name of raw class property to use for frequency in formulas
  Perf100NSTimeFreq("Frequency_Sys100NS"),
  // Name of raw class property to use for timestamp in formulas
  PerfObjTimeStamp("Timestamp_Object"),
  // Name of raw class property to use for frequency in formulas 
  PerfObjTimeFreq("Frequency_Object"),
  // Only one instance of class allowed in namespace
  singleton                                                   
]

class Win32_PerfFormattedData_MemoryStatistics
          : Win32_PerfFormattedData
{

// Define the properties for the class. 
// All the properties perform different
//     statistical operations on the same
//     property, AvailableMBytes, in the raw class

// Define the Average property,
//     which uses the "COOKER_AVERAGE" counter type and 
//     gets its data from the AvailableMBytes 
//     property in the raw class

    [
     CookingType("COOKER_AVERAGE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Average = 0;

// Define the Min property, which uses
//     the "COOKER_MIN" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_MIN"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Min = 0;

// Define the Max property, which uses
//     the "COOKER_MAX" counter type and 
//     gets its data from the
//     AvailableMBytes property in the raw class

    [
     CookingType("COOKER_MAX"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Max = 0;

// Define the Range property, which uses
//     the "COOKER_RANGE" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_RANGE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Range = 0;

// Define the Variance property, which uses
//     the "COOKER_VARIANCE" counter type and 
//     gets its data from the AvailableMBytes
//     property in the raw class

    [
     CookingType("COOKER_VARIANCE"),
     Counter("AvailableMBytes"),
     SampleWindow(10)
    ]
    uint64 Variance = 0;
};

Skrip

Contoh kode skrip berikut memperoleh statistik tentang memori yang tersedia, dalam megabyte, menggunakan MOF yang dibuat sebelumnya. Skrip menggunakan objek skrip SWbemRefresher untuk membuat penyegaran yang berisi satu instans kelas statistik yang dibuat file MOF, yang Win32_PerfFormattedData_MemoryStatistics. Untuk informasi selengkapnya tentang menggunakan skrip, lihat Merefresh Data WMI dalam Skrip. Jika bekerja di C++, lihat Mengakses Data Performa di C++.

Catatan

SWbemRefreshableItem.Object harus dipanggil setelah mendapatkan item dari panggilan ke SWbemRefresher.Add atau skrip akan gagal. SWbemRefresher.Refresh harus dipanggil sebelum memasukkan perulangan untuk mendapatkan nilai garis besar, atau properti statistik adalah nol (0) pada pass pertama.

 

' Connect to the Root\Cimv2 namespace
strComputer = "."
Set objService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

' Create a refresher
Set Refresher = CreateObject("WbemScripting.SWbemRefresher")
If Err <> 0 Then
WScript.Echo Err
WScript.Quit
End If

' Add the single instance of the statistics
'    class to the refresher
Set obMemoryStatistics = Refresher.Add(objService, _
    "Win32_PerfFormattedData_MemoryStatistics=@").Object

' Refresh once to obtain base values for cooking calculations
Refresher.Refresh

Const REFRESH_INTERVAL = 10

' Refresh every REFRESH_INTERVAL seconds 
For I=1 to 3
  WScript.Sleep REFRESH_INTERVAL * 1000
  Refresher.Refresh

  WScript.Echo "System memory statistics" _
      & "(Available Megabytes) " _
      & "for the past 100 seconds - pass " & i 
  WScript.Echo "Average = " _
     & obMemoryStatistics.Average & VBNewLine & "Max = " _
     & obMemoryStatistics.Max & VBNewLine & "Min = " _
     & obMemoryStatistics.Min & VBNewLine & "Range = " _ 
     & obMemoryStatistics.Range & VBNewLine & "Variance = " _
     & obMemoryStatistics.Variance 
Next

Menjalankan Skrip

Prosedur berikut menjelaskan cara menjalankan contoh.

Untuk menjalankan contoh skrip

  1. Salin kode MOF dan skrip ke file di komputer Anda.
  2. Berikan file MOF ekstensi .mof dan file skrip deskripsi .vbs.
  3. Pada baris perintah, ubah ke direktori tempat file disimpan, dan jalankan Mofcomp pada file MOF. Misalnya, jika Anda memberi nama file CalculatedData.mof, maka perintahnya adalah MofcompCalculatedData.mof
  4. Jalankan skrip.

Memantau Data Performa

Mengakses Kelas Performa WMI yang Telah Diinstal Sebelumnya

Kualifikasi Properti untuk Kelas Penghitung Kinerja Yang Diformat

Jenis Penghitung Statistik