From 43553a49b97bfa6c8efffc934f35778f8447ea07 Mon Sep 17 00:00:00 2001 From: zjregee Date: Wed, 17 Jul 2024 12:33:43 +0000 Subject: [PATCH] feat: imporve benchmark logic --- benchmark/anet/main.go | 72 ++++++++++++++------------ benchmark/net/main.go | 72 ++++++++++++++------------ benchmark/netpoll/main.go | 72 ++++++++++++++------------ benchmark/report.log | 27 ++++++++++ benchmark/scripts/run_all_benchmark.sh | 48 ++++++++--------- benchmark/uring/main.go | 72 ++++++++++++++------------ 6 files changed, 207 insertions(+), 156 deletions(-) create mode 100644 benchmark/report.log diff --git a/benchmark/anet/main.go b/benchmark/anet/main.go index 615070e..61c50f4 100644 --- a/benchmark/anet/main.go +++ b/benchmark/anet/main.go @@ -59,54 +59,60 @@ func main() { var ( c int m int - n int messageLength int ) flag.IntVar(&c, "c", 12, "") - flag.IntVar(&m, "m", 1000, "") - flag.IntVar(&n, "n", 100, "") - flag.IntVar(&messageLength, "len", 48, "") + flag.IntVar(&m, "m", 1000000, "") + flag.IntVar(&messageLength, "len", 1024, "") flag.Parse() - start := time.Now() - + count := 0 + var mu sync.Mutex var wg sync.WaitGroup + message := anet.GetRandomString(messageLength-1) + "\n" + + start := time.Now() for i := 0; i < c; i++ { wg.Add(1) go func(i int) { defer wg.Done() - for j := 0; j < m; j++ { - conn, err := net.Dial("tcp", port) + conn, err := net.Dial("tcp", port) + defer func() { + err := conn.Close() if err != nil { - fmt.Printf("failed to connect to server: %v\n", err) - conn.Close() - continue + fmt.Printf("failed to close connection: %v\n", err) + } + }() + if err != nil { + fmt.Printf("failed to connect to server: %v\n", err) + return + } + for { + mu.Lock() + if count == m { + mu.Unlock() + return + } + count += 1 + mu.Unlock() + _, err = conn.Write([]byte(message)) + if err != nil { + fmt.Printf("failed to send message: %v\n", err) + return } - for k := 0; k < n; k++ { - message := anet.GetRandomString(messageLength-1) + "\n" - _, err = conn.Write([]byte(message)) - if err != nil { - fmt.Printf("failed to send message: %v\n", err) - break - } - - response, err := bufio.NewReader(conn).ReadString('\n') - if err != nil { - fmt.Printf("failed to read response: %v\n", err) - break - } - - if message != response { - fmt.Printf("%v %v %v failed\n", i, j, k) - fmt.Printf("expect: %s\n", message) - fmt.Printf("actual: %s\n", response) - break - } + response, err := bufio.NewReader(conn).ReadString('\n') + if err != nil { + fmt.Printf("failed to read response: %v\n", err) + return } - conn.Close() + if response != message { + fmt.Printf("expect: %s\n", message) + fmt.Printf("actual: %s\n", response) + return + } } }(i) } @@ -116,5 +122,5 @@ func main() { minutes := int(elapsed.Minutes()) seconds := int(elapsed.Seconds()) % 60 milliseconds := int(elapsed.Milliseconds() % 1000) - fmt.Printf("the total time for anet to execute %dk connections using %d goroutines, with %d writes per connection and %d bytes per write, is: %d min %d sec %d ms\n", c*m/1000, c, n, messageLength, minutes, seconds, milliseconds) + fmt.Printf("the total time for anet to execute %dk connections using %d goroutines, with %d bytes per write, is: %d min %d sec %d ms\n", m/1000, c, messageLength, minutes, seconds, milliseconds) } diff --git a/benchmark/net/main.go b/benchmark/net/main.go index 8be5eb9..9e9c149 100644 --- a/benchmark/net/main.go +++ b/benchmark/net/main.go @@ -64,54 +64,60 @@ func main() { var ( c int m int - n int messageLength int ) flag.IntVar(&c, "c", 12, "") - flag.IntVar(&m, "m", 1000, "") - flag.IntVar(&n, "n", 100, "") - flag.IntVar(&messageLength, "len", 48, "") + flag.IntVar(&m, "m", 1000000, "") + flag.IntVar(&messageLength, "len", 1024, "") flag.Parse() - start := time.Now() - + count := 0 + var mu sync.Mutex var wg sync.WaitGroup + message := anet.GetRandomString(messageLength-1) + "\n" + + start := time.Now() for i := 0; i < c; i++ { wg.Add(1) go func(i int) { defer wg.Done() - for j := 0; j < m; j++ { - conn, err := net.Dial("tcp", port) + conn, err := net.Dial("tcp", port) + defer func() { + err := conn.Close() if err != nil { - fmt.Printf("failed to connect to server: %v\n", err) - conn.Close() - continue + fmt.Printf("failed to close connection: %v\n", err) + } + }() + if err != nil { + fmt.Printf("failed to connect to server: %v\n", err) + return + } + for { + mu.Lock() + if count == m { + mu.Unlock() + return + } + count += 1 + mu.Unlock() + _, err = conn.Write([]byte(message)) + if err != nil { + fmt.Printf("failed to send message: %v\n", err) + return } - for k := 0; k < n; k++ { - message := anet.GetRandomString(messageLength-1) + "\n" - _, err = conn.Write([]byte(message)) - if err != nil { - fmt.Printf("failed to send message: %v\n", err) - break - } - - response, err := bufio.NewReader(conn).ReadString('\n') - if err != nil { - fmt.Printf("failed to read response: %v\n", err) - break - } - - if message != response { - fmt.Printf("%v %v %v failed\n", i, j, k) - fmt.Printf("expect: %s\n", message) - fmt.Printf("actual: %s\n", response) - break - } + response, err := bufio.NewReader(conn).ReadString('\n') + if err != nil { + fmt.Printf("failed to read response: %v\n", err) + return } - conn.Close() + if response != message { + fmt.Printf("expect: %s\n", message) + fmt.Printf("actual: %s\n", response) + return + } } }(i) } @@ -121,5 +127,5 @@ func main() { minutes := int(elapsed.Minutes()) seconds := int(elapsed.Seconds()) % 60 milliseconds := int(elapsed.Milliseconds() % 1000) - fmt.Printf("the total time for net to execute %dk connections using %d goroutines, with %d writes per connection and %d bytes per write, is: %d min %d sec %d ms\n", c*m/1000, c, n, messageLength, minutes, seconds, milliseconds) + fmt.Printf("the total time for net to execute %dk connections using %d goroutines, with %d bytes per write, is: %d min %d sec %d ms\n", m/1000, c, messageLength, minutes, seconds, milliseconds) } diff --git a/benchmark/netpoll/main.go b/benchmark/netpoll/main.go index ae77210..dca84f1 100644 --- a/benchmark/netpoll/main.go +++ b/benchmark/netpoll/main.go @@ -63,54 +63,60 @@ func main() { var ( c int m int - n int messageLength int ) flag.IntVar(&c, "c", 12, "") - flag.IntVar(&m, "m", 1000, "") - flag.IntVar(&n, "n", 100, "") - flag.IntVar(&messageLength, "len", 48, "") + flag.IntVar(&m, "m", 1000000, "") + flag.IntVar(&messageLength, "len", 1024, "") flag.Parse() - start := time.Now() - + count := 0 + var mu sync.Mutex var wg sync.WaitGroup + message := anet.GetRandomString(messageLength-1) + "\n" + + start := time.Now() for i := 0; i < c; i++ { wg.Add(1) go func(i int) { defer wg.Done() - for j := 0; j < m; j++ { - conn, err := net.Dial("tcp", port) + conn, err := net.Dial("tcp", port) + defer func() { + err := conn.Close() + if err != nil { + fmt.Printf("failed to close connection: %v\n", err) + } + }() + if err != nil { + fmt.Printf("failed to connect to server: %v\n", err) + return + } + for { + mu.Lock() + if count == m { + mu.Unlock() + return + } + count += 1 + mu.Unlock() + _, err = conn.Write([]byte(message)) if err != nil { - fmt.Printf("failed to connect to server: %v\n", err) - conn.Close() - continue + fmt.Printf("failed to send message: %v\n", err) + return } - for k := 0; k < n; k++ { - message := anet.GetRandomString(messageLength-1) + "\n" - _, err = conn.Write([]byte(message)) - if err != nil { - fmt.Printf("failed to send message: %v\n", err) - break - } - - response, err := bufio.NewReader(conn).ReadString('\n') - if err != nil { - fmt.Printf("failed to read response: %v\n", err) - break - } - - if message != response { - fmt.Printf("%v %v %v failed\n", i, j, k) - fmt.Printf("expect: %s\n", message) - fmt.Printf("actual: %s\n", response) - break - } + response, err := bufio.NewReader(conn).ReadString('\n') + if err != nil { + fmt.Printf("failed to read response: %v\n", err) + return } - conn.Close() + if response != message { + fmt.Printf("expect: %s\n", message) + fmt.Printf("actual: %s\n", response) + return + } } }(i) } @@ -120,5 +126,5 @@ func main() { minutes := int(elapsed.Minutes()) seconds := int(elapsed.Seconds()) % 60 milliseconds := int(elapsed.Milliseconds() % 1000) - fmt.Printf("the total time for netpoll to execute %dk connections using %d goroutines, with %d writes per connection and %d bytes per write, is: %d min %d sec %d ms\n", c*m/1000, c, n, messageLength, minutes, seconds, milliseconds) + fmt.Printf("the total time for netpoll to execute %dk connections using %d goroutines, with %d bytes per write, is: %d min %d sec %d ms\n", m/1000, c, messageLength, minutes, seconds, milliseconds) } diff --git a/benchmark/report.log b/benchmark/report.log new file mode 100644 index 0000000..5a588d5 --- /dev/null +++ b/benchmark/report.log @@ -0,0 +1,27 @@ +the total time for net to execute 2000k connections using 4 goroutines, with 1024 bytes per write, is: 0 min 7 sec 254 ms +the total time for net to execute 2000k connections using 12 goroutines, with 1024 bytes per write, is: 0 min 5 sec 219 ms +the total time for net to execute 2000k connections using 64 goroutines, with 1024 bytes per write, is: 0 min 3 sec 816 ms +the total time for net to execute 2000k connections using 128 goroutines, with 1024 bytes per write, is: 0 min 3 sec 534 ms +the total time for net to execute 2000k connections using 512 goroutines, with 1024 bytes per write, is: 0 min 2 sec 907 ms +the total time for net to execute 2000k connections using 1024 goroutines, with 1024 bytes per write, is: 0 min 3 sec 195 ms + +the total time for netpoll to execute 2000k connections using 4 goroutines, with 1024 bytes per write, is: 0 min 7 sec 821 ms +the total time for netpoll to execute 2000k connections using 12 goroutines, with 1024 bytes per write, is: 0 min 4 sec 67 ms +the total time for netpoll to execute 2000k connections using 64 goroutines, with 1024 bytes per write, is: 0 min 2 sec 67 ms +the total time for netpoll to execute 2000k connections using 128 goroutines, with 1024 bytes per write, is: 0 min 2 sec 31 ms +the total time for netpoll to execute 2000k connections using 512 goroutines, with 1024 bytes per write, is: 0 min 2 sec 208 ms +the total time for netpoll to execute 2000k connections using 1024 goroutines, with 1024 bytes per write, is: 0 min 2 sec 906 ms + +the total time for uring to execute 2000k connections using 4 goroutines, with 1024 bytes per write, is: 0 min 10 sec 390 ms +the total time for uring to execute 2000k connections using 12 goroutines, with 1024 bytes per write, is: 0 min 5 sec 451 ms +the total time for uring to execute 2000k connections using 64 goroutines, with 1024 bytes per write, is: 0 min 4 sec 691 ms +the total time for uring to execute 2000k connections using 128 goroutines, with 1024 bytes per write, is: 0 min 5 sec 188 ms +the total time for uring to execute 2000k connections using 512 goroutines, with 1024 bytes per write, is: 0 min 4 sec 616 ms +the total time for uring to execute 2000k connections using 1024 goroutines, with 1024 bytes per write, is: 0 min 4 sec 806 ms + +the total time for anet to execute 2000k connections using 4 goroutines, with 1024 bytes per write, is: 0 min 10 sec 27 ms +the total time for anet to execute 2000k connections using 12 goroutines, with 1024 bytes per write, is: 0 min 4 sec 525 ms +the total time for anet to execute 2000k connections using 64 goroutines, with 1024 bytes per write, is: 0 min 3 sec 494 ms +the total time for anet to execute 2000k connections using 128 goroutines, with 1024 bytes per write, is: 0 min 3 sec 534 ms +the total time for anet to execute 2000k connections using 512 goroutines, with 1024 bytes per write, is: 0 min 3 sec 740 ms +the total time for anet to execute 2000k connections using 1024 goroutines, with 1024 bytes per write, is: 0 min 4 sec 369 ms diff --git a/benchmark/scripts/run_all_benchmark.sh b/benchmark/scripts/run_all_benchmark.sh index 033a26b..631e8e2 100755 --- a/benchmark/scripts/run_all_benchmark.sh +++ b/benchmark/scripts/run_all_benchmark.sh @@ -1,29 +1,29 @@ #!/usr/bin/env bash -./net/bench -c=12 -m=1000 -n=100 -len=256 -./net/bench -c=24 -m=1000 -n=100 -len=256 -./net/bench -c=36 -m=1000 -n=100 -len=256 -./net/bench -c=12 -m=1000 -n=100 -len=1024 -./net/bench -c=24 -m=1000 -n=100 -len=1024 -./net/bench -c=36 -m=1000 -n=100 -len=1024 +./net/bench -c=4 -m=2000000 -len=1024 +./net/bench -c=12 -m=2000000 -len=1024 +./net/bench -c=64 -m=2000000 -len=1024 +./net/bench -c=128 -m=2000000 -len=1024 +./net/bench -c=512 -m=2000000 -len=1024 +./net/bench -c=1024 -m=2000000 -len=1024 -./netpoll/bench -c=12 -m=1000 -n=100 -len=256 -./netpoll/bench -c=24 -m=1000 -n=100 -len=256 -./netpoll/bench -c=36 -m=1000 -n=100 -len=256 -./netpoll/bench -c=12 -m=1000 -n=100 -len=1024 -./netpoll/bench -c=24 -m=1000 -n=100 -len=1024 -./netpoll/bench -c=36 -m=1000 -n=100 -len=1024 +./netpoll/bench -c=4 -m=2000000 -len=1024 +./netpoll/bench -c=12 -m=2000000 -len=1024 +./netpoll/bench -c=64 -m=2000000 -len=1024 +./netpoll/bench -c=128 -m=2000000 -len=1024 +./netpoll/bench -c=512 -m=2000000 -len=1024 +./netpoll/bench -c=1024 -m=2000000 -len=1024 -./uring/bench -c=12 -m=1000 -n=100 -len=256 -./uring/bench -c=24 -m=1000 -n=100 -len=256 -./uring/bench -c=36 -m=1000 -n=100 -len=256 -./uring/bench -c=12 -m=1000 -n=100 -len=1024 -./uring/bench -c=24 -m=1000 -n=100 -len=1024 -./uring/bench -c=36 -m=1000 -n=100 -len=1024 +./uring/bench -c=4 -m=2000000 -len=1024 +./uring/bench -c=12 -m=2000000 -len=1024 +./uring/bench -c=64 -m=2000000 -len=1024 +./uring/bench -c=128 -m=2000000 -len=1024 +./uring/bench -c=512 -m=2000000 -len=1024 +./uring/bench -c=1024 -m=2000000 -len=1024 -./anet/bench -c=12 -m=1000 -n=100 -len=256 -./anet/bench -c=24 -m=1000 -n=100 -len=256 -./anet/bench -c=36 -m=1000 -n=100 -len=256 -./anet/bench -c=12 -m=1000 -n=100 -len=1024 -./anet/bench -c=24 -m=1000 -n=100 -len=1024 -./anet/bench -c=36 -m=1000 -n=100 -len=1024 +./anet/bench -c=4 -m=2000000 -len=1024 +./anet/bench -c=12 -m=2000000 -len=1024 +./anet/bench -c=64 -m=2000000 -len=1024 +./anet/bench -c=128 -m=2000000 -len=1024 +./anet/bench -c=512 -m=2000000 -len=1024 +./anet/bench -c=1024 -m=2000000 -len=1024 diff --git a/benchmark/uring/main.go b/benchmark/uring/main.go index eb52598..5f2e171 100644 --- a/benchmark/uring/main.go +++ b/benchmark/uring/main.go @@ -48,54 +48,60 @@ func main() { var ( c int m int - n int messageLength int ) flag.IntVar(&c, "c", 12, "") - flag.IntVar(&m, "m", 1000, "") - flag.IntVar(&n, "n", 100, "") - flag.IntVar(&messageLength, "len", 48, "") + flag.IntVar(&m, "m", 1000000, "") + flag.IntVar(&messageLength, "len", 1024, "") flag.Parse() - start := time.Now() - + count := 0 + var mu sync.Mutex var wg sync.WaitGroup + message := anet.GetRandomString(messageLength-1) + "\n" + + start := time.Now() for i := 0; i < c; i++ { wg.Add(1) go func(i int) { defer wg.Done() - for j := 0; j < m; j++ { - conn, err := net.Dial("tcp", port) + conn, err := net.Dial("tcp", port) + defer func() { + err := conn.Close() if err != nil { - fmt.Printf("failed to connect to server: %v\n", err) - conn.Close() - continue + fmt.Printf("failed to close connection: %v\n", err) + } + }() + if err != nil { + fmt.Printf("failed to connect to server: %v\n", err) + return + } + for { + mu.Lock() + if count == m { + mu.Unlock() + return + } + count += 1 + mu.Unlock() + _, err = conn.Write([]byte(message)) + if err != nil { + fmt.Printf("failed to send message: %v\n", err) + return } - for k := 0; k < n; k++ { - message := anet.GetRandomString(messageLength-1) + "\n" - _, err = conn.Write([]byte(message)) - if err != nil { - fmt.Printf("failed to send message: %v\n", err) - break - } - - response, err := bufio.NewReader(conn).ReadString('\n') - if err != nil { - fmt.Printf("failed to read response: %v\n", err) - break - } - - if message != response { - fmt.Printf("%v %v %v failed\n", i, j, k) - fmt.Printf("expect: %s\n", message) - fmt.Printf("actual: %s\n", response) - break - } + response, err := bufio.NewReader(conn).ReadString('\n') + if err != nil { + fmt.Printf("failed to read response: %v\n", err) + return } - conn.Close() + if response != message { + fmt.Printf("expect: %s\n", message) + fmt.Printf("actual: %s\n", response) + return + } } }(i) } @@ -105,5 +111,5 @@ func main() { minutes := int(elapsed.Minutes()) seconds := int(elapsed.Seconds()) % 60 milliseconds := int(elapsed.Milliseconds() % 1000) - fmt.Printf("the total time for uring to execute %dk connections using %d goroutines, with %d writes per connection and %d bytes per write, is: %d min %d sec %d ms\n", c*m/1000, c, n, messageLength, minutes, seconds, milliseconds) + fmt.Printf("the total time for uring to execute %dk connections using %d goroutines, with %d bytes per write, is: %d min %d sec %d ms\n", m/1000, c, messageLength, minutes, seconds, milliseconds) }