Skip to content

Commit

Permalink
feat: add profile data
Browse files Browse the repository at this point in the history
  • Loading branch information
zjregee committed Jul 21, 2024
1 parent 9bcac7b commit 2a4b5e7
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 403 deletions.
102 changes: 10 additions & 92 deletions benchmark/anet/main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
package main

import (
"bufio"
"context"
"flag"
"fmt"
"net"
"sync"
"time"

"github.com/zjregee/anet"
)

func runServer(port string, stopChan chan interface{}) {
listener, err := anet.CreateListener("tcp", port)
if err != nil {
panic("shouldn't failed here")
}

eventLoop, err := anet.NewEventLoop(handleConnection)
if err != nil {
panic("shouldn't failed here")
}
go eventLoop.Serve(listener)

go func() {
<-stopChan
eventLoop.Shutdown(context.Background())
listener.Close()
}()
}

func handleConnection(_ context.Context, connection anet.Connection) error {
reader, writer := connection.Reader(), connection.Writer()

Expand All @@ -52,76 +28,18 @@ func handleConnection(_ context.Context, connection anet.Connection) error {
}

func main() {
port := ":8000"
stopchan := make(chan interface{})
runServer(port, stopchan)
defer close(stopchan)

var (
c int
m int
messageLength int
)

flag.IntVar(&c, "c", 12, "")
flag.IntVar(&m, "m", 1000000, "")
flag.IntVar(&messageLength, "len", 1024, "")
var port string
flag.StringVar(&port, "port", ":8000", "")
flag.Parse()

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()
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 send message: %v\n", err)
return
}

response, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
fmt.Printf("failed to read response: %v\n", err)
return
}

if response != message {
fmt.Printf("expect: %s\n", message)
fmt.Printf("actual: %s\n", response)
return
}
}
}(i)
listener, err := anet.CreateListener("tcp", port)
if err != nil {
panic("shouldn't failed here")
}
wg.Wait()

elapsed := time.Since(start)
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 bytes per write, is: %d min %d sec %d ms\n", m/1000, c, messageLength, minutes, seconds, milliseconds)
eventLoop, err := anet.NewEventLoop(handleConnection)
if err != nil {
panic("shouldn't failed here")
}
_ = eventLoop.Serve(listener)
}
Binary file added benchmark/anet_profile.pb.gz
Binary file not shown.
86 changes: 86 additions & 0 deletions benchmark/bencher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"bufio"
"flag"
"fmt"
"net"
"sync"
"time"

"github.com/zjregee/anet"
)

func main() {
var (
c int
m int
messageLength int
port string
name string
)

flag.IntVar(&c, "c", 12, "")
flag.IntVar(&m, "m", 1000000, "")
flag.IntVar(&messageLength, "len", 1024, "")
flag.StringVar(&port, "port", ":8000", "")
flag.StringVar(&name, "name", "anet", "")
flag.Parse()

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()
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 send message: %v\n", err)
return
}

response, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
fmt.Printf("failed to read response: %v\n", err)
return
}

if response != message {
fmt.Printf("expect: %s\n", message)
fmt.Printf("actual: %s\n", response)
return
}
}
}(i)
}
wg.Wait()

elapsed := time.Since(start)
minutes := int(elapsed.Minutes())
seconds := int(elapsed.Seconds()) % 60
milliseconds := int(elapsed.Milliseconds() % 1000)
fmt.Printf("the total time for %s to execute %dk connections using %d goroutines, with %d bytes per write, is: %d min %d sec %d ms\n", name, m/1000, c, messageLength, minutes, seconds, milliseconds)
}
2 changes: 2 additions & 0 deletions benchmark/build_all_benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ rm -rf output/
mkdir output
cp scripts/* output/

go build -v -o output/bencher ./bencher

mkdir -p output/net
go build -v -o output/net/bench ./net

Expand Down
107 changes: 12 additions & 95 deletions benchmark/net/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,9 @@ package main
import (
"bufio"
"flag"
"fmt"
"net"
"sync"
"time"

"github.com/zjregee/anet"
)

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

go func() {
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleConnection(conn)
}
}()

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

func handleConnection(conn net.Conn) {
defer conn.Close()

Expand All @@ -56,76 +29,20 @@ func handleConnection(conn net.Conn) {
}

func main() {
port := ":8000"
stopchan := make(chan interface{})
runServer(port, stopchan)
defer close(stopchan)

var (
c int
m int
messageLength int
)

flag.IntVar(&c, "c", 12, "")
flag.IntVar(&m, "m", 1000000, "")
flag.IntVar(&messageLength, "len", 1024, "")
var port string
flag.StringVar(&port, "port", ":8000", "")
flag.Parse()

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()
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 send message: %v\n", err)
return
}

response, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
fmt.Printf("failed to read response: %v\n", err)
return
}

if response != message {
fmt.Printf("expect: %s\n", message)
fmt.Printf("actual: %s\n", response)
return
}
}
}(i)
listener, err := net.Listen("tcp", port)
if err != nil {
panic("shouldn't failed here")
}
wg.Wait()

elapsed := time.Since(start)
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 bytes per write, is: %d min %d sec %d ms\n", m/1000, c, messageLength, minutes, seconds, milliseconds)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleConnection(conn)
}
}
Loading

0 comments on commit 2a4b5e7

Please sign in to comment.