System.Diagnostics.PerformanceData Ruang nama
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Gunakan kelas di namespace layanan ini untuk menyediakan data penghitung. Penghitung digunakan untuk mengekspos metrik performa kepada konsumen seperti Monitor Performa. Namespace tidak berisi kelas untuk mengonsumsi data penghitung. Untuk deskripsi lengkap arsitektur penghitung kinerja, lihat Penghitung Kinerja.
Kelas
| Nama | Deskripsi |
|---|---|
| CounterData |
Berisi data mentah untuk penghitung. |
| CounterSet |
Mendefinisikan satu set penghitung logis. |
| CounterSetInstance |
Membuat instans penghitung logis yang ditentukan di CounterSet kelas . |
| CounterSetInstanceCounterDataSet |
Berisi kumpulan nilai penghitung. |
Enum
| Nama | Deskripsi |
|---|---|
| CounterSetInstanceType |
Menentukan apakah set penghitung memungkinkan beberapa instans seperti proses dan disk fisik, atau satu instans seperti memori. |
| CounterType |
Menentukan kemungkinan jenis penghitung. Setiap penghitung diberi jenis penghitung. Jenis penghitung menentukan bagaimana data penghitung dihitung, dirata-ratakan, dan ditampilkan. |
Contoh
Berikut ini menunjukkan manifes sederhana:
<!-- <?xml version="1.0" encoding="UTF-16"?> -->
<instrumentationManifest xsi:schemaLocation="http://schemas.microsoft.com/win/2004/08/events eventman.xsd"
xmlns:win="http://manifests.microsoft.com/win/2004/08/windows/events"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:trace=http://schemas.microsoft.com/win/2004/08/events/trace>
<instrumentation>
<counters xmlns=http://schemas.microsoft.com/win/2005/12/counters>
<provider
applicationIdentity = "provider1.exe"
providerType = "userMode"
providerGuid = "{51D1685C-35ED-45be-99FE-17261A4F27F3}">
<counterSet guid = "{582803C9-AACD-45e5-8C30-571141A22092}"
uri = "Microsoft.Windows.System.PerfCounters.Typing"
name = "$(string.CounterSet1.Name)"
description = "$(string.CounterSet1.Description)"
instances = "single">
<counter id = "1"
uri = "Microsoft.Windows.System.PerfCounters.Typing.TotalWords"
name = "$(string.CS1.Counter1.Name)"
description = "$(string.CS1.Counter1.Description)"
type = "perf_counter_rawcount"
detailLevel = "standard"/>
<counter id = "2"
uri = "Microsoft.Windows.System.PerfCounters.Typing.WordsInInterval"
name = "$(string.CS1.Counter2.Name)"
description = "$(string.CS1.Counter2.Description)"
type = "perf_counter_delta"
detailLevel = "standard"/>
<counter id = "3"
uri = "Microsoft.Windows.System.PerfCounters.Typing.LetterAPressed"
name = "$(string.CS1.Counter3.Name)"
description = "$(string.CS1.Counter3.Description)"
type = "perf_counter_rawcount"
detailLevel = "standard"/>
<counter id = "4"
uri = "Microsoft.Windows.System.PerfCounters.Typing.WordsContainingLetterA"
name = "$(string.CS1.Counter4.Name)"
description = "$(string.CS1.Counter4.Description)"
type = "perf_counter_rawcount"
detailLevel = "standard"/>
<counter id = "5"
uri = "Microsoft.Windows.System.PerfCounters.Typing.PercentOfWordsContainingLetterA"
name = "$(string.CS1.Counter5.Name)"
description = "$(string.CS1.Counter5.Description)"
type = "perf_sample_fraction"
baseID = "6"
detailLevel = "standard">
<counterAttributes>
<counterAttribute name = "displayAsReal" />
</counterAttributes>
</counter>
<counter id = "6"
uri = "Microsoft.Windows.System.PerfCounters.Typing.PercentBase"
type = "perf_sample_base"
detailLevel = "standard">
<counterAttributes>
<counterAttribute name = "noDisplay" />
</counterAttributes>
</counter>
</counterSet>
</provider>
</counters>
</instrumentation>
<localization>
<resources culture="en-US">
<stringTable>
<string id="CounterSet1.Name" value="Typing"/>
<string id="CounterSet1.Description" value="Captures simple typing metrics."/>
<string id="CS1.Counter1.Name" value="Total Words Typed"/>
<string id="CS1.Counter1.Description" value="The total number of words typed."/>
<string id="CS1.Counter2.Name" value="Words Typed In Interval"/>
<string id="CS1.Counter2.Description" value="The total number of words typed in the interval."/>
<string id="CS1.Counter3.Name" value="Letter A Pressed"/>
<string id="CS1.Counter3.Description" value="The number of times that the letter A is pressed."/>
<string id="CS1.Counter4.Name" value="Words Containing A"/>
<string id="CS1.Counter4.Description" value="The number of words that contain the letter A."/>
<string id="CS1.Counter5.Name" value="Percent of Words Containing A"/>
<string id="CS1.Counter5.Description" value="The percent of words that contain the letter A in the last interval."/>
</stringTable>
</resources>
</localization>
</instrumentationManifest>
Berikut ini menunjukkan implementasi penyedia sederhana untuk manifes:
using System.Diagnostics.PerformanceData;
private static Guid providerId = new Guid("{51D1685C-35ED-45be-99FE-17261A4F27F3}");
private static Guid typingCounterSetId = new Guid("{582803C9-AACD-45e5-8C30-571141A22092}");
private static CounterSet typingCounterSet; // Defines the counter set
private static CounterSetInstance typingCsInstance; // Instance of the counter set
private static int numberOfLetterAInWord = 0;
. . .
// Create the 'Typing' counter set.
typingCounterSet = new CounterSet(providerId, typingCounterSetId, CounterSetInstanceType.Single);
// Add the counters to the counter set definition.
typingCounterSet.AddCounter(1, CounterType.RawData32, "Total Word Count");
typingCounterSet.AddCounter(2, CounterType.Delta32, "Words Typed In Interval");
typingCounterSet.AddCounter(3, CounterType.RawData32, "A Key Pressed");
typingCounterSet.AddCounter(4, CounterType.RawData32, "Words Containing A");
typingCounterSet.AddCounter(5, CounterType.SampleFraction, "Percent of Words Containing A");
typingCounterSet.AddCounter(6, CounterType.SampleBase, "Percent Base");
// Create an instance of the counter set (contains the counter data).
typingCsInstance = typingCounterSet.CreateCounterSetInstance("Typing Instance");
typingCsInstance.Counters[1].Value = 0;
typingCsInstance.Counters[2].Value = 0;
typingCsInstance.Counters[3].Value = 0;
typingCsInstance.Counters[4].Value = 0;
typingCsInstance.Counters[5].Value = 0;
typingCsInstance.Counters[6].Value = 0;
. . .
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
typingCounterSet.Dispose();
}
// Simple effort to capture letter A key press and words typed.
private void textInput_KeyDown(object sender, KeyEventArgs e)
{
Keys keyData = e.KeyData;
switch (e.KeyData)
{
case Keys.A :
// In the .NET 3.5 Framework, you had to use the
// Value property to set and increment the counter
// value. Beginning with the .NET 4.0 Framework,
// the Value property is safe to use in a multi-
// threaded application.
typingCsInstance.Counters["A Key Pressed"].Value++;
numberOfLetterAInWord++;
break;
case Keys.Enter:
case Keys.Space:
case Keys.Tab:
if (numberOfLetterAInWord > 0)
{
// Beginning with the .NET 4.0 Framework, you
// can use the Increment method to increment
// the counter value by 1. The Increment method
// is safe to use in a multi-threaded
// application.
typingCsInstance.Counters["Words Containing A"].Increment();
typingCsInstance.Counters["Percent of Words Containing A"].Increment();
numberOfLetterAInWord = 0;
}
typingCsInstance.Counters["Percent Base"].Increment();
typingCsInstance.Counters["Total Word Count"].Increment();
typingCsInstance.Counters["Words Typed In Interval"].Increment();
break;
}
}
Keterangan
Kelas di namespace layanan ini mendukung arsitektur baru (versi 2.0) untuk penghitung kinerja yang diperkenalkan di Windows Vista. Dalam arsitektur baru, penyedia tidak lagi menanggapi permintaan konsumen secara langsung, tetapi sebaliknya hanya mempertahankan data penghitung. Sistem menyuntikkan utas ke dalam proses penyedia ketika penyedia membuat instans set penghitung; utas bertanggung jawab untuk menangani permintaan konsumen.
Langkah-langkah berikut menunjukkan proses untuk menulis penyedia penghitung.
Penghitung yang disediakan penyedia ditentukan dalam manifes berbasis XML. Penghitung secara logis dikelompokkan ke dalam set penghitung. Penghitung dalam set penghitung diidentifikasi oleh pengidentifikasi numerik yang unik dalam set penghitung. Penyedia dapat menentukan satu atau beberapa set penghitung. Set penghitung diidentifikasi oleh Guid yang unik untuk penyedia. Perhatikan bahwa jika Anda menggunakan kelas-kelas ini untuk menulis penyedia Anda:
Atribut panggilan balik elemen penyedia diabaikan.
Nilai referensi untuk atribut nama elemen counterAttribute diabaikan.
Untuk detail tentang menulis manifes, lihat Skema Penghitung Kinerja.
Setelah menulis manifes Anda, gunakan alat CTRPP untuk mengkompilasi manifes (ctrpp provider.man). Alat ini menghasilkan empat file: .h, .c, .rc, dan *_r.h. Anda dapat mengabaikan file .h dan .c. File .rc berisi string yang dilokalkan yang ditentukan dalam manifes Anda. Anda menggunakan file .rc dan *_r.h untuk membuat file sumber daya yang dikompilasi (.res) yang Anda sertakan dalam proyek Anda. Panggilan berikut menunjukkan cara mengkompilasi file sumber daya:
rc /r /i "c:\Program Files\Microsoft SDKs\Windows\v6.0\Include" provider.rcJika Anda mendapatkan kesalahan yang mereferensikan sal.h, salin file sal.h dari Microsoft Visual Studio Anda, Visual C menyertakan direktori ke direktori yang Anda tentukan untuk sakelar /i.
Tambahkan jalur ke file sumber daya yang dikompilasi (.res) ke halaman properti Aplikasi proyek Anda.
Tulis penyedia Anda. Langkah-langkah berikut menunjukkan panggilan yang dilakukan oleh penyedia:
CounterSet.CounterSet Panggil konstruktor untuk menentukan set penghitung. Panggil metode ini untuk setiap set penghitung yang ditentukan dalam manifes.
Untuk setiap set penghitung, panggil salah CounterSet.AddCounter satu metode untuk menambahkan penghitung ke set. Panggil metode ini untuk setiap penghitung yang ditentukan dalam set penghitung.
CounterSet.CreateCounterSetInstance Panggil metode untuk membuat instans set penghitung (instans berisi data penghitung). Untuk set penghitung instans tunggal, panggil metode ini satu kali. Untuk beberapa set penghitung instans, panggil metode ini untuk setiap instans yang perlu Anda berikan data penghitung (gunakan nama unik untuk setiap instans).
CounterSetInstance.Counters Gunakan properti untuk mengakses dan mengatur data penghitung untuk penghitung.
Setelah Anda menyelesaikan penyedia Anda, gunakan alat LodCtr untuk mendaftarkan penghitung di komputer. Contohnya,
lodctr /m:provider.manContoh mengasumsikan manifes dan file yang dapat dieksekusi ada di direktori saat ini.
Anda dapat menggunakan kelas di namespace layanan ini pada komputer yang menjalankan Windows Vista dan sistem operasi yang lebih baru.