From 4bc9e37b0f5cbb28af8679de511c14ba5ad3d7e0 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 13:35:07 -0700 Subject: [PATCH 1/8] faster file read --- internal/common/common.go | 23 +++++++++++++ internal/common/common_linux.go | 73 +++++++++++++++++++++++------------------ process/process_test.go | 7 ++++ 3 files changed, 71 insertions(+), 32 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index 9bfece3..bad8d1f 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -114,6 +114,29 @@ func ReadLines(filename string) ([]string, error) { return ReadLinesOffsetN(filename, 0, -1) } +// ReadLine reads a file and returns the first occurrence of a line that is prefixed with prefix. +func ReadLine(filename string, prefix string) (string, error) { + f, err := os.Open(filename) + if err != nil { + return "", err + } + defer f.Close() + r := bufio.NewReader(f) + for { + line, err := r.ReadString('\n') + if err != nil { + if err == io.EOF { + break + } + } + if strings.HasPrefix(line, prefix) { + return line, nil + } + } + + return "", nil +} + // ReadLinesOffsetN reads contents from file and splits them by new line. // The offset tells at which line number to start. // The count determines the number of lines to read (starting from offset): diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index b58edbe..607d963 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -62,17 +62,38 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { return 0, err } - statFile := "stat" + useStatFile := true if system == "lxc" && role == "guest" { // if lxc, /proc/uptime is used. - statFile = "uptime" + useStatFile = false } else if system == "docker" && role == "guest" { // also docker, guest - statFile = "uptime" + useStatFile = false } - filename := HostProcWithContext(ctx, statFile) - lines, err := ReadLines(filename) + if useStatFile { + return readBootTimeStat(ctx) + } else { + filename := HostProcWithContext(ctx, "uptime") + lines, err := ReadLines(filename) + if err != nil { + return handleBootTimeFileReadErr(err) + } + if len(lines) != 1 { + return 0, fmt.Errorf("wrong uptime format") + } + f := strings.Fields(lines[0]) + b, err := strconv.ParseFloat(f[0], 64) + if err != nil { + return 0, err + } + currentTime := float64(time.Now().UnixNano()) / float64(time.Second) + t := currentTime - b + return uint64(t), nil + } +} + +func handleBootTimeFileReadErr(err error) (uint64, error) { if os.IsPermission(err) { var info syscall.Sysinfo_t err := syscall.Sysinfo(&info) @@ -81,42 +102,30 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { } currentTime := time.Now().UnixNano() / int64(time.Second) - t := currentTime - int64(info.Uptime) + t := currentTime - info.Uptime return uint64(t), nil } + return 0, err +} + +func readBootTimeStat(ctx context.Context) (uint64, error) { + filename := HostProcWithContext(ctx, "stat") + line, err := ReadLine(filename, "btime") if err != nil { - return 0, err + return handleBootTimeFileReadErr(err) } - - if statFile == "stat" { - for _, line := range lines { - if strings.HasPrefix(line, "btime") { - f := strings.Fields(line) - if len(f) != 2 { - return 0, fmt.Errorf("wrong btime format") - } - b, err := strconv.ParseInt(f[1], 10, 64) - if err != nil { - return 0, err - } - t := uint64(b) - return t, nil - } - } - } else if statFile == "uptime" { - if len(lines) != 1 { - return 0, fmt.Errorf("wrong uptime format") + if strings.HasPrefix(line, "btime") { + f := strings.Fields(line) + if len(f) != 2 { + return 0, fmt.Errorf("wrong btime format") } - f := strings.Fields(lines[0]) - b, err := strconv.ParseFloat(f[0], 64) + b, err := strconv.ParseInt(f[1], 10, 64) if err != nil { return 0, err } - currentTime := float64(time.Now().UnixNano()) / float64(time.Second) - t := currentTime - b - return uint64(t), nil + t := uint64(b) + return t, nil } - return 0, fmt.Errorf("could not find btime") } diff --git a/process/process_test.go b/process/process_test.go index 9281c93..ae26960 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/shirou/gopsutil/v3/internal/common" + "github.com/stretchr/testify/require" ) var mu sync.Mutex @@ -862,3 +863,9 @@ func BenchmarkProcessPpid(b *testing.B) { p.Ppid() } } + +func BenchmarkProcesses(b *testing.B) { + ps, err := Processes() + require.NoError(b, err) + require.Greater(b, len(ps), 0) +} From a7a94986fb9b6deeef51eb25c71a73c1d1c9becc Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:16:28 -0700 Subject: [PATCH 2/8] send results --- process/results.txt | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 process/results.txt diff --git a/process/results.txt b/process/results.txt new file mode 100644 index 0000000..ec9a266 --- /dev/null +++ b/process/results.txt @@ -0,0 +1,108 @@ +on branch read_file_faster: + +/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcesses_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcesses\E$ -test.run ^$ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcesses +BenchmarkProcesses-80 1000000000 0.1756 ns/op +PASS + +on branch master: +/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcessPpid_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcessPpid\E$ -test.run ^$ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcessPpid +BenchmarkProcessPpid-80 8694 126542 ns/op +PASS + +Typical /proc/stat file: +cpu 768944 10288 1072079 11045715258 3486 0 5444 0 0 0 +cpu0 8786 70 6308 137988080 26 0 67 0 0 0 +cpu1 9068 30 6191 138072341 59 0 11 0 0 0 +cpu2 65109 159 171353 137907618 83 0 8 0 0 0 +cpu3 24084 118 40891 137992666 88 0 8 0 0 0 +cpu4 43038 444 109352 137978323 83 0 4 0 0 0 +cpu5 21472 193 40224 138025036 82 0 0 0 0 0 +cpu6 42410 118 123254 137981039 92 0 3 0 0 0 +cpu7 18037 232 33534 138039186 101 0 5 0 0 0 +cpu8 31662 186 72772 138013182 98 0 4 0 0 0 +cpu9 13657 181 17251 138023478 91 0 221 0 0 0 +cpu10 31512 159 74950 138015049 40 0 0 0 0 0 +cpu11 12116 472 16357 138037994 71 0 166 0 0 0 +cpu12 21121 85 43973 138039462 63 0 4 0 0 0 +cpu13 10251 27 10354 138021006 59 0 0 0 0 0 +cpu14 18905 280 40738 138048357 50 0 5 0 0 0 +cpu15 10326 360 10793 138065576 47 0 3 0 0 0 +cpu16 11191 100 13201 138062883 52 0 0 0 0 0 +cpu17 8028 107 7053 138066687 54 0 0 0 0 0 +cpu18 7964 102 7473 138082226 43 0 6 0 0 0 +cpu19 7326 158 5075 138084645 27 0 1 0 0 0 +cpu20 7577 4 6287 138079436 41 0 0 0 0 0 +cpu21 7813 12 6644 138067886 84 0 131 0 0 0 +cpu22 6736 117 5078 138085923 19 0 2 0 0 0 +cpu23 7123 128 5150 138086066 21 0 3 0 0 0 +cpu24 9399 13 6703 138082336 24 0 39 0 0 0 +cpu25 8955 45 6350 138083373 26 0 0 0 0 0 +cpu26 6441 83 4730 138088400 31 0 2 0 0 0 +cpu27 6157 25 4148 138089297 27 0 6 0 0 0 +cpu28 6401 9 4586 138087528 44 0 1 0 0 0 +cpu29 6391 17 4265 138087849 46 0 21 0 0 0 +cpu30 6286 461 3308 138090520 50 0 0 0 0 0 +cpu31 6094 104 3068 138091314 86 0 0 0 0 0 +cpu32 6022 108 3822 138089008 19 0 1 0 0 0 +cpu33 5909 73 3729 138088729 11 0 1 0 0 0 +cpu34 6820 294 3541 138088142 6 0 1 0 0 0 +cpu35 6616 129 3347 138090230 8 0 0 0 0 0 +cpu36 6206 107 4706 138083252 9 0 1 0 0 0 +cpu37 6584 28 2864 138085803 23 0 1 0 0 0 +cpu38 6220 131 3001 138091331 37 0 2 0 0 0 +cpu39 5486 217 2772 138092221 38 0 2 0 0 0 +cpu40 5842 31 2824 138093761 14 0 0 0 0 0 +cpu41 6105 125 2923 138093217 17 0 0 0 0 0 +cpu42 5204 44 3539 138091542 12 0 1 0 0 0 +cpu43 5839 32 3417 138091082 10 0 4 0 0 0 +cpu44 5615 19 3980 138092487 22 0 4 0 0 0 +cpu45 6026 424 4100 138091306 19 0 0 0 0 0 +cpu46 5527 209 3688 138094125 12 0 0 0 0 0 +cpu47 5336 109 3033 138094396 12 0 3 0 0 0 +cpu48 5494 103 2653 138096147 2 0 1 0 0 0 +cpu49 5218 30 2393 138097187 5 0 0 0 0 0 +cpu50 5484 239 2130 138096460 8 0 0 0 0 0 +cpu51 5043 34 2446 138096773 15 0 0 0 0 0 +cpu52 5498 27 2971 138095154 8 0 12 0 0 0 +cpu53 5181 43 2634 138087718 8 0 2 0 0 0 +cpu54 4988 36 2976 138095457 4 0 0 0 0 0 +cpu55 5081 21 2344 138095338 7 0 0 0 0 0 +cpu56 4636 273 1897 138096693 12 0 2 0 0 0 +cpu57 4884 316 1935 138095963 15 0 3 0 0 0 +cpu58 4870 229 1745 138097331 6 0 1 0 0 0 +cpu59 4924 28 1746 138097999 7 0 5 0 0 0 +cpu60 4461 129 1955 138096954 361 0 0 0 0 0 +cpu61 5030 35 1930 138097414 460 0 0 0 0 0 +cpu62 4179 23 2157 138097375 9 0 5 0 0 0 +cpu63 4242 134 2235 138096928 10 0 8 0 0 0 +cpu64 6682 254 2108 138094052 4 0 3 0 0 0 +cpu65 4354 115 1743 138097071 3 0 0 0 0 0 +cpu66 4162 9 2496 138095759 7 0 0 0 0 0 +cpu67 3863 28 1960 138097697 6 0 5 0 0 0 +cpu68 13484 17 17176 137901816 109 0 4614 0 0 0 +cpu69 12515 17 14273 137980618 100 0 4 0 0 0 +cpu70 5085 74 3738 138092386 10 0 0 0 0 0 +cpu71 4792 14 3574 138093476 13 0 0 0 0 0 +cpu72 4943 167 2880 138090318 12 0 2 0 0 0 +cpu73 5333 17 4367 138089338 19 0 0 0 0 0 +cpu74 4909 5 2995 138076914 30 0 2 0 0 0 +cpu75 4817 256 3160 138072268 30 0 0 0 0 0 +cpu76 4146 233 2160 138096352 8 0 1 0 0 0 +cpu77 4598 474 1860 138096155 10 0 3 0 0 0 +cpu78 4733 34 2352 138094484 29 0 4 0 0 0 +cpu79 4488 164 2359 138094227 44 0 2 0 0 0 +intr 1136940775 0 1020438 49587864 0 0 0 210556659 0 0 0 0 0 867076724 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51690 0 0 0 0 0 0 0 1 5604085 970576 914985 910669 70 33120 33768 24240 21358 14764 17850 12776 9795 8765 6098 7307 8012 15687 13640 7797 11437 +ctxt 1918526774 +btime 1691617230 +processes 501797 +procs_running 39 +procs_blocked 0 +softirq 272142382 51064 32640104 523 8408436 152655 0 4696 152439652 131 78445121 From a48fd711371c6f4bf1f4f8d92e5eb791bd6b9a61 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:33:37 -0700 Subject: [PATCH 3/8] Revert "send results" This reverts commit a7a94986fb9b6deeef51eb25c71a73c1d1c9becc. --- process/results.txt | 108 ---------------------------------------------------- 1 file changed, 108 deletions(-) delete mode 100644 process/results.txt diff --git a/process/results.txt b/process/results.txt deleted file mode 100644 index ec9a266..0000000 --- a/process/results.txt +++ /dev/null @@ -1,108 +0,0 @@ -on branch read_file_faster: - -/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcesses_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcesses\E$ -test.run ^$ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcesses -BenchmarkProcesses-80 1000000000 0.1756 ns/op -PASS - -on branch master: -/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcessPpid_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcessPpid\E$ -test.run ^$ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcessPpid -BenchmarkProcessPpid-80 8694 126542 ns/op -PASS - -Typical /proc/stat file: -cpu 768944 10288 1072079 11045715258 3486 0 5444 0 0 0 -cpu0 8786 70 6308 137988080 26 0 67 0 0 0 -cpu1 9068 30 6191 138072341 59 0 11 0 0 0 -cpu2 65109 159 171353 137907618 83 0 8 0 0 0 -cpu3 24084 118 40891 137992666 88 0 8 0 0 0 -cpu4 43038 444 109352 137978323 83 0 4 0 0 0 -cpu5 21472 193 40224 138025036 82 0 0 0 0 0 -cpu6 42410 118 123254 137981039 92 0 3 0 0 0 -cpu7 18037 232 33534 138039186 101 0 5 0 0 0 -cpu8 31662 186 72772 138013182 98 0 4 0 0 0 -cpu9 13657 181 17251 138023478 91 0 221 0 0 0 -cpu10 31512 159 74950 138015049 40 0 0 0 0 0 -cpu11 12116 472 16357 138037994 71 0 166 0 0 0 -cpu12 21121 85 43973 138039462 63 0 4 0 0 0 -cpu13 10251 27 10354 138021006 59 0 0 0 0 0 -cpu14 18905 280 40738 138048357 50 0 5 0 0 0 -cpu15 10326 360 10793 138065576 47 0 3 0 0 0 -cpu16 11191 100 13201 138062883 52 0 0 0 0 0 -cpu17 8028 107 7053 138066687 54 0 0 0 0 0 -cpu18 7964 102 7473 138082226 43 0 6 0 0 0 -cpu19 7326 158 5075 138084645 27 0 1 0 0 0 -cpu20 7577 4 6287 138079436 41 0 0 0 0 0 -cpu21 7813 12 6644 138067886 84 0 131 0 0 0 -cpu22 6736 117 5078 138085923 19 0 2 0 0 0 -cpu23 7123 128 5150 138086066 21 0 3 0 0 0 -cpu24 9399 13 6703 138082336 24 0 39 0 0 0 -cpu25 8955 45 6350 138083373 26 0 0 0 0 0 -cpu26 6441 83 4730 138088400 31 0 2 0 0 0 -cpu27 6157 25 4148 138089297 27 0 6 0 0 0 -cpu28 6401 9 4586 138087528 44 0 1 0 0 0 -cpu29 6391 17 4265 138087849 46 0 21 0 0 0 -cpu30 6286 461 3308 138090520 50 0 0 0 0 0 -cpu31 6094 104 3068 138091314 86 0 0 0 0 0 -cpu32 6022 108 3822 138089008 19 0 1 0 0 0 -cpu33 5909 73 3729 138088729 11 0 1 0 0 0 -cpu34 6820 294 3541 138088142 6 0 1 0 0 0 -cpu35 6616 129 3347 138090230 8 0 0 0 0 0 -cpu36 6206 107 4706 138083252 9 0 1 0 0 0 -cpu37 6584 28 2864 138085803 23 0 1 0 0 0 -cpu38 6220 131 3001 138091331 37 0 2 0 0 0 -cpu39 5486 217 2772 138092221 38 0 2 0 0 0 -cpu40 5842 31 2824 138093761 14 0 0 0 0 0 -cpu41 6105 125 2923 138093217 17 0 0 0 0 0 -cpu42 5204 44 3539 138091542 12 0 1 0 0 0 -cpu43 5839 32 3417 138091082 10 0 4 0 0 0 -cpu44 5615 19 3980 138092487 22 0 4 0 0 0 -cpu45 6026 424 4100 138091306 19 0 0 0 0 0 -cpu46 5527 209 3688 138094125 12 0 0 0 0 0 -cpu47 5336 109 3033 138094396 12 0 3 0 0 0 -cpu48 5494 103 2653 138096147 2 0 1 0 0 0 -cpu49 5218 30 2393 138097187 5 0 0 0 0 0 -cpu50 5484 239 2130 138096460 8 0 0 0 0 0 -cpu51 5043 34 2446 138096773 15 0 0 0 0 0 -cpu52 5498 27 2971 138095154 8 0 12 0 0 0 -cpu53 5181 43 2634 138087718 8 0 2 0 0 0 -cpu54 4988 36 2976 138095457 4 0 0 0 0 0 -cpu55 5081 21 2344 138095338 7 0 0 0 0 0 -cpu56 4636 273 1897 138096693 12 0 2 0 0 0 -cpu57 4884 316 1935 138095963 15 0 3 0 0 0 -cpu58 4870 229 1745 138097331 6 0 1 0 0 0 -cpu59 4924 28 1746 138097999 7 0 5 0 0 0 -cpu60 4461 129 1955 138096954 361 0 0 0 0 0 -cpu61 5030 35 1930 138097414 460 0 0 0 0 0 -cpu62 4179 23 2157 138097375 9 0 5 0 0 0 -cpu63 4242 134 2235 138096928 10 0 8 0 0 0 -cpu64 6682 254 2108 138094052 4 0 3 0 0 0 -cpu65 4354 115 1743 138097071 3 0 0 0 0 0 -cpu66 4162 9 2496 138095759 7 0 0 0 0 0 -cpu67 3863 28 1960 138097697 6 0 5 0 0 0 -cpu68 13484 17 17176 137901816 109 0 4614 0 0 0 -cpu69 12515 17 14273 137980618 100 0 4 0 0 0 -cpu70 5085 74 3738 138092386 10 0 0 0 0 0 -cpu71 4792 14 3574 138093476 13 0 0 0 0 0 -cpu72 4943 167 2880 138090318 12 0 2 0 0 0 -cpu73 5333 17 4367 138089338 19 0 0 0 0 0 -cpu74 4909 5 2995 138076914 30 0 2 0 0 0 -cpu75 4817 256 3160 138072268 30 0 0 0 0 0 -cpu76 4146 233 2160 138096352 8 0 1 0 0 0 -cpu77 4598 474 1860 138096155 10 0 3 0 0 0 -cpu78 4733 34 2352 138094484 29 0 4 0 0 0 -cpu79 4488 164 2359 138094227 44 0 2 0 0 0 -intr 1136940775 0 1020438 49587864 0 0 0 210556659 0 0 0 0 0 867076724 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51690 0 0 0 0 0 0 0 1 5604085 970576 914985 910669 70 33120 33768 24240 21358 14764 17850 12776 9795 8765 6098 7307 8012 15687 13640 7797 11437 -ctxt 1918526774 -btime 1691617230 -processes 501797 -procs_running 39 -procs_blocked 0 -softirq 272142382 51064 32640104 523 8408436 152655 0 4696 152439652 131 78445121 From ca71a6db3c7495189bdc01e2235e79175cfd5bd2 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:38:11 -0700 Subject: [PATCH 4/8] lint --- internal/common/common_linux.go | 36 ++++++++++++++++++------------------ process/process_test.go | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index 607d963..a644687 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -73,24 +73,24 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { if useStatFile { return readBootTimeStat(ctx) - } else { - filename := HostProcWithContext(ctx, "uptime") - lines, err := ReadLines(filename) - if err != nil { - return handleBootTimeFileReadErr(err) - } - if len(lines) != 1 { - return 0, fmt.Errorf("wrong uptime format") - } - f := strings.Fields(lines[0]) - b, err := strconv.ParseFloat(f[0], 64) - if err != nil { - return 0, err - } - currentTime := float64(time.Now().UnixNano()) / float64(time.Second) - t := currentTime - b - return uint64(t), nil } + + filename := HostProcWithContext(ctx, "uptime") + lines, err := ReadLines(filename) + if err != nil { + return handleBootTimeFileReadErr(err) + } + if len(lines) != 1 { + return 0, fmt.Errorf("wrong uptime format") + } + f := strings.Fields(lines[0]) + b, err := strconv.ParseFloat(f[0], 64) + if err != nil { + return 0, err + } + currentTime := float64(time.Now().UnixNano()) / float64(time.Second) + t := currentTime - b + return uint64(t), nil } func handleBootTimeFileReadErr(err error) (uint64, error) { @@ -102,7 +102,7 @@ func handleBootTimeFileReadErr(err error) (uint64, error) { } currentTime := time.Now().UnixNano() / int64(time.Second) - t := currentTime - info.Uptime + t := currentTime - int64(info.Uptime) return uint64(t), nil } return 0, err diff --git a/process/process_test.go b/process/process_test.go index ae26960..6dc5695 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -19,9 +19,9 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/shirou/gopsutil/v3/internal/common" - "github.com/stretchr/testify/require" ) var mu sync.Mutex From 4ed0f1436a55c8083fda990321046f12d73c92c0 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:39:29 -0700 Subject: [PATCH 5/8] return err if not EOF --- internal/common/common.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/common/common.go b/internal/common/common.go index bad8d1f..7a31d25 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -128,6 +128,7 @@ func ReadLine(filename string, prefix string) (string, error) { if err == io.EOF { break } + return "", err } if strings.HasPrefix(line, prefix) { return line, nil From 6fff1c0e99b0a8b6f9566cfea98353918830f0b0 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Sun, 27 Aug 2023 09:42:51 -0700 Subject: [PATCH 6/8] proper benchmark --- process/process_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/process/process_test.go b/process/process_test.go index 6dc5695..61e2a7a 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -865,7 +865,9 @@ func BenchmarkProcessPpid(b *testing.B) { } func BenchmarkProcesses(b *testing.B) { - ps, err := Processes() - require.NoError(b, err) - require.Greater(b, len(ps), 0) + for i := 0; i < b.N; i++ { + ps, err := Processes() + require.NoError(b, err) + require.Greater(b, len(ps), 0) + } } From ae119d36b8280344151f33a5db5c4a5d58faff89 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Sun, 27 Aug 2023 09:46:08 -0700 Subject: [PATCH 7/8] test results --- process/new.txt | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ process/old.txt | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test_results.txt | 33 ++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 process/new.txt create mode 100644 process/old.txt create mode 100644 test_results.txt diff --git a/process/new.txt b/process/new.txt new file mode 100644 index 0000000..2695a0a --- /dev/null +++ b/process/new.txt @@ -0,0 +1,65 @@ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +Benchmark_fillFromCommWithContext-80 117801 9645 ns/op +Benchmark_fillFromCommWithContext-80 128160 9557 ns/op +Benchmark_fillFromCommWithContext-80 124488 9570 ns/op +Benchmark_fillFromCommWithContext-80 126180 9488 ns/op +Benchmark_fillFromCommWithContext-80 113413 9889 ns/op +Benchmark_fillFromCommWithContext-80 120090 9650 ns/op +Benchmark_fillFromCommWithContext-80 121117 10132 ns/op +Benchmark_fillFromCommWithContext-80 109662 9708 ns/op +Benchmark_fillFromCommWithContext-80 124315 9951 ns/op +Benchmark_fillFromCommWithContext-80 114738 9754 ns/op +Benchmark_fillFromStatusWithContext-80 42361 25613 ns/op +Benchmark_fillFromStatusWithContext-80 45180 26140 ns/op +Benchmark_fillFromStatusWithContext-80 42746 25890 ns/op +Benchmark_fillFromStatusWithContext-80 44743 24767 ns/op +Benchmark_fillFromStatusWithContext-80 47257 26215 ns/op +Benchmark_fillFromStatusWithContext-80 48838 26133 ns/op +Benchmark_fillFromStatusWithContext-80 47120 26333 ns/op +Benchmark_fillFromStatusWithContext-80 44583 26602 ns/op +Benchmark_fillFromStatusWithContext-80 53058 25234 ns/op +Benchmark_fillFromStatusWithContext-80 43609 24786 ns/op +BenchmarkNewProcess-80 9540 125766 ns/op +BenchmarkNewProcess-80 9381 130526 ns/op +BenchmarkNewProcess-80 8694 130677 ns/op +BenchmarkNewProcess-80 10000 126691 ns/op +BenchmarkNewProcess-80 9465 123453 ns/op +BenchmarkNewProcess-80 9014 129650 ns/op +BenchmarkNewProcess-80 9685 127454 ns/op +BenchmarkNewProcess-80 9266 132037 ns/op +BenchmarkNewProcess-80 9447 126732 ns/op +BenchmarkNewProcess-80 9324 128946 ns/op +BenchmarkProcessName-80 405714842 2.906 ns/op +BenchmarkProcessName-80 414845893 2.906 ns/op +BenchmarkProcessName-80 405879571 2.903 ns/op +BenchmarkProcessName-80 405576182 2.902 ns/op +BenchmarkProcessName-80 406102254 2.929 ns/op +BenchmarkProcessName-80 405528814 2.917 ns/op +BenchmarkProcessName-80 406795386 2.904 ns/op +BenchmarkProcessName-80 405236899 2.903 ns/op +BenchmarkProcessName-80 407348631 2.904 ns/op +BenchmarkProcessName-80 406594641 2.900 ns/op +BenchmarkProcessPpid-80 8344 120555 ns/op +BenchmarkProcessPpid-80 10236 120711 ns/op +BenchmarkProcessPpid-80 10000 119398 ns/op +BenchmarkProcessPpid-80 10318 118461 ns/op +BenchmarkProcessPpid-80 10435 116859 ns/op +BenchmarkProcessPpid-80 10386 123064 ns/op +BenchmarkProcessPpid-80 10291 117243 ns/op +BenchmarkProcessPpid-80 10238 119114 ns/op +BenchmarkProcessPpid-80 10484 118057 ns/op +BenchmarkProcessPpid-80 10135 117615 ns/op +BenchmarkProcesses-80 5 218228353 ns/op +BenchmarkProcesses-80 5 215826386 ns/op +BenchmarkProcesses-80 5 219362359 ns/op +BenchmarkProcesses-80 5 215391927 ns/op +BenchmarkProcesses-80 5 209502339 ns/op +BenchmarkProcesses-80 5 217973671 ns/op +BenchmarkProcesses-80 5 211982866 ns/op +BenchmarkProcesses-80 6 207617137 ns/op +BenchmarkProcesses-80 5 217065433 ns/op +BenchmarkProcesses-80 5 211256014 ns/op +PASS +ok github.com/shirou/gopsutil/v3/process 101.689s diff --git a/process/old.txt b/process/old.txt new file mode 100644 index 0000000..0f6d4fe --- /dev/null +++ b/process/old.txt @@ -0,0 +1,65 @@ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +Benchmark_fillFromCommWithContext-80 119751 9204 ns/op +Benchmark_fillFromCommWithContext-80 124860 9442 ns/op +Benchmark_fillFromCommWithContext-80 130927 9197 ns/op +Benchmark_fillFromCommWithContext-80 118206 9708 ns/op +Benchmark_fillFromCommWithContext-80 116026 9653 ns/op +Benchmark_fillFromCommWithContext-80 129135 9085 ns/op +Benchmark_fillFromCommWithContext-80 119784 9883 ns/op +Benchmark_fillFromCommWithContext-80 125440 9648 ns/op +Benchmark_fillFromCommWithContext-80 124136 9535 ns/op +Benchmark_fillFromCommWithContext-80 123098 9556 ns/op +Benchmark_fillFromStatusWithContext-80 43220 25280 ns/op +Benchmark_fillFromStatusWithContext-80 50610 25287 ns/op +Benchmark_fillFromStatusWithContext-80 48500 24915 ns/op +Benchmark_fillFromStatusWithContext-80 47887 25965 ns/op +Benchmark_fillFromStatusWithContext-80 46794 24535 ns/op +Benchmark_fillFromStatusWithContext-80 44284 24318 ns/op +Benchmark_fillFromStatusWithContext-80 52395 25844 ns/op +Benchmark_fillFromStatusWithContext-80 48452 25524 ns/op +Benchmark_fillFromStatusWithContext-80 47455 26497 ns/op +Benchmark_fillFromStatusWithContext-80 54568 26063 ns/op +BenchmarkNewProcess-80 8568 139806 ns/op +BenchmarkNewProcess-80 8533 140902 ns/op +BenchmarkNewProcess-80 8624 138080 ns/op +BenchmarkNewProcess-80 9368 138218 ns/op +BenchmarkNewProcess-80 8607 133557 ns/op +BenchmarkNewProcess-80 8623 137908 ns/op +BenchmarkNewProcess-80 8538 145054 ns/op +BenchmarkNewProcess-80 8443 147078 ns/op +BenchmarkNewProcess-80 7162 141291 ns/op +BenchmarkNewProcess-80 7294 145403 ns/op +BenchmarkProcessName-80 381517957 3.099 ns/op +BenchmarkProcessName-80 381213841 3.099 ns/op +BenchmarkProcessName-80 382578319 3.101 ns/op +BenchmarkProcessName-80 382308120 3.096 ns/op +BenchmarkProcessName-80 381155623 3.118 ns/op +BenchmarkProcessName-80 382158510 3.099 ns/op +BenchmarkProcessName-80 381464982 3.098 ns/op +BenchmarkProcessName-80 381580702 3.101 ns/op +BenchmarkProcessName-80 381748222 3.098 ns/op +BenchmarkProcessName-80 381223675 3.096 ns/op +BenchmarkProcessPpid-80 9006 132538 ns/op +BenchmarkProcessPpid-80 7534 134648 ns/op +BenchmarkProcessPpid-80 9085 128758 ns/op +BenchmarkProcessPpid-80 9062 133006 ns/op +BenchmarkProcessPpid-80 8802 127635 ns/op +BenchmarkProcessPpid-80 9714 129837 ns/op +BenchmarkProcessPpid-80 9093 132282 ns/op +BenchmarkProcessPpid-80 8434 124920 ns/op +BenchmarkProcessPpid-80 9072 128690 ns/op +BenchmarkProcessPpid-80 9181 133940 ns/op +BenchmarkProcesses-80 5 249461092 ns/op +BenchmarkProcesses-80 4 257841237 ns/op +BenchmarkProcesses-80 4 256107246 ns/op +BenchmarkProcesses-80 4 256276757 ns/op +BenchmarkProcesses-80 4 255431972 ns/op +BenchmarkProcesses-80 5 261030906 ns/op +BenchmarkProcesses-80 5 259028838 ns/op +BenchmarkProcesses-80 4 255359492 ns/op +BenchmarkProcesses-80 4 251292727 ns/op +BenchmarkProcesses-80 5 251119366 ns/op +PASS +ok github.com/shirou/gopsutil/v3/process 103.759s diff --git a/test_results.txt b/test_results.txt new file mode 100644 index 0000000..99a4a97 --- /dev/null +++ b/test_results.txt @@ -0,0 +1,33 @@ +Before: +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcesses +BenchmarkProcesses-80 4 252089573 ns/op +PASS + +Process finished with the exit code 0 + +After: +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcesses +BenchmarkProcesses-80 6 203633386 ns/op +PASS + +Process finished with the exit code 0 + + +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process + │ old.txt │ new.txt │ + │ sec/op │ sec/op vs base │ +_fillFromCommWithContext-80 9.546µ ± 4% 9.679µ ± 3% +1.40% (p=0.045 n=10) +_fillFromStatusWithContext-80 25.41µ ± 3% 26.01µ ± 5% ~ (p=0.247 n=10) +NewProcess-80 140.4µ ± 4% 128.2µ ± 2% -8.66% (p=0.000 n=10) +ProcessName-80 3.099n ± 0% 2.904n ± 0% -6.29% (p=0.000 n=10) +ProcessPpid-80 131.1µ ± 3% 118.8µ ± 2% -9.36% (p=0.000 n=10) +Processes-80 255.8m ± 2% 215.6m ± 3% -15.70% (p=0.000 n=10) +geomean 39.03µ 36.59µ -6.25% From 54c31d884d8caa2caa6e1fa4ab81526ebe25f82f Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Sun, 27 Aug 2023 10:01:46 -0700 Subject: [PATCH 8/8] Revert "test results" This reverts commit ae119d36b8280344151f33a5db5c4a5d58faff89. --- process/new.txt | 65 -------------------------------------------------------- process/old.txt | 65 -------------------------------------------------------- test_results.txt | 33 ---------------------------- 3 files changed, 163 deletions(-) delete mode 100644 process/new.txt delete mode 100644 process/old.txt delete mode 100644 test_results.txt diff --git a/process/new.txt b/process/new.txt deleted file mode 100644 index 2695a0a..0000000 --- a/process/new.txt +++ /dev/null @@ -1,65 +0,0 @@ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -Benchmark_fillFromCommWithContext-80 117801 9645 ns/op -Benchmark_fillFromCommWithContext-80 128160 9557 ns/op -Benchmark_fillFromCommWithContext-80 124488 9570 ns/op -Benchmark_fillFromCommWithContext-80 126180 9488 ns/op -Benchmark_fillFromCommWithContext-80 113413 9889 ns/op -Benchmark_fillFromCommWithContext-80 120090 9650 ns/op -Benchmark_fillFromCommWithContext-80 121117 10132 ns/op -Benchmark_fillFromCommWithContext-80 109662 9708 ns/op -Benchmark_fillFromCommWithContext-80 124315 9951 ns/op -Benchmark_fillFromCommWithContext-80 114738 9754 ns/op -Benchmark_fillFromStatusWithContext-80 42361 25613 ns/op -Benchmark_fillFromStatusWithContext-80 45180 26140 ns/op -Benchmark_fillFromStatusWithContext-80 42746 25890 ns/op -Benchmark_fillFromStatusWithContext-80 44743 24767 ns/op -Benchmark_fillFromStatusWithContext-80 47257 26215 ns/op -Benchmark_fillFromStatusWithContext-80 48838 26133 ns/op -Benchmark_fillFromStatusWithContext-80 47120 26333 ns/op -Benchmark_fillFromStatusWithContext-80 44583 26602 ns/op -Benchmark_fillFromStatusWithContext-80 53058 25234 ns/op -Benchmark_fillFromStatusWithContext-80 43609 24786 ns/op -BenchmarkNewProcess-80 9540 125766 ns/op -BenchmarkNewProcess-80 9381 130526 ns/op -BenchmarkNewProcess-80 8694 130677 ns/op -BenchmarkNewProcess-80 10000 126691 ns/op -BenchmarkNewProcess-80 9465 123453 ns/op -BenchmarkNewProcess-80 9014 129650 ns/op -BenchmarkNewProcess-80 9685 127454 ns/op -BenchmarkNewProcess-80 9266 132037 ns/op -BenchmarkNewProcess-80 9447 126732 ns/op -BenchmarkNewProcess-80 9324 128946 ns/op -BenchmarkProcessName-80 405714842 2.906 ns/op -BenchmarkProcessName-80 414845893 2.906 ns/op -BenchmarkProcessName-80 405879571 2.903 ns/op -BenchmarkProcessName-80 405576182 2.902 ns/op -BenchmarkProcessName-80 406102254 2.929 ns/op -BenchmarkProcessName-80 405528814 2.917 ns/op -BenchmarkProcessName-80 406795386 2.904 ns/op -BenchmarkProcessName-80 405236899 2.903 ns/op -BenchmarkProcessName-80 407348631 2.904 ns/op -BenchmarkProcessName-80 406594641 2.900 ns/op -BenchmarkProcessPpid-80 8344 120555 ns/op -BenchmarkProcessPpid-80 10236 120711 ns/op -BenchmarkProcessPpid-80 10000 119398 ns/op -BenchmarkProcessPpid-80 10318 118461 ns/op -BenchmarkProcessPpid-80 10435 116859 ns/op -BenchmarkProcessPpid-80 10386 123064 ns/op -BenchmarkProcessPpid-80 10291 117243 ns/op -BenchmarkProcessPpid-80 10238 119114 ns/op -BenchmarkProcessPpid-80 10484 118057 ns/op -BenchmarkProcessPpid-80 10135 117615 ns/op -BenchmarkProcesses-80 5 218228353 ns/op -BenchmarkProcesses-80 5 215826386 ns/op -BenchmarkProcesses-80 5 219362359 ns/op -BenchmarkProcesses-80 5 215391927 ns/op -BenchmarkProcesses-80 5 209502339 ns/op -BenchmarkProcesses-80 5 217973671 ns/op -BenchmarkProcesses-80 5 211982866 ns/op -BenchmarkProcesses-80 6 207617137 ns/op -BenchmarkProcesses-80 5 217065433 ns/op -BenchmarkProcesses-80 5 211256014 ns/op -PASS -ok github.com/shirou/gopsutil/v3/process 101.689s diff --git a/process/old.txt b/process/old.txt deleted file mode 100644 index 0f6d4fe..0000000 --- a/process/old.txt +++ /dev/null @@ -1,65 +0,0 @@ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -Benchmark_fillFromCommWithContext-80 119751 9204 ns/op -Benchmark_fillFromCommWithContext-80 124860 9442 ns/op -Benchmark_fillFromCommWithContext-80 130927 9197 ns/op -Benchmark_fillFromCommWithContext-80 118206 9708 ns/op -Benchmark_fillFromCommWithContext-80 116026 9653 ns/op -Benchmark_fillFromCommWithContext-80 129135 9085 ns/op -Benchmark_fillFromCommWithContext-80 119784 9883 ns/op -Benchmark_fillFromCommWithContext-80 125440 9648 ns/op -Benchmark_fillFromCommWithContext-80 124136 9535 ns/op -Benchmark_fillFromCommWithContext-80 123098 9556 ns/op -Benchmark_fillFromStatusWithContext-80 43220 25280 ns/op -Benchmark_fillFromStatusWithContext-80 50610 25287 ns/op -Benchmark_fillFromStatusWithContext-80 48500 24915 ns/op -Benchmark_fillFromStatusWithContext-80 47887 25965 ns/op -Benchmark_fillFromStatusWithContext-80 46794 24535 ns/op -Benchmark_fillFromStatusWithContext-80 44284 24318 ns/op -Benchmark_fillFromStatusWithContext-80 52395 25844 ns/op -Benchmark_fillFromStatusWithContext-80 48452 25524 ns/op -Benchmark_fillFromStatusWithContext-80 47455 26497 ns/op -Benchmark_fillFromStatusWithContext-80 54568 26063 ns/op -BenchmarkNewProcess-80 8568 139806 ns/op -BenchmarkNewProcess-80 8533 140902 ns/op -BenchmarkNewProcess-80 8624 138080 ns/op -BenchmarkNewProcess-80 9368 138218 ns/op -BenchmarkNewProcess-80 8607 133557 ns/op -BenchmarkNewProcess-80 8623 137908 ns/op -BenchmarkNewProcess-80 8538 145054 ns/op -BenchmarkNewProcess-80 8443 147078 ns/op -BenchmarkNewProcess-80 7162 141291 ns/op -BenchmarkNewProcess-80 7294 145403 ns/op -BenchmarkProcessName-80 381517957 3.099 ns/op -BenchmarkProcessName-80 381213841 3.099 ns/op -BenchmarkProcessName-80 382578319 3.101 ns/op -BenchmarkProcessName-80 382308120 3.096 ns/op -BenchmarkProcessName-80 381155623 3.118 ns/op -BenchmarkProcessName-80 382158510 3.099 ns/op -BenchmarkProcessName-80 381464982 3.098 ns/op -BenchmarkProcessName-80 381580702 3.101 ns/op -BenchmarkProcessName-80 381748222 3.098 ns/op -BenchmarkProcessName-80 381223675 3.096 ns/op -BenchmarkProcessPpid-80 9006 132538 ns/op -BenchmarkProcessPpid-80 7534 134648 ns/op -BenchmarkProcessPpid-80 9085 128758 ns/op -BenchmarkProcessPpid-80 9062 133006 ns/op -BenchmarkProcessPpid-80 8802 127635 ns/op -BenchmarkProcessPpid-80 9714 129837 ns/op -BenchmarkProcessPpid-80 9093 132282 ns/op -BenchmarkProcessPpid-80 8434 124920 ns/op -BenchmarkProcessPpid-80 9072 128690 ns/op -BenchmarkProcessPpid-80 9181 133940 ns/op -BenchmarkProcesses-80 5 249461092 ns/op -BenchmarkProcesses-80 4 257841237 ns/op -BenchmarkProcesses-80 4 256107246 ns/op -BenchmarkProcesses-80 4 256276757 ns/op -BenchmarkProcesses-80 4 255431972 ns/op -BenchmarkProcesses-80 5 261030906 ns/op -BenchmarkProcesses-80 5 259028838 ns/op -BenchmarkProcesses-80 4 255359492 ns/op -BenchmarkProcesses-80 4 251292727 ns/op -BenchmarkProcesses-80 5 251119366 ns/op -PASS -ok github.com/shirou/gopsutil/v3/process 103.759s diff --git a/test_results.txt b/test_results.txt deleted file mode 100644 index 99a4a97..0000000 --- a/test_results.txt +++ /dev/null @@ -1,33 +0,0 @@ -Before: -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcesses -BenchmarkProcesses-80 4 252089573 ns/op -PASS - -Process finished with the exit code 0 - -After: -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcesses -BenchmarkProcesses-80 6 203633386 ns/op -PASS - -Process finished with the exit code 0 - - -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process - │ old.txt │ new.txt │ - │ sec/op │ sec/op vs base │ -_fillFromCommWithContext-80 9.546µ ± 4% 9.679µ ± 3% +1.40% (p=0.045 n=10) -_fillFromStatusWithContext-80 25.41µ ± 3% 26.01µ ± 5% ~ (p=0.247 n=10) -NewProcess-80 140.4µ ± 4% 128.2µ ± 2% -8.66% (p=0.000 n=10) -ProcessName-80 3.099n ± 0% 2.904n ± 0% -6.29% (p=0.000 n=10) -ProcessPpid-80 131.1µ ± 3% 118.8µ ± 2% -9.36% (p=0.000 n=10) -Processes-80 255.8m ± 2% 215.6m ± 3% -15.70% (p=0.000 n=10) -geomean 39.03µ 36.59µ -6.25%