github.com/KEINOS/go-countline@v1.1.1-0.20221217083629-60710df7606b/cl/_gen/gen_test_data.go (about) 1 // This creates a file of 1 GiB in size with lines of random values. 2 // 3 //nolint:gochecknoglobals,forbidigo 4 package main 5 6 import ( 7 "bufio" 8 "fmt" 9 "os" 10 "path/filepath" 11 12 "github.com/pkg/errors" 13 ) 14 15 // DataSizes defines the data sizes to generate. 16 // 17 //nolint:gomnd 18 var DataSizes = []struct { 19 Name string 20 Size int 21 }{ 22 {Name: "Tiny", Size: 1024}, // 1KiB 23 {Name: "Small", Size: 1024 * 1024}, // 1MiB 24 {Name: "Medium", Size: 1024 * 1024 * 10}, // 10MiB 25 {Name: "Large", Size: 1024 * 1024 * 50}, // 50MiB 26 {Name: "Huge", Size: 1024 * 1024 * 100}, // 100MiB 27 {Name: "Giant", Size: 1024 * 1024 * 1024}, // 1GiB 28 } 29 30 func main() { 31 fmt.Printf("Generating consistent data file:\n") 32 33 err := genFiles(filepath.Join(".", "testdata")) 34 exitOnError(err) 35 } 36 37 var OsExit = os.Exit 38 39 func exitOnError(err error) { 40 if err != nil { 41 fmt.Fprint(os.Stderr, err) 42 OsExit(1) 43 } 44 } 45 46 func genFiles(pathDirBase string) error { 47 for _, data := range DataSizes { 48 dataSize := data.Size 49 nameFile := "data_" + data.Name + ".txt" 50 pathFile := filepath.Join(pathDirBase, nameFile) 51 52 // Skip if file already exists 53 if infoFile, err := os.Stat(pathFile); err == nil && infoFile.Size() >= int64(dataSize) { 54 fmt.Printf(" - %s ... OK (exits)\n", pathFile) 55 56 continue 57 } 58 59 if err := genFile(dataSize, pathFile); err != nil { 60 return errors.Wrap(err, "failed to generate file") 61 } 62 } 63 64 return nil 65 } 66 67 // forceFailWraite forces the writer to fail if true. It is only used for testing as 68 // a dependency injection. 69 var forceFailWraite = false 70 71 func genFile(sizeFile int, pathFile string) error { 72 fmt.Printf(" - %s ...\r", pathFile) 73 74 fileP, err := os.Create(pathFile) 75 if err != nil { 76 return errors.Wrap(err, "failed to open/create file") 77 } 78 79 defer fileP.Close() 80 81 bufP := bufio.NewWriter(fileP) 82 defer bufP.Flush() 83 84 totalSize := int64(0) 85 countLine := 0 86 87 for { 88 countLine++ 89 90 written, err := bufP.WriteString(fmt.Sprintf("line: %d\n", countLine)) 91 if err != nil || forceFailWraite { 92 if forceFailWraite { 93 err = errors.New("forced error") 94 } 95 96 return errors.Wrap(err, "failed to write line") 97 } 98 99 totalSize += int64(written) 100 101 if totalSize >= int64(sizeFile) { 102 fmt.Printf(" - %s, size: %d, line: %d ... OK\n", pathFile, totalSize, countLine) 103 104 break 105 } 106 107 if totalSize%1024 == 0 && !IsCI() { 108 fmt.Printf(" - %s, size: %d, line: %d\r", pathFile, totalSize, countLine) 109 } 110 } 111 112 return nil 113 } 114 115 var pathDockerEnv = filepath.Join("/", ".dockerenv") 116 117 // IsCI returns true if the current process is running inside a CI environment. Such as Github Actions or Docker. 118 func IsCI() bool { 119 return IsGithubActions() || IsDocker() 120 } 121 122 // IsDocker returns true if the current process is running inside a Docker container. 123 func IsDocker() bool { 124 _, err := os.Stat(pathDockerEnv) 125 126 return err == nil 127 } 128 129 // IsGithubActions returns true if the current process is running inside a Github Actions. 130 func IsGithubActions() bool { 131 return os.Getenv("GITHUB_ACTIONS") != "" 132 }