"LogRoutine"의 두 판 사이의 차이

(새 문서: ==개요== ;LogGen <syntaxhighlight lang='go'> package main import ( "fmt" "os" "strconv" "sync" "time" "github.com/google/uuid" ) func generateLog(wg *sync.WaitGroup, batch...)
 
 
(같은 사용자의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;LogGen
;LogRoutine


<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
14번째 줄: 14번째 줄:
"github.com/google/uuid"
"github.com/google/uuid"
)
)
func generateLog(wg *sync.WaitGroup, batchID string, logNumber int) {
defer wg.Done()
fmt.Printf("LogGen %s %d\n", batchID, logNumber)
}


func main() {
func main() {
startTime := time.Now()
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 {
35번째 줄: 24번째 줄:
}
}
}
}
prefix := fmt.Sprintf("LogRoutine %s", uuid.New().String()[:8])


batchID := uuid.New().String()[:8]
var wg sync.WaitGroup
 
startTime := time.Now()
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 func(index int) {
defer wg.Done()
fmt.Println(prefix, index)
}(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.Println("LogRoutine elapsed", elapsedTime)
fmt.Printf("LogGen end: %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("Elapsed Time: %s\n", elapsedTime)
}
}
</syntaxhighlight>
</syntaxhighlight>


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

2024년 8월 12일 (월) 23:49 기준 최신판

개요[ | ]

LogRoutine
package main

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

	"github.com/google/uuid"
)

func main() {
	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)
		}
	}
	prefix := fmt.Sprintf("LogRoutine %s", uuid.New().String()[:8])

	var wg sync.WaitGroup
	startTime := time.Now()
	for i := 1; i <= numLogs; i++ {
		wg.Add(1)
		go func(index int) {
			defer wg.Done()
			fmt.Println(prefix, index)
		}(i)
	}
	wg.Wait()
	elapsedTime := time.Since(startTime)

	fmt.Println("LogRoutine elapsed", elapsedTime)
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}