-
Couldn't load subscription status.
- Fork 256
[QE]monitor cpu usage in e2e test case #4977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lilyLuLiu
wants to merge
1
commit into
crc-org:main
Choose a base branch
from
lilyLuLiu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−33
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package testsuite | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/crc-org/crc/v2/test/extended/util" | ||
| "github.com/shirou/gopsutil/v4/cpu" | ||
| ) | ||
|
|
||
| type Monitor struct { | ||
| cancelFunc context.CancelFunc | ||
| isRunning bool | ||
| mu sync.Mutex | ||
| wg sync.WaitGroup | ||
| interval time.Duration | ||
| } | ||
|
|
||
| func NewMonitor(interval time.Duration) *Monitor { | ||
| return &Monitor{ | ||
| interval: interval, | ||
| } | ||
| } | ||
|
|
||
| func (m *Monitor) Start() error { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
|
|
||
| if m.isRunning { | ||
| return fmt.Errorf("The collector is running") | ||
| } | ||
|
|
||
| fmt.Printf("Attempt to start CPU collector, interval: %s", m.interval) | ||
|
|
||
| // create a context.WithCancel | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| m.cancelFunc = cancel | ||
| m.isRunning = true | ||
|
|
||
| // stary goroutine | ||
| m.wg.Add(1) | ||
| go m.collectLoop(ctx) | ||
|
|
||
| fmt.Println("CPU collector has been successfully started") | ||
| return nil | ||
| } | ||
|
|
||
| func (m *Monitor) Stop() error { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
|
|
||
| if !m.isRunning { | ||
| return fmt.Errorf("The collector is not running") | ||
| } | ||
| if m.cancelFunc != nil { | ||
| m.cancelFunc() | ||
| } | ||
| m.isRunning = false | ||
| m.wg.Wait() | ||
| fmt.Println("CPU collector has sent a stop signal") | ||
| // may need wait a while to stop | ||
| return nil | ||
| } | ||
|
|
||
| func (m *Monitor) collectLoop(ctx context.Context) { | ||
| defer m.wg.Done() | ||
|
|
||
| fmt.Println("--> collect goroutine start...") | ||
| calcInterval := m.interval | ||
|
|
||
| for { | ||
| // 1. check Context whether be cancled | ||
| select { | ||
| case <-ctx.Done(): | ||
| fmt.Println("<-- collect goroutine receive stop signal") | ||
| return // exit goroutine | ||
| default: | ||
| // continue collect data | ||
| } | ||
|
|
||
| // 2. collect data | ||
| totalPercent, err := cpu.Percent(calcInterval, false) | ||
| // no need to sleep, calcInterval automatically do it | ||
|
|
||
| if err != nil { | ||
| fmt.Printf("Error: fail to collect CPU data: %v", err) | ||
| time.Sleep(1 * time.Second) | ||
| continue | ||
| } | ||
|
|
||
| if len(totalPercent) > 0 { | ||
| data := fmt.Sprintf("[%s], cpu percent: %.2f%%\n", | ||
| time.Now().Format("15:04:05"), totalPercent[0]) | ||
| wd, _ := os.Getwd() | ||
| file := filepath.Join(wd, "../test-results/cpu-consume.txt") | ||
| err := util.WriteToFile(data, file) | ||
| if err != nil { | ||
| fmt.Printf("Error: fail to write to %s: %v", file, err) | ||
| } | ||
| } | ||
| /* | ||
| vMem, err := mem.VirtualMemory() | ||
| if err != nil { | ||
| fmt.Printf("Error: failed to collect memory data: %v", err) | ||
| continue | ||
| } | ||
| memoryused := vMem.Used / 1024 / 1024 | ||
| data := fmt.Sprintf("[%s], MemoryUsed(MB): %d\n" , | ||
| time.Now().Format("15:04:05"), memoryused) | ||
| wd, _ := os.Getwd() | ||
| file := filepath.Join(wd, "../test-results/memory-consume.txt") | ||
| util.WriteToFile(data, file) | ||
| */ | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.