Skip to content

Commit 6b5199c

Browse files
committed
[QE]monitor cpu usage in e2e test case
1 parent 8d2c93c commit 6b5199c

File tree

4 files changed

+149
-32
lines changed

4 files changed

+149
-32
lines changed

test/e2e/features/story_microshift.feature

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ Feature: Microshift test stories
77
And setting config property "persistent-volume-size" to value "20" succeeds
88
And ensuring network mode user
99
And executing single crc setup command succeeds
10-
And get cpu data "Before start"
1110
And get memory data "Before start"
11+
And record timestamp "start"
1212
And starting CRC with default bundle succeeds
13-
And get cpu data "After start"
1413
And get memory data "After start"
1514
And ensuring oc command is available
1615
And ensuring microshift cluster is fully operational
@@ -22,6 +21,7 @@ Feature: Microshift test stories
2221

2322
@microshift @testdata @linux @windows @darwin @cleanup
2423
Scenario: Start and expose a basic HTTP service and check after restart
24+
And record timestamp "deployment"
2525
Given executing "oc create namespace testproj" succeeds
2626
And executing "oc config set-context --current --namespace=testproj" succeeds
2727
When executing "oc apply -f httpd-example.yaml" succeeds
@@ -40,9 +40,10 @@ Feature: Microshift test stories
4040
And get memory data "After deployment"
4141
Then executing "curl -s http://httpd-example-testproj.apps.crc.testing" succeeds
4242
And stdout should contain "Hello CRC!"
43+
And record timestamp "stop"
4344
When executing "crc stop" succeeds
44-
And get cpu data "After stop"
4545
And get memory data "After stop"
46+
And record timestamp "start again"
4647
And starting CRC with default bundle succeeds
4748
And checking that CRC is running
4849
And with up to "4" retries with wait period of "1m" http response from "http://httpd-example-testproj.apps.crc.testing" has status code "200"

test/e2e/features/story_openshift.feature

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ Feature: 4 Openshift stories
33

44
Background:
55
Given setting config property "disk-size" to value "40" succeeds
6+
And get memory data "Before start"
7+
And record timestamp "start"
68
And ensuring CRC cluster is running
9+
And get memory data "After start"
10+
And record timestamp "deployment"
711
And ensuring oc command is available
812
And ensuring user is logged in succeeds
913

1014
# End-to-end health check
1115

12-
@darwin @linux @windows @testdata @story_health @needs_namespace
16+
@darwin @linux @windows @testdata @story_health @needs_namespace @performance
1317
Scenario: Overall cluster health
1418
Given executing "oc new-project testproj" succeeds
15-
And get cpu data "After start"
16-
And get memory data "After start"
1719
When executing "oc apply -f httpd-example.yaml" succeeds
1820
And executing "oc rollout status deployment httpd-example" succeeds
1921
Then stdout should contain "successfully rolled out"
@@ -25,14 +27,14 @@ Feature: 4 Openshift stories
2527
Then stdout should contain "httpd-example exposed"
2628
When executing "oc expose svc httpd-example" succeeds
2729
Then stdout should contain "httpd-example exposed"
28-
And get cpu data "After deployment"
29-
And get memory data "After deployment"
3030
When with up to "20" retries with wait period of "5s" http response from "http://httpd-example-testproj.apps-crc.testing" has status code "200"
3131
Then executing "curl -s http://httpd-example-testproj.apps-crc.testing" succeeds
3232
And stdout should contain "Hello CRC!"
33+
And get memory data "After deployment"
34+
And record timestamp "stop"
3335
When executing "crc stop" succeeds
34-
And get cpu data "After stop"
3536
And get memory data "After stop"
37+
And record timestamp "start again"
3638
And starting CRC with default bundle succeeds
3739
And checking that CRC is running
3840
And with up to "4" retries with wait period of "1m" http response from "http://httpd-example-testproj.apps-crc.testing" has status code "200"

test/e2e/testsuite/performace.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package testsuite
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
"os"
8+
"path/filepath"
9+
"github.com/crc-org/crc/v2/test/extended/util"
10+
"github.com/shirou/gopsutil/v4/cpu"
11+
//"github.com/shirou/gopsutil/v4/mem"
12+
)
13+
14+
type Monitor struct {
15+
// cancle flag of monitoring
16+
cancelFunc context.CancelFunc
17+
// running flag
18+
isRunning bool
19+
// interval time of gather data
20+
interval time.Duration
21+
}
22+
23+
func NewMonitor(interval time.Duration) *Monitor {
24+
return &Monitor{
25+
interval: interval,
26+
}
27+
}
28+
29+
func (m *Monitor) Start() error {
30+
if m.isRunning {
31+
return fmt.Errorf("The collector is running")
32+
}
33+
34+
fmt.Printf("Attempt to start CPU collector, interval: %s", m.interval)
35+
36+
// create a context.WithCancel
37+
ctx, cancel := context.WithCancel(context.Background())
38+
m.cancelFunc = cancel
39+
m.isRunning = true
40+
41+
// stary goroutine
42+
go m.collectLoop(ctx)
43+
44+
fmt.Println("CPU collector has been successfully started")
45+
return nil
46+
}
47+
48+
func (m *Monitor) Stop() error {
49+
if !m.isRunning {
50+
return fmt.Errorf("The collector is not running")
51+
}
52+
53+
// call cancle function
54+
if m.cancelFunc != nil {
55+
m.cancelFunc()
56+
}
57+
58+
// clear flag
59+
m.isRunning = false
60+
fmt.Println("CPU collector has sent a stop signal")
61+
// may need wait a while to stop
62+
return nil
63+
}
64+
65+
func (m *Monitor) collectLoop(ctx context.Context) {
66+
fmt.Println("--> collect goroutine start...")
67+
calcInterval := m.interval
68+
69+
for {
70+
// 1. check Context whether be cancled
71+
select {
72+
case <-ctx.Done():
73+
fmt.Println("<-- collect goroutine receive stop signal")
74+
return // exit goroutine
75+
default:
76+
// continue collect data
77+
}
78+
79+
// 2. collect data
80+
totalPercent, err := cpu.Percent(calcInterval, false)
81+
// no need to sleep, calcInterval automatically do it
82+
83+
if err != nil {
84+
fmt.Printf("Error: fail to collect CPU data: %v", err)
85+
time.Sleep(1 * time.Second)
86+
continue
87+
}
88+
89+
if len(totalPercent) > 0 {
90+
data := fmt.Sprintf("[%s], cpu percent: %.2f%%\n",
91+
time.Now().Format("15:04:05"), totalPercent[0])
92+
wd, _ := os.Getwd()
93+
file := filepath.Join(wd, "../test-results/cpu-consume.txt")
94+
util.WriteToFile(data, file)
95+
}
96+
/*
97+
vMem, err := mem.VirtualMemory()
98+
if err != nil {
99+
fmt.Printf("Error: failed to collect memory data: %v", err)
100+
continue
101+
}
102+
memoryused := vMem.Used / 1024 / 1024
103+
data := fmt.Sprintf("[%s], MemoryUsed(MB): %d\n" ,
104+
time.Now().Format("15:04:05"), memoryused)
105+
wd, _ := os.Getwd()
106+
file := filepath.Join(wd, "../test-results/memory-consume.txt")
107+
util.WriteToFile(data, file)
108+
*/
109+
}
110+
}
111+

test/e2e/testsuite/testsuite.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
crcCmd "github.com/crc-org/crc/v2/test/extended/crc/cmd"
2929
"github.com/crc-org/crc/v2/test/extended/util"
3030
"github.com/cucumber/godog"
31-
"github.com/shirou/gopsutil/v4/cpu"
3231
"github.com/shirou/gopsutil/v4/mem"
3332
"github.com/spf13/pflag"
3433
)
@@ -169,6 +168,7 @@ func InitializeTestSuite(tctx *godog.TestSuiteContext) {
169168
}
170169

171170
func InitializeScenario(s *godog.ScenarioContext) {
171+
monitor := NewMonitor(1 * time.Second)
172172

173173
s.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
174174

@@ -264,12 +264,9 @@ func InitializeScenario(s *godog.ScenarioContext) {
264264
}
265265
}
266266

267-
if tag.Name == "@story_health" {
268-
if err := getCPUdata("Before start"); err != nil {
269-
fmt.Printf("Failed to collect CPU data: %v\n", err)
270-
}
271-
if err := getMemoryData("Before start"); err != nil {
272-
fmt.Printf("Failed to collect memory data: %v\n", err)
267+
if tag.Name == "@performance" {
268+
if err := monitor.Start(); err != nil {
269+
fmt.Println("Failed to start monitor: %v", err)
273270
}
274271
}
275272
}
@@ -398,6 +395,13 @@ func InitializeScenario(s *godog.ScenarioContext) {
398395
}
399396
}
400397

398+
if tag.Name == "@performance" {
399+
if err := monitor.Stop(); err != nil {
400+
fmt.Println("Failed to stop monitoring: %v", err)
401+
}
402+
fmt.Println("Collection has stopped. Wait for 5 seconds to confirm that the collection task will no longer output data")
403+
time.Sleep(5 * time.Second)
404+
}
401405
}
402406

403407
return ctx, nil
@@ -579,8 +583,8 @@ func InitializeScenario(s *godog.ScenarioContext) {
579583
EnsureApplicationIsAccessibleViaNodePort)
580584
s.Step(`^persistent volume of size "([^"]*)"GB exists$`,
581585
EnsureVMPartitionSizeCorrect)
582-
s.Step(`^get cpu data "([^"]*)"`,
583-
getCPUdata)
586+
s.Step(`^record timestamp "([^"]*)"`,
587+
getTimestamp)
584588
s.Step(`^get memory data "([^"]*)"`,
585589
getMemoryData)
586590

@@ -1320,20 +1324,6 @@ func deserializeListBlockDeviceCommandOutputToExtractPVSize(lsblkOutput string)
13201324
return diskSize - (lvmSize + 1), nil
13211325
}
13221326

1323-
func getCPUdata(content string) error {
1324-
cpuData, err := cpu.Percent(0, false)
1325-
if err != nil {
1326-
return fmt.Errorf("failed to get CPU data: %v", err)
1327-
}
1328-
if len(cpuData) == 0 {
1329-
return fmt.Errorf("no CPU data available")
1330-
}
1331-
data := fmt.Sprintf("%s: %.2f%%\n", content, cpuData)
1332-
wd, _ := os.Getwd()
1333-
file := filepath.Join(wd, "../test-results/cpu-consume.txt")
1334-
return util.WriteToFile(data, file)
1335-
}
1336-
13371327
func getMemoryData(content string) error {
13381328
v, err := mem.VirtualMemory()
13391329
if err != nil {
@@ -1351,3 +1341,16 @@ func getMemoryData(content string) error {
13511341
file := filepath.Join(wd, "../test-results/memory-consume.txt")
13521342
return util.WriteToFile(data, file)
13531343
}
1344+
1345+
1346+
1347+
func getTimestamp(content string) {
1348+
data := fmt.Sprintf("[%s], %s\n" ,
1349+
time.Now().Format("15:04:05"), content)
1350+
wd, err := os.Getwd()
1351+
if err != nil {
1352+
fmt.Println("failed to get working directory: %v", err)
1353+
}
1354+
file := filepath.Join(wd, "../test-results/time-stamp.txt")
1355+
util.WriteToFile(data, file)
1356+
}

0 commit comments

Comments
 (0)