Skip to content

Commit

Permalink
feat: imporve benchmark logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zjregee committed Jul 17, 2024
1 parent 5fabf46 commit 43553a4
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 156 deletions.
72 changes: 39 additions & 33 deletions benchmark/anet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
72 changes: 39 additions & 33 deletions benchmark/net/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
72 changes: 39 additions & 33 deletions benchmark/netpoll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
27 changes: 27 additions & 0 deletions benchmark/report.log
Original file line number Diff line number Diff line change
@@ -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
48 changes: 24 additions & 24 deletions benchmark/scripts/run_all_benchmark.sh
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 43553a4

Please sign in to comment.