建立TCP连接时不用池

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net"
    "sync"
    "testing"
    "time"
)

func connectToService() interface{} {
    time.Sleep(1 * time.Second)
    return struct{}{}
}

func startNetworkDaemon() *sync.WaitGroup {
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        server, err := net.Listen("tcp", "localhost:8080")
        if err != nil {
            log.Fatalf("cannot listen: %v", err)
        }
        defer server.Close()
        wg.Done()

        for {
            conn, err := server.Accept()
            if err != nil {
                log.Printf("cannot accept connection: %v", err)
                continue
            }

            connectToService()
            fmt.Fprintln(conn, "")
            conn.Close()
        }
    }()
    return &wg
}

func init() {
    daemonStarted := startNetworkDaemon()
    daemonStarted.Wait()
}

func BenchmarkNetworkRequest(b *testing.B) {
    for i := 0; i < b.N; i++ {
        conn, err := net.Dial("tcp", "localhost:8080")
        if err != nil {
            b.Fatalf("cannot dial host: %v", err)
        }

        if _, err := ioutil.ReadAll(conn); err != nil {
            b.Fatalf("cannot read :%v", err)
        }

        conn.Close()

    }
}

程序输出如下,耗时的数量级是10的9次方ns。


image.png

你可能感兴趣的:(建立TCP连接时不用池)