Skip to content

Instantly share code, notes, and snippets.

@ajxchapman
Last active Sep 24, 2021
Embed
What would you like to do?
NFT Benchmarking
package main
import (
"fmt"
"net"
"os"
)
func connect() {
c, err := net.Dial("tcp4", fmt.Sprintf("%s:4444", os.Args[1]))
if err != nil {
panic(err)
}
defer c.Close()
b := make([]byte, 4096)
c.Read(b)
}
func main() {
for {
connect()
}
}
package main
import (
"fmt"
"net"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
var startTime time.Time
var counter int
var timeout int = 30
func finish() {
endTime := time.Since(startTime)
fmt.Printf("\n%d connections in %d ticks %.02f/sec\n", counter, endTime.Milliseconds(), float64(counter*1000)/float64(endTime.Milliseconds()))
os.Exit(0)
}
func main() {
if len(os.Args) > 1 {
var err error
timeout, err = strconv.Atoi(os.Args[1])
if err != nil {
panic(err)
}
fmt.Printf("Using timeout of %d seconds\n", timeout)
}
l, err := net.Listen("tcp4", ":4444")
if err != nil {
panic(err)
}
defer l.Close()
// Setup signal handling to clean up at exit by signal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-signalChan
finish()
}()
for {
c, err := l.Accept()
if err != nil {
panic(err)
}
go func() {
if counter == 0 {
startTime = time.Now()
fmt.Printf("Started at %s\n", startTime)
time.AfterFunc(time.Duration(timeout)*time.Second, finish)
}
counter += 1
c.Write([]byte("Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! "))
c.Close()
}()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment