github.com/google/cadvisor@v0.49.1/info/v1/test/datagen.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	"fmt"
    19  	"math/rand"
    20  	"time"
    21  
    22  	info "github.com/google/cadvisor/info/v1"
    23  )
    24  
    25  func GenerateRandomStats(numStats, numCores int, duration time.Duration) []*info.ContainerStats {
    26  	ret := make([]*info.ContainerStats, numStats)
    27  	perCoreUsages := make([]uint64, numCores)
    28  	currentTime := time.Now()
    29  	for i := range perCoreUsages {
    30  		perCoreUsages[i] = uint64(rand.Int63n(1000))
    31  	}
    32  	for i := 0; i < numStats; i++ {
    33  		stats := new(info.ContainerStats)
    34  		stats.Timestamp = currentTime
    35  		currentTime = currentTime.Add(duration)
    36  
    37  		percore := make([]uint64, numCores)
    38  		for i := range perCoreUsages {
    39  			perCoreUsages[i] += uint64(rand.Int63n(1000))
    40  			percore[i] = perCoreUsages[i]
    41  			stats.Cpu.Usage.Total += percore[i]
    42  		}
    43  		stats.Cpu.Usage.PerCpu = percore
    44  		stats.Cpu.Usage.User = stats.Cpu.Usage.Total
    45  		stats.Cpu.Usage.System = 0
    46  		stats.Memory.Usage = uint64(rand.Int63n(4096))
    47  		stats.Memory.Cache = uint64(rand.Int63n(4096))
    48  		stats.Memory.RSS = uint64(rand.Int63n(4096))
    49  		stats.Memory.MappedFile = uint64(rand.Int63n(4096))
    50  		stats.Memory.KernelUsage = uint64(rand.Int63n(4096))
    51  		stats.ReferencedMemory = uint64(rand.Int63n(1000))
    52  		ret[i] = stats
    53  	}
    54  	return ret
    55  }
    56  
    57  func GenerateRandomContainerSpec(numCores int) info.ContainerSpec {
    58  	ret := info.ContainerSpec{
    59  		CreationTime: time.Now(),
    60  		HasCpu:       true,
    61  		Cpu:          info.CpuSpec{},
    62  		HasMemory:    true,
    63  		Memory:       info.MemorySpec{},
    64  	}
    65  	ret.Cpu.Limit = uint64(1000 + rand.Int63n(2000))
    66  	ret.Cpu.MaxLimit = uint64(1000 + rand.Int63n(2000))
    67  	ret.Cpu.Mask = fmt.Sprintf("0-%d", numCores-1)
    68  	ret.Memory.Limit = uint64(4096 + rand.Int63n(4096))
    69  	return ret
    70  }
    71  
    72  func GenerateRandomContainerInfo(containerName string, numCores int, query *info.ContainerInfoRequest, duration time.Duration) *info.ContainerInfo {
    73  	stats := GenerateRandomStats(query.NumStats, numCores, duration)
    74  	spec := GenerateRandomContainerSpec(numCores)
    75  
    76  	ret := &info.ContainerInfo{
    77  		ContainerReference: info.ContainerReference{
    78  			Name: containerName,
    79  		},
    80  		Spec:  spec,
    81  		Stats: stats,
    82  	}
    83  	return ret
    84  }