Skip to content

Commit

Permalink
Adding flag to instead write random data.
Browse files Browse the repository at this point in the history
  • Loading branch information
strategicpause committed Nov 19, 2023
1 parent ea48ada commit 2f95cae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 12 additions & 4 deletions command/memory/leak.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memory

import (
"crypto/rand"
"fmt"
"github.com/strategicpause/memory-leak/metrics"
"time"
Expand All @@ -11,6 +12,7 @@ type Params struct {
BlockSizeInBytes uint64
StepTimeInSeconds time.Duration
PauseTimeInSeconds time.Duration
RandomData bool
}

func memoryLeak(params *Params) error {
Expand All @@ -21,8 +23,13 @@ func memoryLeak(params *Params) error {

for i := 0; i < numEntries; i++ {
list[i] = make([]byte, params.BlockSizeInBytes)
for j := 0; j < int(params.BlockSizeInBytes); j++ {
list[i][j] = 0
if params.RandomData {
_, _ = rand.Read(list[i])
} else {
// This will result in allocating memory from the virtual address space.
for j := 0; j < int(params.BlockSizeInBytes); j++ {
list[i][j] = 0
}
}
metrics.PrintMemory()
time.Sleep(params.StepTimeInSeconds)
Expand All @@ -36,6 +43,7 @@ func memoryLeak(params *Params) error {
}

func PrintParams(params *Params) {
fmt.Printf("MaxMemory = %v MiB\tBlockSize = %v MiB\tPauseTime = %v.\n",
metrics.BToMiB(params.MaxMemoryInBytes), metrics.BToMiB(params.BlockSizeInBytes), params.PauseTimeInSeconds)
fmt.Printf("MaxMemory = %v MiB\tBlockSize = %v MiB\tPauseTime = %v\tRandomData = %t.\n",
metrics.BToMiB(params.MaxMemoryInBytes), metrics.BToMiB(params.BlockSizeInBytes),
params.PauseTimeInSeconds, params.RandomData)
}
13 changes: 9 additions & 4 deletions command/memory/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

const (
MaxMemoryName = "max-memory"
BlockSizeName = "block-size"
StepDurationName = "step-duration"
PauseDurationName = "pause-duration"
MaxMemoryName = "max-memory"
BlockSizeName = "block-size"
StepDurationName = "step-duration"
PauseDurationName = "pause-duration"
WithRandomDataName = "with-random-data"
)

func Register() cli.Command {
Expand Down Expand Up @@ -44,6 +45,9 @@ func flags() []cli.Flag {
Usage: "Time to wait, in seconds, after all memory has been allocated.",
Value: 0,
},
cli.BoolFlag{
Name: WithRandomDataName,
},
}
}

Expand All @@ -67,6 +71,7 @@ func action(ctx *cli.Context) error {
BlockSizeInBytes: uint64(blockSize),
StepTimeInSeconds: ctx.Duration(StepDurationName),
PauseTimeInSeconds: ctx.Duration(PauseDurationName),
RandomData: ctx.Bool(WithRandomDataName),
}

return memoryLeak(params)
Expand Down

0 comments on commit 2f95cae

Please sign in to comment.