压力测试golang小demo

package main

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

func main() {
    url := "https://www.baidu.com/" // 要测试的URL
    numRequests := 99999999999999          // 总请求数
    numConcurrent := 99999999         // 并发请求数

    var wg sync.WaitGroup
    wg.Add(numRequests)

    client := http.Client{
        Timeout: time.Second * 10, // 请求超时时间
    }

    for i := 0; i < numConcurrent; i++ {
        go func() {
            for j := 0; j < numRequests/numConcurrent; j++ {
                start := time.Now()
                resp, err := client.Get(url)
                if err != nil {
                    fmt.Println("Error:", err)
                    continue
                }
                defer resp.Body.Close()

                duration := time.Since(start)
                fmt.Printf("Status: %d, Response time: %v\n", resp.StatusCode, duration)
                wg.Done()
            }
        }()
    }

    wg.Wait()
    fmt.Println("Done!")
}

你可能感兴趣的:(golang)