Added websocket header test and benchmark tool

This commit is contained in:
Toby Chui
2024-12-30 21:01:45 +08:00
parent 1d4c275db3
commit ad4721820b
7 changed files with 214 additions and 0 deletions

3
tools/benchmark/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module imuslab.com/zoraxy/benchmark
go 1.23.2

51
tools/benchmark/main.go Normal file
View File

@@ -0,0 +1,51 @@
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
var (
//Global variables
stopchan chan bool
//Runtime flags
benchmarkWebserverListeningPort int
)
func init() {
flag.IntVar(&benchmarkWebserverListeningPort, "port", 8123, "Port to listen on")
flag.Parse()
}
/* SIGTERM handler, do shutdown sequences before closing */
func SetupCloseHandler() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
//Stop all request loops
fmt.Println("Stopping request generators")
if stopchan != nil {
stopchan <- true
}
// Wait for all goroutines to finish
time.Sleep(1 * time.Second)
os.Exit(0)
}()
}
func main() {
//Setup the SIGTERM handler
SetupCloseHandler()
//Start the web server
fmt.Println("Starting web server on port", benchmarkWebserverListeningPort)
fmt.Println("In Zoraxy, point your test proxy rule to this server at the given port")
startWebServer()
select {}
}

42
tools/benchmark/server.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"fmt"
"net/http"
"time"
)
// Start the web server for reciving test request
// in Zoraxy, point test.localhost to this server at the given port in the start variables
func startWebServer() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Print the request details to console
fmt.Printf("Timestamp: %s\n", time.Now().Format(time.RFC1123))
fmt.Printf("Request type: %s\n", r.Method)
fmt.Printf("Payload size: %d bytes\n", r.ContentLength)
fmt.Printf("Request URI: %s\n", r.RequestURI)
fmt.Printf("User Agent: %s\n", r.UserAgent())
fmt.Printf("Remote Address: %s\n", r.RemoteAddr)
fmt.Println("----------------------------------------")
//Set header to text
w.Header().Set("Content-Type", "text/plain")
// Send response, print the request details to web page
w.Write([]byte("----------------------------------------\n"))
w.Write([]byte("Request type: " + r.Method + "\n"))
w.Write([]byte(fmt.Sprintf("Payload size: %d bytes\n", r.ContentLength)))
w.Write([]byte("Request URI: " + r.RequestURI + "\n"))
w.Write([]byte("User Agent: " + r.UserAgent() + "\n"))
w.Write([]byte("Remote Address: " + r.RemoteAddr + "\n"))
w.Write([]byte("----------------------------------------\n"))
})
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%d", benchmarkWebserverListeningPort), nil)
if err != nil {
fmt.Printf("Failed to start server: %v\n", err)
stopchan <- true
}
}()
}