86 lines
1.5 KiB
Go
86 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
llog "github.com/TransX/log"
|
|
"github.com/TransX/protocol"
|
|
"github.com/davecheney/profile"
|
|
"io/ioutil"
|
|
"log"
|
|
"math/rand"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var mux sync.Mutex
|
|
var benchSeed int
|
|
|
|
func transClient() {
|
|
t := protocol.NewTransTCP()
|
|
t.Start("1200", "127.0.0.1", "1201", "client")
|
|
}
|
|
|
|
func transServer() {
|
|
t := protocol.NewTransTCP()
|
|
t.Start("1201", "192.168.56.101", "80", "server")
|
|
}
|
|
|
|
func doHttp(c chan int) {
|
|
resp, err := http.Get("http://127.0.0.1:1200/test.bin")
|
|
if err != nil {
|
|
log.Println("could not get:", err)
|
|
return
|
|
}
|
|
defer func() {
|
|
resp.Body.Close()
|
|
resp.Close = true
|
|
log.Println("body close")
|
|
}()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
log.Printf("got %d bytes\n", len(body))
|
|
if err != nil {
|
|
log.Println("not get:", err)
|
|
} else {
|
|
|
|
}
|
|
c <- 0
|
|
|
|
}
|
|
|
|
func randMillionSecond() time.Duration {
|
|
mux.Lock()
|
|
benchSeed++
|
|
defer mux.Unlock()
|
|
s := rand.NewSource(int64(benchSeed))
|
|
r := rand.New(s)
|
|
return time.Duration(r.Int() % 1000)
|
|
}
|
|
|
|
func attack() {
|
|
c := make(chan int)
|
|
for i := 0; i < 20; i++ {
|
|
time.Sleep(time.Millisecond * randMillionSecond())
|
|
go doHttp(c)
|
|
}
|
|
for i := 0; i < 20; i++ {
|
|
<-c
|
|
}
|
|
log.Println("Finish")
|
|
}
|
|
|
|
func main1() {
|
|
cfg := profile.Config{
|
|
MemProfile: true,
|
|
ProfilePath: "./profile", // store profiles in current directory
|
|
NoShutdownHook: true, // do not hook SIGINT
|
|
CPUProfile: true,
|
|
BlockProfile: true,
|
|
}
|
|
llog.LogTo("applog/log.txt", "ERROR")
|
|
defer profile.Start(&cfg).Stop()
|
|
benchSeed = 0
|
|
go transClient()
|
|
go transServer()
|
|
attack()
|
|
}
|