Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (88 loc) · 2.85 KB
/
Copy pathmain.go
File metadata and controls
107 lines (88 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main
import (
"log"
"os"
"sync"
"time"
"github.com/gogits/cron"
"github.com/iuriikogan/k8s-file-churner/config"
"github.com/iuriikogan/k8s-file-churner/utils"
"github.com/spf13/cobra"
_ "go.uber.org/automaxprocs"
)
var rootCmd = &cobra.Command{
Use: "k8s-file-churner",
Short: "K8s File Churner",
Run: runCommand,
}
func runCommand(cmd *cobra.Command, args []string) {
// start the timer
start := time.Now()
// Load the config
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err.Error())
}
// Create the app/testfiles directory
err = os.MkdirAll("app/testfiles", 0755)
if err != nil {
log.Fatal(err.Error())
}
// typecast ChurnIntervalMinutes to time.Duration
printChurnInterval := time.Duration.Minutes(cfg.ChurnIntervalMinutes)
log.Printf("Churn interval in minutes: %v\n", printChurnInterval)
// log stuff
log.Printf("************************************\nK8s File Churner was made by Iurii Kogan - koganiurii@gmail.com \n************************************\n")
log.Println("Starting K8s File Churner...\nAll testfiles will be written to app/testfiles directory")
log.Printf("Size of each file in Mb: %d\n", cfg.SizeOfFileMB)
log.Printf("Size of PVC in Gb: %d\n", cfg.SizeOfPVCGB)
log.Printf("Churn percentage: %v\n", (cfg.ChurnPercentage * 100))
log.Printf("Churn interval in minutes: %v\n", printChurnInterval)
// calculate number of files to create
sizeOfPVCMB := int(cfg.SizeOfPVCGB * 999)
numberOfFiles := ((sizeOfPVCMB) / (cfg.SizeOfFileMB))
log.Printf("Number of files to create: %d\n", numberOfFiles)
fileSizeBytes := int(cfg.SizeOfFileMB * 1024 * 1024)
// start creating the files
var wg sync.WaitGroup
wg.Add(numberOfFiles)
c := cron.New()
c.AddFunc("@every", "2m", func() {
log.Println("waiting for files to be created")
})
c.Start()
// Launch a goroutine for each file creation
for i := 0; i < numberOfFiles; i++ {
go utils.CreateFileWithRandomData(fileSizeBytes, i, &wg)
}
// Wait for all the goroutines to finish
wg.Wait()
c.Stop()
// once all files are created, set the live probe
utils.Live()
// log the number of files created, their size and the time it took
log.Printf("Created %v files of size %vMb\nTook %s\n", numberOfFiles, cfg.SizeOfFileMB, time.Since(start))
// start churning the files
churnTicker := time.NewTicker(cfg.ChurnIntervalMinutes)
go func() {
log.Printf("Churning %v percent of files every %v", (cfg.ChurnPercentage * 100), printChurnInterval)
for {
select {
case <-churnTicker.C:
utils.ChurnFiles(cfg.ChurnPercentage, fileSizeBytes, &wg)
case <-time.After(120 * time.Second):
log.Println("Waiting to churn files")
}
}
}()
// this is a hack to keep the program running until interrupted
<-make(chan struct{})
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}