-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
"sync" | ||
"bufio" | ||
"context" | ||
"math/rand" | ||
|
||
"github.com/cloudwego/netpoll" | ||
) | ||
|
||
func runServer(port string, stopChan chan interface{}) { | ||
listener, err := netpoll.CreateListener("tcp", port) | ||
if err != nil { | ||
panic("can't failed here") | ||
} | ||
eventLoop, err := netpoll.NewEventLoop(handleConnection) | ||
if err != nil { | ||
panic("can't failed here") | ||
} | ||
go eventLoop.Serve(listener) | ||
|
||
go func() { | ||
<- stopChan | ||
eventLoop.Shutdown(context.Background()) | ||
listener.Close() | ||
}() | ||
} | ||
|
||
func handleConnection(ctx context.Context, conn netpoll.Connection) error { | ||
defer conn.Close() | ||
|
||
reader := conn.Reader() | ||
writer := conn.Writer() | ||
|
||
for { | ||
message, err := reader.Until('\n') | ||
if err != nil { | ||
return err | ||
} | ||
_, err = writer.WriteBinary(message) | ||
if err != nil { | ||
return err | ||
} | ||
err = writer.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
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" | ||
stopchan := make(chan interface{}) | ||
runServer(port, stopchan) | ||
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 netpoll to execute %dk connections, with %d writes per connection, is: %d minutes %d seconds\n", m / 1000, n, minutes, seconds) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
"time" | ||
|
||
"github.com/cloudwego/netpoll" | ||
) | ||
|
||
func runServer(port string, stopChan chan interface{}) { | ||
listener, err := netpoll.CreateListener("tcp", port) | ||
if err != nil { | ||
panic("can't failed here") | ||
} | ||
eventLoop, err := netpoll.NewEventLoop(handleConnection) | ||
if err != nil { | ||
panic("can't failed here") | ||
} | ||
go eventLoop.Serve(listener) | ||
|
||
go func() { | ||
<- stopChan | ||
eventLoop.Shutdown(context.Background()) | ||
listener.Close() | ||
}() | ||
} | ||
|
||
func handleConnection(ctx context.Context, conn netpoll.Connection) error { | ||
defer conn.Close() | ||
|
||
reader := conn.Reader() | ||
writer := conn.Writer() | ||
|
||
for { | ||
message, err := reader.Until('\n') | ||
if err != nil { | ||
return err | ||
} | ||
_, err = writer.WriteBinary(message) | ||
if err != nil { | ||
return err | ||
} | ||
err = writer.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
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" | ||
stopchan := make(chan interface{}) | ||
runServer(port, stopchan) | ||
defer close(stopchan) | ||
|
||
m := 100000 | ||
n := 100 | ||
messageLength := 48 | ||
|
||
start := time.Now() | ||
|
||
for i := 0; i < m; i++ { | ||
conn, err := net.Dial("tcp", port) | ||
if err != nil { | ||
fmt.Printf("failed to connect to server: %v\n", err) | ||
conn.Close() | ||
continue | ||
} | ||
|
||
for j := 0; j < n; j++ { | ||
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 failed\n", i, j) | ||
fmt.Printf("expect: %s\n", message) | ||
fmt.Printf("actual: %s\n", response) | ||
break | ||
} | ||
} | ||
|
||
conn.Close() | ||
} | ||
|
||
elapsed := time.Since(start) | ||
minutes := int(elapsed.Minutes()) | ||
seconds := int(elapsed.Seconds()) % 60 | ||
fmt.Printf("the total time for netpoll to execute %dk connections, with %d writes per connection, is: %d minutes %d seconds\n", m / 1000, n, minutes, seconds) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters