"LogRoutine"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 4개는 보이지 않습니다)
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, logIndex int) {
defer wg.Done()
fmt.Printf("LogGen BatchID: %s, LogIndex: %d\n", batchID, logIndex)
}


func main() {
func main() {
startTime := time.Now()
var wg sync.WaitGroup
numLogs := 10
numLogs := 10
if envNumLogs, exists := os.LookupEnv("NUM_LOGS"); exists {
if envNumLogs, exists := os.LookupEnv("NUM_LOGS"); exists {
32번째 줄: 24번째 줄:
}
}
}
}
prefix := fmt.Sprintf("LogRoutine %s", uuid.New().String()[:8])


batchID := uuid.New().String()[:8]
var wg sync.WaitGroup
fmt.Printf("LogGen start: %s\n", startTime.Format(time.RFC3339))
startTime := time.Now()
 
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)
}
}
wg.Wait()
wg.Wait()
elapsedTime := time.Since(startTime)


elapsedTime := time.Since(startTime)
fmt.Println("LogRoutine elapsed", elapsedTime)
fmt.Printf("LogGen end: %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("LogGen 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 }}