send 10000 request to a website in 1 sec in go

To send 10000 requests to a website in 1 second, we can take advantage of Go's concurrency features. Specifically, we will use goroutines and channels to make concurrent HTTP requests.

Here's an example code that demonstrates this approach:

main.go
package main

import (
	"fmt"
	"net/http"
	"sync"
	"time"
)

func main() {
	url := "http://example.com"
	numRequests := 10000
	numConcurrency := 1000

	var waitGroup sync.WaitGroup
	waitGroup.Add(numRequests)

	transport := &http.Transport{
		MaxIdleConns:        numConcurrency,
		MaxConnsPerHost:     numConcurrency,
		MaxIdleConnsPerHost: numConcurrency,
	}
	client := &http.Client{
		Transport: transport,
	}

	for i := 0; i < numRequests; i++ {
		go func() {
			defer waitGroup.Done()

			req, err := http.NewRequest("GET", url, nil)
			if err != nil {
				fmt.Println("Error making request:", err)
				return
			}

			resp, err := client.Do(req)
			if err != nil {
				fmt.Println("Error in response:", err)
				return
			}

			// Consume the response body to reuse the connection
			_, err = io.Copy(ioutil.Discard, resp.Body)
			if err != nil {
				fmt.Println("Error consuming response body:", err)
			}
			defer resp.Body.Close()
		}()
	}

	startTime := time.Now()
	waitGroup.Wait()
	duration := time.Since(startTime)

	fmt.Printf("Sent %d requests in %s\n", numRequests, duration)
	fmt.Printf("Requests per second: %.2f\n", float64(numRequests)/duration.Seconds())
}
1178 chars
59 lines

In this code, we create a http.Client with a custom transport that allows multiple idle connections and limits the maximum number of connections per host. We also create a sync.WaitGroup to wait for all requests to complete.

We then loop over the number of requests we want to send, and call go func() to create a new goroutine for each request. In each goroutine, we create a new HTTP request and send it using the http.Client.

Once all requests are sent, we wait for them to complete using the sync.WaitGroup.

Finally, we calculate the duration of the entire operation and print some statistics, such as the total number of requests sent and the requests per second.

gistlibby LogSnag