| 1 |
// Copyright 2015-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build windows |
| 15 |
|
| 16 |
package pse |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"os/exec" |
| 21 |
"runtime" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
) |
| 26 |
|
| 27 |
func checkValues(t *testing.T, pcpu, tPcpu float64, rss, tRss int64) { |
| 28 |
if pcpu != tPcpu { |
| 29 |
delta := int64(pcpu - tPcpu) |
| 30 |
if delta < 0 { |
| 31 |
delta = -delta |
| 32 |
} |
| 33 |
if delta > 30 { // 30%? |
| 34 |
t.Fatalf("CPUs did not match close enough: %f vs %f", pcpu, tPcpu) |
| 35 |
} |
| 36 |
} |
| 37 |
if rss != tRss { |
| 38 |
delta := rss - tRss |
| 39 |
if delta < 0 { |
| 40 |
delta = -delta |
| 41 |
} |
| 42 |
if delta > 1024*1024 { // 1MB |
| 43 |
t.Fatalf("RSSs did not match close enough: %d vs %d", rss, tRss) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestPSEmulationWin(t *testing.T) { |
| 49 |
var pcpu, tPcpu float64 |
| 50 |
var rss, vss, tRss int64 |
| 51 |
|
| 52 |
runtime.GC() |
| 53 |
|
| 54 |
if err := ProcUsage(&pcpu, &rss, &vss); err != nil { |
| 55 |
t.Fatalf("Error: %v", err) |
| 56 |
} |
| 57 |
|
| 58 |
runtime.GC() |
| 59 |
|
| 60 |
imageName := getProcessImageName() |
| 61 |
// query the counters using typeperf |
| 62 |
out, err := exec.Command("typeperf.exe", |
| 63 |
fmt.Sprintf("\\Process(%s)\\%% Processor Time", imageName), |
| 64 |
fmt.Sprintf("\\Process(%s)\\Working Set - Private", imageName), |
| 65 |
fmt.Sprintf("\\Process(%s)\\Virtual Bytes", imageName), |
| 66 |
"-sc", "1").Output() |
| 67 |
if err != nil { |
| 68 |
t.Fatal("unable to run command", err) |
| 69 |
} |
| 70 |
|
| 71 |
// parse out results - refer to comments in procUsage for detail |
| 72 |
results := strings.Split(string(out), "\r\n") |
| 73 |
values := strings.Split(results[2], ",") |
| 74 |
|
| 75 |
// parse pcpu |
| 76 |
tPcpu, err = strconv.ParseFloat(strings.Trim(values[1], "\""), 64) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("Unable to parse percent cpu: %s", values[1]) |
| 79 |
} |
| 80 |
|
| 81 |
// parse private bytes (rss) |
| 82 |
fval, err := strconv.ParseFloat(strings.Trim(values[2], "\""), 64) |
| 83 |
if err != nil { |
| 84 |
t.Fatalf("Unable to parse private bytes: %s", values[2]) |
| 85 |
} |
| 86 |
tRss = int64(fval) |
| 87 |
|
| 88 |
checkValues(t, pcpu, tPcpu, rss, tRss) |
| 89 |
|
| 90 |
runtime.GC() |
| 91 |
|
| 92 |
// Again to test caching |
| 93 |
if err = ProcUsage(&pcpu, &rss, &vss); err != nil { |
| 94 |
t.Fatalf("Error: %v", err) |
| 95 |
} |
| 96 |
checkValues(t, pcpu, tPcpu, rss, tRss) |
| 97 |
} |