k8s.io/kubernetes@v1.29.3/test/images/resource-consumer/utils_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2021 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package main
    21  
    22  import (
    23  	"log"
    24  	"os/exec"
    25  	"strconv"
    26  	"time"
    27  )
    28  
    29  var (
    30  	consumeCPUBinary = "./consume-cpu/consume-cpu.exe"
    31  	consumeMemBinary = "testlimit.exe"
    32  )
    33  
    34  // ConsumeMem consumes a given number of megabytes for the specified duration.
    35  func ConsumeMem(megabytes int, durationSec int) {
    36  	log.Printf("ConsumeMem megabytes: %v, durationSec: %v", megabytes, durationSec)
    37  	megabytesString := strconv.Itoa(megabytes)
    38  	durationSecString := strconv.Itoa(durationSec)
    39  	// creating new consume memory process
    40  	consumeMem := exec.Command(consumeMemBinary, "-accepteula", "-d", megabytesString, "-e", "0", durationSecString, "-c", "1")
    41  	err := consumeMem.Start()
    42  	if err != nil {
    43  		log.Printf("Error while consuming memory: %v", err)
    44  		return
    45  	}
    46  
    47  	// testlimit does not end after durationSec elapsed, so we'll stop it ourselves.
    48  	time.AfterFunc(time.Duration(durationSec)*time.Second, func() {
    49  		if err := consumeMem.Process.Kill(); err != nil {
    50  			log.Printf("Could not kill testlimit process! Error: %v", err)
    51  		}
    52  	})
    53  }