View Javadoc
1   /*
2    * Copyright 2022-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix.aix.perfstat;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.emptyString;
9   import static org.hamcrest.Matchers.greaterThan;
10  import static org.hamcrest.Matchers.greaterThanOrEqualTo;
11  import static org.hamcrest.Matchers.not;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  import java.util.Arrays;
15  import java.util.List;
16  
17  import org.junit.jupiter.api.Test;
18  import org.junit.jupiter.api.condition.EnabledOnOs;
19  import org.junit.jupiter.api.condition.OS;
20  
21  import com.sun.jna.Native;
22  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_cpu_t;
23  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_cpu_total_t;
24  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_disk_t;
25  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_memory_total_t;
26  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_netinterface_t;
27  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_partition_config_t;
28  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_process_t;
29  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_protocol_t;
30  
31  @EnabledOnOs(OS.AIX)
32  class PerfstatTest {
33      @Test
34      void testQueryConfig() {
35          perfstat_partition_config_t config = PerfstatConfig.queryConfig();
36          assertThat("Should have at least one logical CPU", config.lcpus, greaterThan(0));
37      }
38  
39      @Test
40      void testQueryCpu() {
41          perfstat_cpu_t[] cpus = PerfstatCpu.queryCpu();
42          assertThat("Should have at least one CPU", cpus.length, greaterThan(0));
43          for (perfstat_cpu_t cpu : cpus) {
44              assertThat("Should have at least one idle tick", cpu.idle, greaterThan(0L));
45          }
46  
47          perfstat_cpu_total_t total = PerfstatCpu.queryCpuTotal();
48          assertThat("Should have at least one idle tick", total.idle, greaterThan(0L));
49  
50          long affinity = PerfstatCpu.queryCpuAffinityMask();
51          assertThat("Should have at least one CPU", affinity, greaterThan(0L));
52      }
53  
54      @Test
55      void testQueryDiskStats() {
56          perfstat_disk_t[] disks = PerfstatDisk.queryDiskStats();
57          assertThat("Should have at least one disk", disks.length, greaterThan(0));
58          for (perfstat_disk_t disk : disks) {
59              // Virtual disks may give 0 capacity but also have 0 time
60              if (disk.time == 0) {
61                  assertThat("Should have a nonnegative disk capacity", disk.size, greaterThanOrEqualTo(0L));
62              } else {
63                  assertThat("Should have a nonzero disk capacity", disk.size, greaterThan(0L));
64              }
65          }
66      }
67  
68      @Test
69      void testQueryMemory() {
70          perfstat_memory_total_t mem = PerfstatMemory.queryMemoryTotal();
71          assertThat("Should have nonzero memory", mem.real_total, greaterThan(0L));
72      }
73  
74      @Test
75      void testQueryNetInterface() {
76          perfstat_netinterface_t[] nets = PerfstatNetInterface.queryNetInterfaces();
77          assertThat("Should have at least one interface", nets.length, greaterThan(0));
78          for (perfstat_netinterface_t net : nets) {
79              assertThat("Should have a nonempty name", Native.toString(net.name), not(emptyString()));
80          }
81      }
82  
83      @Test
84      void testQueryProcesses() {
85          perfstat_process_t[] procs = PerfstatProcess.queryProcesses();
86          assertThat("Should have at least one process", procs.length, greaterThan(0));
87          for (perfstat_process_t proc : procs) {
88              assertThat("Should have at least one thread", proc.num_threads, greaterThan(0L));
89          }
90      }
91  
92      @Test
93      void testQueryProtocol() {
94          perfstat_protocol_t[] protos = PerfstatProtocol.queryProtocols();
95          assertThat("Should have at least one protocol", protos.length, greaterThan(0));
96          List<String> validProtos = Arrays.asList("ip", "ipv6", "icmp", "icmpv6", "udp", "tcp", "rpc", "nfs", "nfsv2",
97                  "nfsv3", "nfsv4");
98          for (perfstat_protocol_t proto : protos) {
99              String protoName = Native.toString(proto.name);
100             assertTrue(validProtos.contains(protoName), "Protocol must be in defined list of names");
101         }
102     }
103 }