...

Source file src/edge-infra.dev/hack/sds/memoryload/main.go

Documentation: edge-infra.dev/hack/sds/memoryload

     1  package main
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"math"
     7  	"os"
     8  	"strconv"
     9  	"time"
    10  
    11  	"edge-infra.dev/pkg/lib/fog"
    12  )
    13  
    14  // use up memory for testing
    15  // from command line e.g.
    16  // MEMORYLOAD=3000 go run hack/sds/memoryload/main.go
    17  func main() {
    18  	log := fog.New().WithName("memoryload")
    19  	// Check if the user provided an argument for the size
    20  	size := os.Getenv("MEMORYLOAD")
    21  	if len(size) < 1 {
    22  		err := fmt.Errorf("invalid arguments")
    23  		log.Error(err, "please provide MEMORYLOAD as ENV variable in MB")
    24  		return
    25  	}
    26  
    27  	// Convert the argument to an integer
    28  	sizeInMB, err := strconv.Atoi(size)
    29  	if err != nil {
    30  		err := fmt.Errorf("invalid arguments")
    31  		log.Error(err, "valid number of Mb not provided")
    32  		return
    33  	}
    34  
    35  	// Calculate the size in bytes
    36  	sizeInBytes := sizeInMB * 1024 * 1024
    37  
    38  	// Allocate memory
    39  	data := make([]byte, sizeInBytes)
    40  
    41  	log.Info("allocating memory", "size (bytes)", sizeInBytes)
    42  	// Fill the allocated memory with random data
    43  	stuff, err := rand.Read(data)
    44  	if err != nil {
    45  		log.Error(err, "unable to generated random data")
    46  		return
    47  	}
    48  
    49  	log.Info("successfully filled memory", "bytes", stuff)
    50  
    51  	// Prevent the program from exiting immediately
    52  	time.Sleep(math.MaxInt64)
    53  }
    54  

View as plain text