Created
October 21, 2020 09:25
-
-
Save BarryDeng/718b155d63f1db3e944c5459a965d27a to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net" | |
"sync" | |
"time" | |
) | |
func main() { | |
tasksNum := flag.Int("num", 10, "number of requests") | |
addr := flag.String("addr", "127.0.0.1:80", "address of request") | |
resultFile := flag.String("file", "/tmp/ddos", "file stored success ratio") | |
flag.Parse() | |
beg := time.Now() | |
successNum := 0 | |
wg := &sync.WaitGroup{} | |
for i := 0; i < *tasksNum; i++ { | |
wg.Add(1) | |
go func(addr string, group *sync.WaitGroup) { | |
err := establishConn(addr) | |
if err == nil { | |
successNum++ | |
} | |
group.Done() | |
}(*addr, wg) | |
} | |
wg.Wait() | |
fmt.Printf("time consumed: %fs\n", time.Now().Sub(beg).Seconds()) | |
successRatio := float32(successNum) / float32(*tasksNum) | |
fmt.Printf("success ratio: %d, %f\n", successNum, successRatio) | |
if err := ioutil.WriteFile(*resultFile, []byte(fmt.Sprintf("%f", successRatio)), 0777); err != nil { | |
log.Fatalf("write file error: %v", err) | |
} | |
} | |
func establishConn(addr string) error { | |
_, err := net.Dial("tcp", addr) | |
if err != nil { | |
log.Printf("%s: dial error: %s", addr, err) | |
return err | |
} | |
// log.Println(i, ":connect to server ok") | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment