diff --git a/Makefile b/Makefile index 24ab5aa8..eefc0fd2 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ windows_exporter.exe: **/*.go test: go test -v ./... +bench: + go test -v -bench='benchmark(cpu|logicaldisk|logon|memory|net|process|service|system|tcp|time)collector' ./... + lint: golangci-lint -c .golangci.yaml run diff --git a/collector/ad_test.go b/collector/ad_test.go new file mode 100644 index 00000000..6bb13e48 --- /dev/null +++ b/collector/ad_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkADCollector(b *testing.B) { + benchmarkCollector(b, "ad", NewADCollector) +} diff --git a/collector/adfs_test.go b/collector/adfs_test.go new file mode 100644 index 00000000..35db4d74 --- /dev/null +++ b/collector/adfs_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkADFSCollector(b *testing.B) { + benchmarkCollector(b, "adfs", newADFSCollector) +} diff --git a/collector/collector_test.go b/collector/collector_test.go index bafeb8f9..cfbc284d 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -3,6 +3,8 @@ package collector import ( "reflect" "testing" + + "github.com/prometheus/client_golang/prometheus" ) func TestExpandChildCollectors(t *testing.T) { @@ -32,3 +34,27 @@ func TestExpandChildCollectors(t *testing.T) { }) } } + +func benchmarkCollector(b *testing.B, name string, collectFunc func() (Collector, error)) { + // Create perflib scrape context. Some perflib collectors required a correct context, + // or will fail during benchmark. + scrapeContext, err := PrepareScrapeContext([]string{name}) + if err != nil { + b.Error(err) + } + c, err := collectFunc() + if err != nil { + b.Error(err) + } + + metrics := make(chan prometheus.Metric) + go func() { + for { + <-metrics + } + }() + + for i := 0; i < b.N; i++ { + c.Collect(scrapeContext, metrics) + } +} diff --git a/collector/container_test.go b/collector/container_test.go new file mode 100644 index 00000000..4da471b8 --- /dev/null +++ b/collector/container_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkContainerCollector(b *testing.B) { + benchmarkCollector(b, "container", NewContainerMetricsCollector) +} diff --git a/collector/cpu_test.go b/collector/cpu_test.go new file mode 100644 index 00000000..4788e598 --- /dev/null +++ b/collector/cpu_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkCPUCollector(b *testing.B) { + benchmarkCollector(b, "cpu", newCPUCollector) +} diff --git a/collector/cs_test.go b/collector/cs_test.go index a4b3c9f1..b1f3f073 100644 --- a/collector/cs_test.go +++ b/collector/cs_test.go @@ -2,22 +2,8 @@ package collector import ( "testing" - - "github.com/prometheus/client_golang/prometheus" ) -func BenchmarkCsCollect(b *testing.B) { - c, err := NewCSCollector() - if err != nil { - b.Error(err) - } - metrics := make(chan prometheus.Metric) - go func() { - for { - <-metrics - } - }() - for i := 0; i < b.N; i++ { - c.Collect(&ScrapeContext{}, metrics) - } +func BenchmarkCsCollector(b *testing.B) { + benchmarkCollector(b, "cs", NewCSCollector) } diff --git a/collector/dfsr_test.go b/collector/dfsr_test.go new file mode 100644 index 00000000..2cc338f9 --- /dev/null +++ b/collector/dfsr_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkDFSRCollector(b *testing.B) { + benchmarkCollector(b, "dfsr", NewDFSRCollector) +} diff --git a/collector/dhcp_test.go b/collector/dhcp_test.go new file mode 100644 index 00000000..98956d93 --- /dev/null +++ b/collector/dhcp_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkDHCPCollector(b *testing.B) { + benchmarkCollector(b, "dhcp", NewDhcpCollector) +} diff --git a/collector/dns_test.go b/collector/dns_test.go new file mode 100644 index 00000000..efb778c4 --- /dev/null +++ b/collector/dns_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkDNSCollector(b *testing.B) { + benchmarkCollector(b, "dns", NewDNSCollector) +} diff --git a/collector/exchange_test.go b/collector/exchange_test.go new file mode 100644 index 00000000..465f245a --- /dev/null +++ b/collector/exchange_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkExchangeCollector(b *testing.B) { + benchmarkCollector(b, "exchange", newExchangeCollector) +} diff --git a/collector/fsrmquota_test.go b/collector/fsrmquota_test.go new file mode 100644 index 00000000..03f42cee --- /dev/null +++ b/collector/fsrmquota_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkFsrmQuotaCollector(b *testing.B) { + benchmarkCollector(b, "fsrmquota", newFSRMQuotaCollector) +} diff --git a/collector/hyperv_test.go b/collector/hyperv_test.go new file mode 100644 index 00000000..c8a9dc59 --- /dev/null +++ b/collector/hyperv_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkHypervCollector(b *testing.B) { + benchmarkCollector(b, "hyperv", NewHyperVCollector) +} diff --git a/collector/iis_test.go b/collector/iis_test.go new file mode 100644 index 00000000..746b27be --- /dev/null +++ b/collector/iis_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkIISCollector(b *testing.B) { + benchmarkCollector(b, "iis", NewIISCollector) +} diff --git a/collector/logical_disk_test.go b/collector/logical_disk_test.go new file mode 100644 index 00000000..96c50e22 --- /dev/null +++ b/collector/logical_disk_test.go @@ -0,0 +1,13 @@ +package collector + +import ( + "testing" +) + +func BenchmarkLogicalDiskCollector(b *testing.B) { + // Whitelist is not set in testing context (kingpin flags not parsed), causing the collector to skip all disks. + localVolumeWhitelist := ".+" + volumeWhitelist = &localVolumeWhitelist + + benchmarkCollector(b, "logical_disk", NewLogicalDiskCollector) +} diff --git a/collector/logon_test.go b/collector/logon_test.go new file mode 100644 index 00000000..42f452aa --- /dev/null +++ b/collector/logon_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkLogonCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewLogonCollector) +} diff --git a/collector/memory_test.go b/collector/memory_test.go new file mode 100644 index 00000000..76cb8765 --- /dev/null +++ b/collector/memory_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkMemoryCollector(b *testing.B) { + benchmarkCollector(b, "memory", NewMemoryCollector) +} diff --git a/collector/msmq_test.go b/collector/msmq_test.go new file mode 100644 index 00000000..4c3bccad --- /dev/null +++ b/collector/msmq_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkMsmqCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewMSMQCollector) +} diff --git a/collector/mssql_test.go b/collector/mssql_test.go new file mode 100644 index 00000000..4929bf23 --- /dev/null +++ b/collector/mssql_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkMSSQLCollector(b *testing.B) { + benchmarkCollector(b, "mssql", NewMSSQLCollector) +} diff --git a/collector/net_test.go b/collector/net_test.go index c4757ce2..aeb3bfe3 100644 --- a/collector/net_test.go +++ b/collector/net_test.go @@ -2,7 +2,9 @@ package collector -import "testing" +import ( + "testing" +) func TestNetworkToInstanceName(t *testing.T) { data := map[string]string{ @@ -15,3 +17,10 @@ func TestNetworkToInstanceName(t *testing.T) { } } } + +func BenchmarkNetCollector(b *testing.B) { + // Whitelist is not set in testing context (kingpin flags not parsed), causing the collector to skip all interfaces. + localNicWhitelist := ".+" + nicWhitelist = &localNicWhitelist + benchmarkCollector(b, "net", NewNetworkCollector) +} diff --git a/collector/netframework_clrexceptions_test.go b/collector/netframework_clrexceptions_test.go new file mode 100644 index 00000000..5174b255 --- /dev/null +++ b/collector/netframework_clrexceptions_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNetFrameworkNETCLRExceptionsCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRExceptionsCollector) +} diff --git a/collector/netframework_clrinterop_test.go b/collector/netframework_clrinterop_test.go new file mode 100644 index 00000000..73db090c --- /dev/null +++ b/collector/netframework_clrinterop_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNETFrameworkNETCLRInteropCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRInteropCollector) +} diff --git a/collector/netframework_clrjit_test.go b/collector/netframework_clrjit_test.go new file mode 100644 index 00000000..44daa0fe --- /dev/null +++ b/collector/netframework_clrjit_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNETFrameworkNETCLRJitCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRJitCollector) +} diff --git a/collector/netframework_clrloading_test.go b/collector/netframework_clrloading_test.go new file mode 100644 index 00000000..20216261 --- /dev/null +++ b/collector/netframework_clrloading_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNETFrameworkNETCLRLoadingCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRLoadingCollector) +} diff --git a/collector/netframework_clrlocksandthreads_test.go b/collector/netframework_clrlocksandthreads_test.go new file mode 100644 index 00000000..164739c1 --- /dev/null +++ b/collector/netframework_clrlocksandthreads_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNETFrameworkNETCLRLocksAndThreadsCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRLocksAndThreadsCollector) +} diff --git a/collector/netframework_clrmemory_test.go b/collector/netframework_clrmemory_test.go new file mode 100644 index 00000000..cc947a43 --- /dev/null +++ b/collector/netframework_clrmemory_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNETFrameworkNETCLRMemoryCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRMemoryCollector) +} diff --git a/collector/netframework_clrremoting_test.go b/collector/netframework_clrremoting_test.go new file mode 100644 index 00000000..556d60ae --- /dev/null +++ b/collector/netframework_clrremoting_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNETFrameworkNETCLRRemotingCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRRemotingCollector) +} diff --git a/collector/netframework_clrsecurity_test.go b/collector/netframework_clrsecurity_test.go new file mode 100644 index 00000000..93c4d416 --- /dev/null +++ b/collector/netframework_clrsecurity_test.go @@ -0,0 +1,10 @@ +package collector + +import ( + "testing" +) + +func BenchmarkNETFrameworkNETCLRSecurityCollector(b *testing.B) { + // No context name required as collector source is WMI + benchmarkCollector(b, "", NewNETFramework_NETCLRSecurityCollector) +} diff --git a/collector/os_test.go b/collector/os_test.go index 1b6b7d9d..5e98f42d 100644 --- a/collector/os_test.go +++ b/collector/os_test.go @@ -2,23 +2,8 @@ package collector import ( "testing" - - "github.com/prometheus/client_golang/prometheus" ) -func BenchmarkOsCollect(b *testing.B) { - o, err := NewOSCollector() - if err != nil { - b.Error(err) - } - metrics := make(chan prometheus.Metric) - go func() { - for { - <-metrics - } - }() - s, err := PrepareScrapeContext([]string{"os"}) - for i := 0; i < b.N; i++ { - o.Collect(s, metrics) - } +func BenchmarkOSCollector(b *testing.B) { + benchmarkCollector(b, "os", NewOSCollector) } diff --git a/collector/process_test.go b/collector/process_test.go new file mode 100644 index 00000000..6537c367 --- /dev/null +++ b/collector/process_test.go @@ -0,0 +1,14 @@ +package collector + +import ( + "testing" +) + +func BenchmarkProcessCollector(b *testing.B) { + // Whitelist is not set in testing context (kingpin flags not parsed), causing the collector to skip all processes. + localProcessWhitelist := ".+" + processWhitelist = &localProcessWhitelist + + // No context name required as collector source is WMI + benchmarkCollector(b, "", newProcessCollector) +} diff --git a/collector/remote_fx_test.go b/collector/remote_fx_test.go new file mode 100644 index 00000000..0fcc8625 --- /dev/null +++ b/collector/remote_fx_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkRemoteFXCollector(b *testing.B) { + benchmarkCollector(b, "remote_fx", NewRemoteFx) +} diff --git a/collector/service_test.go b/collector/service_test.go new file mode 100644 index 00000000..be2e7d4e --- /dev/null +++ b/collector/service_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkServiceCollector(b *testing.B) { + benchmarkCollector(b, "service", NewserviceCollector) +} diff --git a/collector/smtp_test.go b/collector/smtp_test.go new file mode 100644 index 00000000..8b130576 --- /dev/null +++ b/collector/smtp_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkSmtpCollector(b *testing.B) { + benchmarkCollector(b, "smtp", NewSMTPCollector) +} diff --git a/collector/system_test.go b/collector/system_test.go new file mode 100644 index 00000000..ee7ec9dd --- /dev/null +++ b/collector/system_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkSystemCollector(b *testing.B) { + benchmarkCollector(b, "system", NewSystemCollector) +} diff --git a/collector/tcp_test.go b/collector/tcp_test.go new file mode 100644 index 00000000..92468df4 --- /dev/null +++ b/collector/tcp_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkTCPCollector(b *testing.B) { + benchmarkCollector(b, "tcp", NewTCPCollector) +} diff --git a/collector/terminal_services_test.go b/collector/terminal_services_test.go new file mode 100644 index 00000000..b443633c --- /dev/null +++ b/collector/terminal_services_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkTerminalServicesCollector(b *testing.B) { + benchmarkCollector(b, "terminal_services", NewTerminalServicesCollector) +} diff --git a/collector/thermalzone_test.go b/collector/thermalzone_test.go new file mode 100644 index 00000000..d5d60b3e --- /dev/null +++ b/collector/thermalzone_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkThermalZoneCollector(b *testing.B) { + benchmarkCollector(b, "thermalzone", NewThermalZoneCollector) +} diff --git a/collector/time_test.go b/collector/time_test.go new file mode 100644 index 00000000..9107bea9 --- /dev/null +++ b/collector/time_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkTimeCollector(b *testing.B) { + benchmarkCollector(b, "time", newTimeCollector) +} diff --git a/collector/vmware_test.go b/collector/vmware_test.go new file mode 100644 index 00000000..f135530a --- /dev/null +++ b/collector/vmware_test.go @@ -0,0 +1,9 @@ +package collector + +import ( + "testing" +) + +func BenchmarkVmwareCollector(b *testing.B) { + benchmarkCollector(b, "vmware", NewVmwareCollector) +}