Skip to content

Instantly share code, notes, and snippets.

@yusufpapurcu
Created November 28, 2021 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusufpapurcu/55b405db2df43acc9625c22d6c856163 to your computer and use it in GitHub Desktop.
Save yusufpapurcu/55b405db2df43acc9625c22d6c856163 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
func main() {
LinqTest()
fmt.Println("--------")
LinqTest()
fmt.Println("--------")
LinqTest()
fmt.Println("--------")
LinqTest()
fmt.Println("--------")
LinqTest()
}
func LinqTest() {
start := time.Now()
// Stats Holder
statsHolder := make([]Stats, 0, 11664000)
// Reset Epoch Time
epochStart := 1609459200 // 01.01.2021 00:00
// Lets generate random metrics:
// Loop through Hypervisors
for hv := 0; hv < 3; hv++ {
// Loop through VMs
for vm := 0; vm < 6; vm++ {
// Set Identifier
identifier := "windows"
if vm%2 == 0 {
identifier = "linux"
}
// Loop through metrics
for metric := 0; metric < 10; metric++ {
// Loop through timestamps
for timestamp := 0; timestamp < 32400; timestamp++ {
statsHolder = append(statsHolder, Stats{
Epoch: int64(epochStart + timestamp),
Identifier: identifier,
DeviceID: hv + 1,
DeviceType: "server",
ChildID: vm + 1,
ChildType: "vm",
MetricID: metric + 1,
Value: float64(rand.Intn(20) + 10),
MaxValue: float64(rand.Intn(80) + 40),
})
}
}
}
}
// Story
// Lets also add same logic for containers here.
// We will see how we can seperate VMs and Containers
// Reset Epoch Time
epochStart = 1609459200 // 01.01.2021 00:00
// Lets generate random metrics:
// Loop through Hypervisors
for hv := 0; hv < 3; hv++ {
// Loop through VMs
for vm := 0; vm < 6; vm++ {
// Set Identifier
identifier := "windows"
if vm%2 == 0 {
identifier = "linux"
}
// Loop through metrics
for metric := 0; metric < 10; metric++ {
// Loop through timestamps
for timestamp := 0; timestamp < 32400; timestamp++ {
statsHolder = append(statsHolder, Stats{
Epoch: int64(epochStart + timestamp),
Identifier: identifier,
DeviceID: hv + 1,
DeviceType: "node",
ChildID: vm + 1,
ChildType: "container",
MetricID: metric + 1,
Value: float64(rand.Intn(20) + 10),
MaxValue: float64(rand.Intn(80) + 40),
})
}
}
}
}
elapsed := time.Since(start)
fmt.Println(elapsed)
}
func getAggregatedEpoch(epoch int64) int64 {
fark := epoch % 3600
if fark > 0 {
return epoch - fark
}
return epoch
}
// Stats Model
// We will store performance metrics in this model
type Stats struct {
Epoch int64 // timestamp, in Unix format
Identifier string // unique identifier of metric's owner, for example OS of VM: windows/linux
DeviceID int // device id of owner, for example hypervisor id in VirtualMetric: 6
DeviceType string // device type of owner, for example: server
ChildID int // child id of owner, for example id of a VM: 12
ChildType string // child type of owner, for example: vm
MetricID int // metric id, for example an id for cpu usage: 700
Value float64 // metric's average value of 20 seconds
MaxValue float64 // metric's max value in 20 seconds
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment