From 78a9b724ed86d108a80304f2e6a7a8f3526d33a7 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Wed, 30 Jun 2021 16:46:52 +0300 Subject: [PATCH] Upgraded MemoryProfiling api + tests --- apier/v1/core_it_test.go | 73 +++++++++++++++++++++++++++++++++++----- cores/core.go | 3 ++ utils/consts.go | 1 + 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/apier/v1/core_it_test.go b/apier/v1/core_it_test.go index 53d5a5cda..3f6f04184 100644 --- a/apier/v1/core_it_test.go +++ b/apier/v1/core_it_test.go @@ -21,6 +21,7 @@ along with this program. If not, see package v1 import ( + "fmt" "net/rpc" "net/rpc/jsonrpc" "os" @@ -46,18 +47,27 @@ var ( testCoreSv1LoadCofig, testCoreSv1InitDataDB, testCoreSv1InitStorDB, - testCoreSv1StartEngineByExecWithCPUProfiling, - testCoreSv1RPCConn, - testCoreSv1StartCPUProfilingErrorAlreadyStarted, - testCoreSv1Sleep, - testCoreSv1StopCPUProfiling, - testCoreSv1KillEngine, + /* + testCoreSv1StartEngineByExecWithCPUProfiling, + testCoreSv1RPCConn, + testCoreSv1StartCPUProfilingErrorAlreadyStarted, + testCoreSv1Sleep, + testCoreSv1StopCPUProfiling, + testCoreSv1KillEngine, + + */ testCoreSv1StartEngine, testCoreSv1RPCConn, - testCoreSv1StopCPUProfilingBeforeStart, - testCoreSv1StartCPUProfiling, + /* + testCoreSv1StopCPUProfilingBeforeStart, + testCoreSv1StartCPUProfiling, + testCoreSv1Sleep, + testCoreSv1StopCPUProfiling, + + */ + testCoreSv1StartMemoryProfiling, testCoreSv1Sleep, - testCoreSv1StopCPUProfiling, + testCoreSv1StopMemoryProfiling, testCoreSv1KillEngine, } ) @@ -201,6 +211,51 @@ func testCoreSv1StopCPUProfiling(t *testing.T) { } } +func testCoreSv1StartMemoryProfiling(t *testing.T) { + var reply string + args := &utils.MemoryPrf{ + DirPath: argPath, + Interval: 100 * time.Millisecond, + NrFiles: 2, + } + if err := coreV1Rpc.Call(utils.CoreSv1StartMemoryProfiling, + args, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Unexpected reply returned") + } +} + +func testCoreSv1StopMemoryProfiling(t *testing.T) { + var reply string + if err := coreV1Rpc.Call(utils.CoreSv1StopMemoryProfiling, + new(utils.MemoryPrf), &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Unexpected reply returned") + } + + for i := 1; i <= 2; i++ { + file, err := os.Open(path.Join(argPath, fmt.Sprintf("mem%v.prof", i))) + if err != nil { + t.Error(err) + } + defer file.Close() + + //compare the size + size, err := file.Stat() + if err != nil { + t.Error(err) + } else if size.Size() < int64(415) { + t.Errorf("Size of MemoryProfile %v is lower that expected", size.Size()) + } + //after we checked that CPUProfile was made successfully, can delete it + if err := os.Remove(path.Join(argPath, fmt.Sprintf("mem%v.prof", i))); err != nil { + t.Error(err) + } + } +} + func testCoreSv1KillEngine(t *testing.T) { if err := engine.KillEngine(*waitRater); err != nil { t.Error(err) diff --git a/cores/core.go b/cores/core.go index 02832791d..a8f22a591 100644 --- a/cores/core.go +++ b/cores/core.go @@ -131,6 +131,9 @@ func (cS *CoreService) StartMemoryProfiling(args *utils.MemoryPrf) (err error) { return utils.NewErrMandatoryIeMissing("Path") } cS.shdWg.Add(1) + if cS.stopMemPrf == nil { + cS.stopMemPrf = make(chan struct{}) + } go MemProfiling(args.DirPath, args.Interval, args.NrFiles, cS.shdWg, cS.stopMemPrf, cS.shdChan) return } diff --git a/utils/consts.go b/utils/consts.go index 0a0a83bb1..d38bb7e0e 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -1519,6 +1519,7 @@ const ( CoreSv1StartCPUProfiling = "CoreSv1.StartCPUProfiling" CoreSv1StopCPUProfiling = "CoreSv1.StopCPUProfiling" CoreSv1StartMemoryProfiling = "CoreSv1.StartMemoryProfiling" + CoreSv1StopMemoryProfiling = "CoreSv1.StopMemoryProfiling" ) // RouteS APIs