Skip to content

Commit

Permalink
feat: update benchmark layout
Browse files Browse the repository at this point in the history
  • Loading branch information
zjregee committed Jul 5, 2024
1 parent c6c5426 commit ee5942c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 4 deletions.
135 changes: 135 additions & 0 deletions benchmark/net/concurrent/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package main

import (
"os"
"net"
"fmt"
"time"
"sync"
"bufio"
"math/rand"

"github.com/sirupsen/logrus"
)

func runServer(port string, stopChan chan interface{}, logger *logrus.Logger) {
listener, err := net.Listen("tcp", port)
if err != nil {
panic("shouldn't failed here")
}

go func() {
for {
conn, err := listener.Accept()
if err != nil {
logger.Warnf("error occurred when accept: %s", err.Error())
continue
}
go handleConnection(conn, logger)
}
}()

go func() {
<- stopChan
listener.Close()
}()
}

func handleConnection(conn net.Conn, logger *logrus.Logger) {
defer conn.Close()

reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)

for {
message, err := reader.ReadString('\n')
if err != nil {
logger.Warnf("error occurred when read: %s",err.Error())
return
}
_, err = writer.WriteString(message)
if err != nil {
logger.Warnf("error occurred when write: %s",err.Error())
return
}
err = writer.Flush()
if err != nil {
logger.Warnf("error occurred when flush: %s",err.Error())
return
}
}
}

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func randomString(length int) string {
seed := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[seed.Intn(len(charset))]
}
return string(b)
}

func main() {
port := ":8000"
logger := logrus.New()
logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.FatalLevel)
stopchan := make(chan interface{})
runServer(port, stopchan, logger)
defer close(stopchan)

c := 12
m := 10000
n := 100
messageLength := 48

start := time.Now()

var wg sync.WaitGroup
for i := 0; i < c; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < m; j++ {
conn, err := net.Dial("tcp", port)
if err != nil {
fmt.Printf("failed to connect to server: %v\n", err)
conn.Close()
continue
}

for k := 0; k < n; k++ {
message := randomString(messageLength) + "\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
}
}

conn.Close()
}
}()
}
wg.Wait()

elapsed := time.Since(start)
minutes := int(elapsed.Minutes())
seconds := int(elapsed.Seconds()) % 60
fmt.Printf("the total time for net to execute %dk connections, with %d writes per connection, is: %d minutes %d seconds\n", m / 1000, n, minutes, seconds)
}
File renamed without changes.
10 changes: 6 additions & 4 deletions benchmark/run_benchmark.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env bash

go build -v -o ./net/main ./net
go build -v -o ./uring/main ./uring
go build -v -o ./net/bench_serial ./net/serial
go build -v -o ./net/bench_concurrent ./net/concurrent
go build -v -o ./uring/bench_serial ./uring/serial

./net/main
./uring/main
./net/bench_serial
./net/bench_concurrent
./uring/bench_serial
File renamed without changes.

0 comments on commit ee5942c

Please sign in to comment.