diff --git a/go-server/main.go b/go-server/main.go index 3686049..e02ae3e 100644 --- a/go-server/main.go +++ b/go-server/main.go @@ -209,7 +209,7 @@ func main() { http.ListenAndServe(":8080", nil) }() - // TLS + // TLS certPath := "/app/certificate/certificate.pem" keyPath := "/app/certificate/private-key.pem" @@ -221,5 +221,5 @@ func main() { fmt.Println("HTTPS server error:", err) } }() - select{} + select {} } diff --git a/traffic-generators/1-high-connection/tcpclient.go b/traffic-generators/1-high-connection/tcpclient.go index dd3b503..b58e347 100644 --- a/traffic-generators/1-high-connection/tcpclient.go +++ b/traffic-generators/1-high-connection/tcpclient.go @@ -7,9 +7,9 @@ import ( "os" "strconv" "strings" + "sync/atomic" "syscall" "time" - "sync/atomic" ) const ( @@ -139,7 +139,7 @@ func main() { } for serverPort := MIN_SERVER_PORT; ; serverPort++ { - if atomic.LoadInt64(&liveConnection) + 1 > int64(maxConcurrent){ + if atomic.LoadInt64(&liveConnection)+1 > int64(maxConcurrent) { time.Sleep(time.Second) timeStart = time.Now() sleepCounter = 0 diff --git a/traffic-generators/new-flow/tcpclient.go b/traffic-generators/new-flow/tcpclient.go index c855e95..44ddec4 100644 --- a/traffic-generators/new-flow/tcpclient.go +++ b/traffic-generators/new-flow/tcpclient.go @@ -166,4 +166,4 @@ func main() { sleepCounter = 0 } } -} \ No newline at end of file +} diff --git a/traffic-tools-plugin/README.md b/traffic-tools-plugin/README.md new file mode 100644 index 0000000..c0bc80b --- /dev/null +++ b/traffic-tools-plugin/README.md @@ -0,0 +1,78 @@ +# 打流工具介绍 + +## 1. DNS 打流工具 + +**协议:** UDP (L4) 和 DNS (L7) + +通过 UDP 协议和 DNS 协议的流量触发 DNS 插件。该工具可以模拟 DNS 流量,用于测试和评估 DNS 插件的性能和响应。 + +**使用方法:** + +``` + -c int + Number of concurrent connections (default 1) + -r int + Number of requests per connection (default 10) +``` + +## 2. HTTP 打流工具 + +**协议:** HTTP (L7) + +通过 8080 端口和 URL 为 "/user_info" 的流量触发 HTTP 插件。该工具可以模拟 HTTP 流量,特定的请求和响应头部字段用于触发 HTTP 插件的计算。 + +**请求 (Req):** +- Header 添加 "Custom-Trace-Info" 用于 HTTP 插件计算 + +**响应 (Resp):** +- Data 添加 "code" 和 "data" 字段用于 HTTP 插件计算 +- +**使用方法:** + +``` + -r int + request per second (default 100) +``` + +## 3. HTTP Status Rewrite 打流工具 + +**协议:** HTTP (L7) + +通过监听 HTTP 响应,修改 Data 字段的 "OPT_STATUS" 为 "SUCCESS",同时填充其他数据,以触发 TCP 分片(TCP Fragment)。 + +**响应 (Resp):** +- Data 添加 "OPT_STATUS": "SUCCESS",其他填充数据用于触发 TCP 分片 + +**使用方法:** + +``` + -r int + request per second (default 100) +``` + +## 4. GO_HTTP2_Uprobe 打流工具 + +**协议:** gRPC + +通过 gRPC 调用触发,指定 "Trace" 和 "Trace" 字段。该工具用于测试和评估http2_uprobe插件性能。 + +**使用方法:** + +``` + -r int + request per second (default 100) +``` + +## 5. KRPC 打流工具 + +**使用方法:** + +``` + -h string + krpc pod clusterIP + -r int + request per second (default 100) +``` + + + diff --git a/traffic-tools-plugin/dns/client.go b/traffic-tools-plugin/dns/client.go index aa76a06..ef469c4 100644 --- a/traffic-tools-plugin/dns/client.go +++ b/traffic-tools-plugin/dns/client.go @@ -1,20 +1,21 @@ package main import ( + "flag" "fmt" "golang.org/x/net/dns/dnsmessage" "net" - "os" - "strconv" "time" ) + var ( - newFlow = 1 - request_per_flow = 1 + request_per_conn = flag.Int("r", 10, "Number of requests per connection") + concurrentConn = flag.Int("c", 1, "Number of concurrent connections") request_interval = time.Second / 10 - ) + func newConnection(queryData []byte) { + flag.Parse() serverAddr := "127.0.0.1:19053" // 创建 UDP 连接到 DNS 服务器 @@ -27,7 +28,7 @@ func newConnection(queryData []byte) { fmt.Printf("Error creating UDP connection: %v\n", err) } defer conn.Close() - for i:=0; i < request_per_flow; i++{ + for i := 0; i < *request_per_conn; i++ { conn.Write(queryData) responseData := make([]byte, 512) conn.Read(responseData) @@ -39,7 +40,7 @@ func main() { message := dnsmessage.Message{ Header: dnsmessage.Header{ - ID: 12345, + ID: 12345, Response: false, OpCode: 0, RecursionDesired: true, @@ -57,25 +58,18 @@ func main() { if err != nil { fmt.Printf("Failed to encode DNS query: %v\n", err) } - if len(os.Args) == 2{ - newFlow, _ = strconv.Atoi(os.Args[1]) - } - if len(os.Args) == 3{ - newFlow, _ = strconv.Atoi(os.Args[1]) - request_per_flow, _ = strconv.Atoi(os.Args[2]) - } sleepCounter := 0 timeStart := time.Now() - for { + for { go newConnection(queryData) - time.Sleep(time.Second / time.Duration(newFlow) / 6) + time.Sleep(time.Second / time.Duration(*concurrentConn) / 6) sleepCounter += 1 - if sleepCounter >= newFlow { + if sleepCounter >= *concurrentConn { timeElapsed := time.Since(timeStart) if timeElapsed < time.Second { - fmt.Printf("Create %d connections\n",sleepCounter) + fmt.Printf("Create %d connections\n", sleepCounter) time.Sleep(time.Second - timeElapsed) } @@ -84,4 +78,4 @@ func main() { } } -} \ No newline at end of file +} diff --git a/traffic-tools-plugin/dns/server.go b/traffic-tools-plugin/dns/server.go index 93b5ec6..9218e16 100644 --- a/traffic-tools-plugin/dns/server.go +++ b/traffic-tools-plugin/dns/server.go @@ -2,9 +2,9 @@ package main import ( "fmt" + "golang.org/x/net/dns/dnsmessage" "net" "sync/atomic" - "golang.org/x/net/dns/dnsmessage" "time" ) @@ -26,7 +26,7 @@ func main() { defer conn.Close() fmt.Printf("DNS server is listening on %s\n", serverAddr) - go func (){ + go func() { for { buffer := make([]byte, 512) _, addr, err := conn.ReadFromUDP(buffer) @@ -38,21 +38,21 @@ func main() { // 解析 DNS 查询请求 var request dnsmessage.Message request.Unpack(buffer) - + // 构建 DNS 响应消息 response := dnsmessage.Message{ Header: dnsmessage.Header{ - ID: request.Header.ID, // 使用相同的事务 ID - Response: true, // 设置为响应 + ID: request.Header.ID, // 使用相同的事务 ID + Response: true, // 设置为响应 }, Questions: request.Questions, // 复制查询问题 Answers: []dnsmessage.Resource{ { Header: dnsmessage.ResourceHeader{ Name: request.Questions[0].Name, // 使用相同的查询域名 - Type: dnsmessage.TypeA, // 响应类型,A表示 IPv4 地址 - Class: dnsmessage.ClassINET, // 查询类别,通常是 INET - TTL: 300, // TTL (Time to Live) 设置为 300 秒 + Type: dnsmessage.TypeA, // 响应类型,A表示 IPv4 地址 + Class: dnsmessage.ClassINET, // 查询类别,通常是 INET + TTL: 300, // TTL (Time to Live) 设置为 300 秒 }, Body: &dnsmessage.AResource{ A: [4]byte{192, 168, 1, 100}, // 设置响应的 IP 地址 @@ -64,13 +64,13 @@ func main() { conn.WriteToUDP(responseData, addr) } }() - + ticker := time.NewTicker(1 * time.Second) i := 0 for range ticker.C { i += 1 fmt.Printf("%s %d totalPacket %d,\n", - time.Now().Format(time.RFC3339),i ,atomic.LoadUint64(&totalPacket)) + time.Now().Format(time.RFC3339), i, atomic.LoadUint64(&totalPacket)) } } diff --git a/traffic-tools-plugin/go_http2_uprobe/client.go b/traffic-tools-plugin/go_http2_uprobe/client.go index c06a946..5b20814 100644 --- a/traffic-tools-plugin/go_http2_uprobe/client.go +++ b/traffic-tools-plugin/go_http2_uprobe/client.go @@ -1,48 +1,45 @@ package main import ( - "context" - "demo/go_http2_uprobe/pb" - "fmt" - "google.golang.org/grpc" - "time" - "os" - "strconv" + "context" + "demo/go_http2_uprobe/pb" + "flag" + "fmt" + "google.golang.org/grpc" + "time" ) var ( - newFlow = 80 + requestPerSecond = flag.Int("r", 100, "request per second") ) func newRequest(cli pb.DemoClient) { - var i uint32 = 1 - cli.Call(context.TODO(), &pb.Req{ - Msg: &pb.Msg{ - ID: i, - Payload: []byte{1, 2, 3, 4}, - }, - Trace: &pb.Trace{ - TraceId: 999, - Start: 999, - Span: 123, - }, - }) + var i uint32 = 1 + cli.Call(context.TODO(), &pb.Req{ + Msg: &pb.Msg{ + ID: i, + Payload: []byte{1, 2, 3, 4}, + }, + Trace: &pb.Trace{ + TraceId: 999, + Start: 999, + Span: 123, + }, + }) } func main() { - if len(os.Args) == 2{ - newFlow, _ = strconv.Atoi(os.Args[1]) - } + flag.Parse() sleepCounter := 0 timeStart := time.Now() - c, _ := grpc.Dial("127.0.0.1:1234", grpc.WithInsecure()) - cli := pb.NewDemoClient(c) - for { + c, _ := grpc.Dial("127.0.0.1:1234", grpc.WithInsecure()) + cli := pb.NewDemoClient(c) + for { go newRequest(cli) - time.Sleep(time.Second / time.Duration(newFlow) / 6) + time.Sleep(time.Second / time.Duration(*requestPerSecond) / 6) sleepCounter += 1 - if sleepCounter >= newFlow { + if sleepCounter >= *requestPerSecond { timeElapsed := time.Since(timeStart) fmt.Printf("Create %d requests, cost time %v\n", sleepCounter, timeElapsed) if timeElapsed < time.Second { @@ -53,4 +50,4 @@ func main() { sleepCounter = 0 } } -} \ No newline at end of file +} diff --git a/traffic-tools-plugin/go_http2_uprobe/server.go b/traffic-tools-plugin/go_http2_uprobe/server.go index 8f9a92f..11d5501 100644 --- a/traffic-tools-plugin/go_http2_uprobe/server.go +++ b/traffic-tools-plugin/go_http2_uprobe/server.go @@ -1,33 +1,33 @@ package main import ( - "context" - "demo/go_http2_uprobe/pb" - "fmt" - "google.golang.org/grpc" - "net" + "context" + "demo/go_http2_uprobe/pb" + "fmt" + "google.golang.org/grpc" + "net" ) type demoServer struct{} func (s *demoServer) Call(ctx context.Context, req *pb.Req) (*pb.Resp, error) { - return &pb.Resp{ - Msg: req.Msg, - Trace: &pb.Trace{ - TraceId: 999, - Start: 999, - Span: 123, - }, - }, nil + return &pb.Resp{ + Msg: req.Msg, + Trace: &pb.Trace{ + TraceId: 999, + Start: 999, + Span: 123, + }, + }, nil } func main() { - s := grpc.NewServer() - pb.RegisterDemoServer(s, &demoServer{}) - l, err := net.Listen("tcp", ":1234") - if err != nil { - panic(err) - } - fmt.Println("Server is running on :1234") - panic(s.Serve(l)) + s := grpc.NewServer() + pb.RegisterDemoServer(s, &demoServer{}) + l, err := net.Listen("tcp", ":1234") + if err != nil { + panic(err) + } + fmt.Println("Server is running on :1234") + panic(s.Serve(l)) } diff --git a/traffic-tools-plugin/http/client.go b/traffic-tools-plugin/http/client.go index 0b5a543..a30d95b 100644 --- a/traffic-tools-plugin/http/client.go +++ b/traffic-tools-plugin/http/client.go @@ -1,26 +1,24 @@ package main import ( + "flag" "fmt" - "os" - "net/http" "io/ioutil" - "strconv" + "net/http" "time" ) const ( - SERVER_PORT = 8080 + SERVER_PORT = 8080 REQUEST_INTERVAL = time.Second / 10 ) var ( - newFlow = 80 - + requestPerSecond = flag.Int("r", 100, "request per second") ) -func newRequest(client *http.Client,req *http.Request) { - +func newRequest(client *http.Client, req *http.Request) { + flag.Parse() resp, err := client.Do(req) if err != nil { fmt.Println("request err:", err) @@ -32,41 +30,36 @@ func newRequest(client *http.Client,req *http.Request) { fmt.Println("read response err", err) return } - + time.Sleep(REQUEST_INTERVAL) } - func main() { - if len(os.Args) == 2{ - newFlow, _ = strconv.Atoi(os.Args[1]) - } - sleepCounter := 0 timeStart := time.Now() transport := &http.Transport{ - MaxIdleConns: 100, - MaxIdleConnsPerHost: 100, + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, } client := &http.Client{ Transport: transport, - Timeout: 100 * time.Second, + Timeout: 100 * time.Second, + } + req, err := http.NewRequest("GET", "http://localhost:8080/user_info?username=dftest&type=1", nil) + if err != nil { + fmt.Println("create req err:", err) + return } - req, err := http.NewRequest("GET", "http://localhost:8080/user_info?username=dftest", nil) - if err != nil { - fmt.Println("create req err:", err) - return - } jsonValue := `{"trace_id": "101", "span_id": "110"}` - req.Header.Set("Custom-Trace-Info", jsonValue) + req.Header.Set("Custom-Trace-Info", jsonValue) - for { + for { go newRequest(client, req) - time.Sleep(time.Second / time.Duration(newFlow) / 6) + time.Sleep(time.Second / time.Duration(*requestPerSecond) / 6) sleepCounter += 1 - if sleepCounter >= newFlow { + if sleepCounter >= *requestPerSecond { timeElapsed := time.Since(timeStart) fmt.Printf("Create %d requests, cost time %v\n", sleepCounter, timeElapsed) if timeElapsed < time.Second { @@ -77,4 +70,4 @@ func main() { sleepCounter = 0 } } -} \ No newline at end of file +} diff --git a/traffic-tools-plugin/http/server.go b/traffic-tools-plugin/http/server.go index cde4432..c170d49 100644 --- a/traffic-tools-plugin/http/server.go +++ b/traffic-tools-plugin/http/server.go @@ -1,36 +1,36 @@ package main import ( - "fmt" - "net/http" - "encoding/json" + "encoding/json" + "fmt" + "net/http" ) func test(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - data := map[string]interface{}{ - "code": "0", - "data": map[string]interface{}{ - "user_id": "123456", - "register_time": "1697361101", - }, - } + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + data := map[string]interface{}{ + "code": "0", + "data": map[string]interface{}{ + "user_id": "123456", + "register_time": "1697361101", + }, + } - jsonData, err := json.Marshal(data) - _, err = w.Write(jsonData) - if err != nil { - http.Error(w, "Failed to write response", http.StatusInternalServerError) - return - } + jsonData, err := json.Marshal(data) + _, err = w.Write(jsonData) + if err != nil { + http.Error(w, "Failed to write response", http.StatusInternalServerError) + return + } } func main() { - http.HandleFunc("/user_info", test) + http.HandleFunc("/user_info", test) - fmt.Println("Server is running on :8080") - err := http.ListenAndServe(":8080", nil) - if err != nil { - fmt.Println("Error:", err) - } + fmt.Println("Server is running on :8080") + err := http.ListenAndServe(":8080", nil) + if err != nil { + fmt.Println("Error:", err) + } } diff --git a/traffic-tools-plugin/http_status_rewrite/client.go b/traffic-tools-plugin/http_status_rewrite/client.go index 6001691..b7e8d76 100644 --- a/traffic-tools-plugin/http_status_rewrite/client.go +++ b/traffic-tools-plugin/http_status_rewrite/client.go @@ -1,25 +1,23 @@ package main import ( + "flag" "fmt" - "os" - "net/http" "io/ioutil" - "strconv" + "net/http" "time" ) const ( - SERVER_PORT = 8080 - REQUEST_INTERVAL = time.Second + SERVER_PORT = 8080 + REQUEST_INTERVAL = time.Second ) var ( - newFlow = 80 - serverIp = "" + requestPerSecond = flag.Int("r", 100, "request per second") ) -func newRequest(client *http.Client,req *http.Request) { +func newRequest(client *http.Client, req *http.Request) { resp, err := client.Do(req) if err != nil { @@ -32,38 +30,35 @@ func newRequest(client *http.Client,req *http.Request) { fmt.Println("read response err", err) return } - + time.Sleep(REQUEST_INTERVAL) } - func main() { - if len(os.Args) == 2{ - newFlow, _ = strconv.Atoi(os.Args[1]) - } + flag.Parse() ipAddress := "http://127.0.0.1:8080" sleepCounter := 0 timeStart := time.Now() transport := &http.Transport{ - MaxIdleConns: 100, - MaxIdleConnsPerHost: 100, + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, } client := &http.Client{ Transport: transport, - Timeout: 100 * time.Second, + Timeout: 100 * time.Second, } req, err := http.NewRequest("GET", ipAddress+"/v1/vtaps/", nil) - if err != nil { - fmt.Println("create req err:", err) - return - } + if err != nil { + fmt.Println("create req err:", err) + return + } - for { + for { go newRequest(client, req) - time.Sleep(time.Second / time.Duration(newFlow) / 6) + time.Sleep(time.Second / time.Duration(*requestPerSecond) / 6) sleepCounter += 1 - if sleepCounter >= newFlow { + if sleepCounter >= *requestPerSecond { timeElapsed := time.Since(timeStart) fmt.Printf("Create %d requests, cost time %v\n", sleepCounter, timeElapsed) if timeElapsed < time.Second { @@ -73,4 +68,4 @@ func main() { sleepCounter = 0 } } -} \ No newline at end of file +} diff --git a/traffic-tools-plugin/http_status_rewrite/server.go b/traffic-tools-plugin/http_status_rewrite/server.go index a2242fa..18bc8fd 100644 --- a/traffic-tools-plugin/http_status_rewrite/server.go +++ b/traffic-tools-plugin/http_status_rewrite/server.go @@ -1,10 +1,11 @@ package main import ( - "fmt" - "net/http" - "encoding/json" + "encoding/json" + "fmt" + "net/http" ) + type DataItem struct { ID int `json:"ID"` NAME string `json:"NAME"` @@ -12,16 +13,16 @@ type DataItem struct { } type ResponseData struct { - OPT_STATUS string `json:"OPT_STATUS"` - DESCRIPTION string `json:"DESCRIPTION"` + OPT_STATUS string `json:"OPT_STATUS"` + DESCRIPTION string `json:"DESCRIPTION"` DATA []DataItem `json:"DATA"` } func test(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) data := ResponseData{ - OPT_STATUS: "SUCCESS", + OPT_STATUS: "SUCCESS", DESCRIPTION: "Data retrieved successfully", DATA: []DataItem{ { @@ -49,23 +50,22 @@ func test(w http.ResponseWriter, r *http.Request) { NAME: "Item 2111111111111111111111111111111111111111111111111111111111111111111111111111", STATE: 2, }, - }, } - jsonData, err := json.Marshal(data) - _, err = w.Write(jsonData) - if err != nil { - http.Error(w, "Failed to write response", http.StatusInternalServerError) - return - } + jsonData, err := json.Marshal(data) + _, err = w.Write(jsonData) + if err != nil { + http.Error(w, "Failed to write response", http.StatusInternalServerError) + return + } } func main() { - http.HandleFunc("/v1/vtaps/", test) + http.HandleFunc("/v1/vtaps/", test) - fmt.Println("Server is running on :8080") - err := http.ListenAndServe(":8080", nil) - if err != nil { - fmt.Println("Error:", err) - } + fmt.Println("Server is running on :8080") + err := http.ListenAndServe(":8080", nil) + if err != nil { + fmt.Println("Error:", err) + } } diff --git a/traffic-tools-plugin/krpc/client.go b/traffic-tools-plugin/krpc/client.go index 61ffd1f..6d6bcf9 100644 --- a/traffic-tools-plugin/krpc/client.go +++ b/traffic-tools-plugin/krpc/client.go @@ -1,11 +1,10 @@ package main import ( + "flag" "fmt" - "os" - "net/http" "io/ioutil" - "strconv" + "net/http" "time" ) @@ -14,11 +13,11 @@ const ( ) var ( - newFlow = 700 - clusterIP = "" + requestPerSecond = flag.Int("r", 100, "request per second") + clusterIP = flag.String("h", "", "krpc pod clusterIP") ) -func newRequest(client *http.Client,req *http.Request) { +func newRequest(client *http.Client, req *http.Request) { resp, err := client.Do(req) if err != nil { @@ -31,41 +30,34 @@ func newRequest(client *http.Client,req *http.Request) { fmt.Println("read response err", err) return } - + time.Sleep(REQUEST_INTERVAL) } - func main() { - if len(os.Args) == 2{ - clusterIP = os.Args[1] - } - if len(os.Args) == 3{ - clusterIP = os.Args[1] - newFlow, _ = strconv.Atoi(os.Args[2]) - } + flag.Parse() sleepCounter := 0 timeStart := time.Now() transport := &http.Transport{ - MaxIdleConns: 100, - MaxIdleConnsPerHost: 100, + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, } client := &http.Client{ Transport: transport, - Timeout: 100 * time.Second, + Timeout: 100 * time.Second, } - req, err := http.NewRequest("GET", "http://"+ clusterIP +":8450/v1/openapi/queryPriceRule", nil) + req, err := http.NewRequest("GET", "http://"+*clusterIP+":8450/v1/openapi/queryPriceRule", nil) // req.Close = true - if err != nil { - fmt.Println("create req err:", err) - return - } - for { + if err != nil { + fmt.Println("create req err:", err) + return + } + for { go newRequest(client, req) - time.Sleep(time.Second / time.Duration(newFlow) / 6) + time.Sleep(time.Second / time.Duration(*requestPerSecond) / 6) sleepCounter += 1 - if sleepCounter >= newFlow { + if sleepCounter >= *requestPerSecond { timeElapsed := time.Since(timeStart) fmt.Printf("Create %d requests, cost time %v\n", sleepCounter, timeElapsed) if timeElapsed < time.Second { @@ -76,4 +68,4 @@ func main() { sleepCounter = 0 } } -} \ No newline at end of file +}