github.com/nats-io/nats-server/v2@v2.11.0-preview.2/server/pse/pse_test.go (about) 1 // Copyright 2015-2018 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 package pse 15 16 import ( 17 "fmt" 18 "os" 19 "os/exec" 20 "runtime" 21 "testing" 22 ) 23 24 func TestPSEmulation(t *testing.T) { 25 if runtime.GOOS == "windows" { 26 t.Skipf("Skipping this test on Windows") 27 } 28 var rss, vss, psRss, psVss int64 29 var pcpu, psPcpu float64 30 31 runtime.GC() 32 33 // PS version first 34 pidStr := fmt.Sprintf("%d", os.Getpid()) 35 out, err := exec.Command("ps", "o", "pcpu=,rss=,vsz=", "-p", pidStr).Output() 36 if err != nil { 37 t.Fatalf("Failed to execute ps command: %v\n", err) 38 } 39 40 fmt.Sscanf(string(out), "%f %d %d", &psPcpu, &psRss, &psVss) 41 psRss *= 1024 // 1k blocks, want bytes. 42 psVss *= 1024 // 1k blocks, want bytes. 43 44 runtime.GC() 45 46 // Our internal version 47 ProcUsage(&pcpu, &rss, &vss) 48 49 if pcpu != psPcpu { 50 delta := int64(pcpu - psPcpu) 51 if delta < 0 { 52 delta = -delta 53 } 54 if delta > 30 { // 30%? 55 t.Fatalf("CPUs did not match close enough: %f vs %f", pcpu, psPcpu) 56 } 57 } 58 if rss != psRss { 59 delta := rss - psRss 60 if delta < 0 { 61 delta = -delta 62 } 63 if delta > 1024*1024 { // 1MB 64 t.Fatalf("RSSs did not match close enough: %d vs %d", rss, psRss) 65 } 66 } 67 }