"LogRoutine"의 두 판 사이의 차이

(새 문서: ==개요== ;LogGen <syntaxhighlight lang='go'> package main import ( "fmt" "os" "strconv" "sync" "time" "github.com/google/uuid" ) func generateLog(wg *sync.WaitGroup, batch...)
 
15번째 줄: 15번째 줄:
)
)


func generateLog(wg *sync.WaitGroup, batchID string, logNumber int) {
func generateLog(wg *sync.WaitGroup, batchID string, logIndex int) {
defer wg.Done()
defer wg.Done()
fmt.Printf("LogGen %s %d\n", batchID, logNumber)
fmt.Printf("LogGen BatchID: %s, LogIndex: %d\n", batchID, logIndex)
}
}


24번째 줄: 24번째 줄:
var wg sync.WaitGroup
var wg sync.WaitGroup


// Set default number of logs
numLogs := 10
numLogs := 10
// Check if NUM_LOGS environment variable is set and parse it
if envNumLogs, exists := os.LookupEnv("NUM_LOGS"); exists {
if envNumLogs, exists := os.LookupEnv("NUM_LOGS"); exists {
if parsedNumLogs, err := strconv.Atoi(envNumLogs); err == nil {
if parsedNumLogs, err := strconv.Atoi(envNumLogs); err == nil {
37번째 줄: 34번째 줄:


batchID := uuid.New().String()[:8]
batchID := uuid.New().String()[:8]
fmt.Printf("LogGen start: %s\n", startTime.Format(time.RFC3339))


fmt.Printf("LogGen start: %s\n", startTime.Format(time.RFC3339))
for i := 1; i <= numLogs; i++ {
for i := 1; i <= numLogs; i++ {
wg.Add(1)
wg.Add(1)
go generateLog(&wg, batchID, i)
go generateLog(&wg, batchID, i)
}
}
// Wait for all log generation goroutines to finish
wg.Wait()
wg.Wait()


// Calculate the elapsed time
elapsedTime := time.Since(startTime)
elapsedTime := time.Since(startTime)
// Print the end time and the elapsed time
fmt.Printf("LogGen end: %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("LogGen end: %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("Elapsed Time: %s\n", elapsedTime)
fmt.Printf("LogGen Elapsed Time: %s\n", elapsedTime)
}
}
</syntaxhighlight>
</syntaxhighlight>


[[분류: Go]]
[[분류: Go]]

2024년 8월 12일 (월) 23:27 판

개요

LogGen
package main

import (
	"fmt"
	"os"
	"strconv"
	"sync"
	"time"

	"github.com/google/uuid"
)

func generateLog(wg *sync.WaitGroup, batchID string, logIndex int) {
	defer wg.Done()
	fmt.Printf("LogGen BatchID: %s, LogIndex: %d\n", batchID, logIndex)
}

func main() {
	startTime := time.Now()
	var wg sync.WaitGroup

	numLogs := 10
	if envNumLogs, exists := os.LookupEnv("NUM_LOGS"); exists {
		if parsedNumLogs, err := strconv.Atoi(envNumLogs); err == nil {
			numLogs = parsedNumLogs
		} else {
			fmt.Printf("Invalid NUM_LOGS value, using default: %d\n", numLogs)
		}
	}

	batchID := uuid.New().String()[:8]
	fmt.Printf("LogGen start: %s\n", startTime.Format(time.RFC3339))

	for i := 1; i <= numLogs; i++ {
		wg.Add(1)
		go generateLog(&wg, batchID, i)
	}
	wg.Wait()

	elapsedTime := time.Since(startTime)
	fmt.Printf("LogGen end: %s\n", time.Now().Format(time.RFC3339))
	fmt.Printf("LogGen Elapsed Time: %s\n", elapsedTime)
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}