Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net/http"
- "sync"
- "time"
- )
- func main() {
- numeroRichieste := 1000
- start := time.Now()
- // for i := 0; i < numeroRichieste; i++ {
- // resp, err := http.Get("https://httpbin.org/delay/1")
- // if err != nil {
- // fmt.Println("Errore:", err)
- // continue
- // }
- // resp.Body.Close()
- // fmt.Printf("Richiesta %d completata\n", i+1)
- // }
- elapsed := time.Since(start)
- //fmt.Printf("Tempo totale senza goroutine: %s\n", elapsed)
- start = time.Now()
- var wg sync.WaitGroup
- wg.Add(numeroRichieste)
- contatore := 0
- var mu sync.Mutex
- for i := 0; i < numeroRichieste; i++ {
- go func(i int) {
- defer wg.Done()
- mu.Lock()
- contatore++
- mu.Unlock()
- resp, err := http.Get("https://httpbin.org/delay/1")
- if err != nil {
- fmt.Printf("Errore nella richiesta %d: %v\n", i+1, err)
- return
- }
- resp.Body.Close()
- //fmt.Printf("Richiesta %d completata\n", i+1)
- }(i)
- }
- wg.Wait()
- elapsed = time.Since(start)
- fmt.Printf("Tempo totale con goroutine: %s\n", elapsed)
- fmt.Println("Contatore ", contatore)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement