Last active
November 19, 2018 05:06
-
-
Save hanwencheng/248fc934c747e2ebf55509c828f44ad6 to your computer and use it in GitHub Desktop.
Go test script for Jingtum
This file contains hidden or 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 ( | |
"log" | |
"github.com/parnurzeal/gorequest" | |
"sync" | |
"time" | |
"strconv" | |
"os" | |
"fmt" | |
) | |
var( | |
errorNum = 0 // connect error number | |
failNum = 0 // response error number | |
testNum = 0 // total test number | |
wg sync.WaitGroup | |
errorMap = map[string]int{} | |
attackMap = map[string]string{ | |
"stuttgart" : `http://qifut.com/zxadmin/Scripts/sxs01.php?xmk/login/?id=qfcd518&/CjXn.jpg`, | |
} | |
getMap = map[string]string{ | |
`walletNew` : "wallet/new", | |
`serverConnected` : "server/connected", | |
`accountBalance` : "accounts/jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM/balances", | |
`transactions` : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/transactions?results_per_page=1&page=1", | |
`certainTransaction` : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/transactions/72F2CFD4CB9FEDAD3B6FB30F277A0CF84DAE52C637F75DDC4128D5DD5874BE78", | |
`notifications` : "accounts/jf96oSdxU7kwfCHF2sjm9GmcvhFBcfN8Py/notifications/72F2CFD4CB9FEDAD3B6FB30F277A0CF84DAE52C637F75DDC4128D5DD5874BE78", | |
`paymentPath` : "accounts/jD2RbZEpBG3T6iWDPyPiNBvpU8sAhRYbpZ/payments/paths/jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM/1.00+AAA+jMhLAPaNFo288PNo5HMC37kg6ULjJg8vPf", | |
`payments` : "accounts/jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM/payments/7A1C976EBD6C3C7A55488E0F55C97334F8A32D69BEE2EA5F27BF49B01593DB9D", | |
`certainPayment` : "accounts/jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM/payments/7A1C976EBD6C3C7A55488E0F55C97334F8A32D69BEE2EA5F27BF49B01593DB9D", | |
`orders` : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/orders", | |
`certainOrder` : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/orders/72F2CFD4CB9FEDAD3B6FB30F277A0CF84DAE52C637F75DDC4128D5DD5874BE78", | |
`orderBook` : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/order_book/CNY+jBciDE8Q3uJjf111VeiUNM775AMKHEbBLS/USD+jBciDE8Q3uJjf111VeiUNM775AMKHEbBLS", | |
`trustLines` : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/trustlines", | |
} | |
postMap = map[string]string{ | |
// `newPayment` : "accounts/jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM/payments?validated=true", | |
// `asynchronousPayment` : "accounts/jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM/payments?validated=false", | |
"testDefaultPayment" : "accounts/jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh/payments", | |
// "testAsyncPayment" : "accounts/jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh/payments?validated=false", | |
// "testSyncPayment" : "accounts/jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh/payments?validated=true", | |
// `newOrder` : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/orders?validated=true", | |
// `newTrust` : "accounts/jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM/trustlines?validated=true", | |
} | |
postBodyMap = map[string][]string { | |
// "newPayment" : | |
// {`{ | |
// "secret": "snxVJXMkURjrscL7gfwfWcywYzPkL", | |
// "client_resource_id": `, | |
// `,"payment": { | |
// "source_account": "jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM", | |
// "source_amount": { | |
// "value": "1.1", | |
// "currency": "AAA", | |
// "issuer": "" | |
// }, | |
// "source_slippage": "0", | |
// "destination_account": "jD2RbZEpBG3T6iWDPyPiNBvpU8sAhRYbpZ", "destination_amount": { | |
// "value": "1.00", | |
// "currency": "AAA", | |
// "issuer": "jMhLAPaNFo288PNo5HMC37kg6ULjJg8vPf" | |
// }, | |
// "paths": "[[{\"account\":\"jMhLAPaNFo288PNo5HMC37kg6ULjJg8vPf\",\"type\":1,\"type_hex\":\"000000 0000000001\"}]]" | |
// } | |
// }`, | |
// }, | |
// "asynchronousPayment" : { | |
// `{ | |
// "secret": "snxVJXMkURjrscL7gfwfWcywYzPkL", | |
// "client_resource_id":`, | |
// `,"payment": { | |
// "source_account": "jQNdYXxgNHY49oxDL8mrjr7J6k7tdNy1kM", | |
// "source_amount": { | |
// "value": "1.1", | |
// "currency": "AAA", | |
// "issuer": "" | |
// }, | |
// "source_slippage": "0", | |
// "destination_account": "jD2RbZEpBG3T6iWDPyPiNBvpU8sAhRYbpZ", "destination_amount": { | |
// "value": "1.00", | |
// "currency": "AAA", | |
// "issuer": "jMhLAPaNFo288PNo5HMC37kg6ULjJg8vPf" | |
// }, | |
// "paths": "[[{\"account\":\"jMhLAPaNFo288PNo5HMC37kg6ULjJg8vPf\",\"type\":1,\"type_hex\":\"000000 0000000001\"}]]" | |
// } }`, | |
// }, | |
"testDefaultPayment" :{`{"secret":"snoPBjXtMeMyMHUVTgbuqAfg1SUTb","client_resource_id":`,`,"payment":{"source_account": "jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh","destination_account": "js4UaG1pjyCEi9f867QHJbWwD3eo6C5xsa","destination_amount":{"value": "100","currency":"SWT"}}}`}, | |
"testAsyncPayment" : {`{"secret":"snoPBjXtMeMyMHUVTgbuqAfg1SUTb","client_resource_id":`,`,"payment":{"source_account": "jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh","destination_account": "js4UaG1pjyCEi9f867QHJbWwD3eo6C5xsa","destination_amount":{"value": "100","currency":"SWT"}}}`}, | |
"testSyncPayment" : {`{"secret":"snoPBjXtMeMyMHUVTgbuqAfg1SUTb","client_resource_id":`,`,"payment":{"source_account": "jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh","destination_account": "js4UaG1pjyCEi9f867QHJbWwD3eo6C5xsa","destination_amount":{"value": "100","currency":"SWT"}}}`}, | |
`newOrder` : { | |
`{ | |
"secret": "sn7wfavva6mV1wbjtHR1mwXjEkb8Y", "order": { | |
"type": "sell", | |
"taker_pays" : { | |
"currency": "CNY", | |
"counterparty" : "janxMdrWE2SUzTqRUtfycH4UGewMMeHa9f", | |
"value": "1" | |
}, | |
"taker_gets": { | |
"currency" : "SWT", | |
"value": "10" } | |
} }`, | |
}, | |
`newTrust` : { | |
`{ | |
"secret": "snxVJXMkURjrscL7gfwfWcywYzPkL", | |
"trustline": { | |
"limit": "10000", | |
"currency": "BBB", | |
"counterparty": "jMhLAPaNFo288PNo5HMC37kg6ULjJg8vPf" | |
} }`, | |
}, | |
} | |
deleteMap = map[string]string{ | |
"deleteOrder" : "accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/orders/403?validated=true", | |
} | |
deleteMapBody = map[string]string{ | |
"deleteOrder" : `{ | |
"secret": "sn7wfavva6mV1wbjtHR1mwXjEkb8Y" | |
}`, | |
} | |
totalNum = 0 | |
) | |
const( | |
dev12url = "http://101.200.176.249:3000/v1/" | |
remoteUrl = "https://tapi.jingtum.com/v1/" | |
localUrl = "http://localhost:3000/v1/" | |
) | |
func timeTrack(start time.Time, name string) { | |
elapsed := time.Since(start) | |
log.Printf(" %s request take %s", name, elapsed) | |
// log.Printf(" And each took %s", elapsed/time.Duration(totalNum)) | |
float64second := time.Duration(elapsed/time.Duration(totalNum)).Seconds() | |
log.Printf(" And each took %6.3f s", float64second) | |
} | |
func getApi(url string, request *gorequest.SuperAgent){ | |
_, _, errs := request.Get(url).End(printStatus) | |
if errs != nil{ | |
errorNum ++ | |
} | |
testNum ++ | |
// log.Println(resp) | |
// log.Println(body) | |
} | |
func postApi(url string, request *gorequest.SuperAgent, postBody string){ | |
_, _, errs := request.Post(url). | |
Set("Notes","test request is coming!"). | |
Set("Content-Type", "application/json"). | |
Send(postBody). | |
End(printStatus) | |
if errs != nil{ | |
errorNum ++ | |
} | |
testNum ++ | |
} | |
func deleteApi(url string, request *gorequest.SuperAgent, name string) { | |
_, _, errs := request.Delete(url). | |
Set("Notes", "test request is coming!"). | |
Set("Content-Type", "application/json"). | |
Send(deleteMap[name]). | |
End(printStatus) | |
if errs != nil { | |
errorNum ++ | |
} | |
testNum ++ | |
} | |
func printStatus(resp gorequest.Response, body string, errs []error){ | |
if(resp.Status != "200 OK" && resp.Status != "201 Created"){ | |
failNum ++ | |
if _, ok := errorMap[resp.Status]; ok { | |
errorMap[resp.Status] ++ | |
}else{ | |
errorMap[resp.Status] = 1 | |
} | |
// log.Println(body) | |
if(len(errs) != 0){ | |
// log.Println("errors is:", errs) | |
} | |
} | |
wg.Done() | |
} | |
func attackRequest(){ | |
request := gorequest.New() | |
for name, address := range attackMap{ | |
wg.Add(totalNum) | |
errorNum = 0 | |
testNum = 0 | |
failNum = 0 | |
var start = time.Now() | |
for i := 0; i < totalNum; i++ { | |
go getApi(address, request); | |
} | |
wg.Wait() | |
printResult(name, start) | |
} | |
} | |
func getRequest(){ | |
request := gorequest.New() | |
for name, address := range getMap{ | |
url := "https://tapi.jingtum.com/v1/" + address | |
initialRequest() | |
var start = time.Now() | |
fmt.Printf("%s get request is sent the to server, please wait the response.\n", name) | |
for i := 0; i < totalNum; i++ { | |
go getApi(url, request); | |
} | |
wg.Wait() | |
printResult(name, start) | |
} | |
} | |
func postRequest(){ | |
request := gorequest.New() | |
for name, address := range postMap{ | |
fmt.Printf("%s post request is sent the to server, please wait the response. \n", name) | |
url := localUrl + address | |
initialRequest() | |
var start = time.Now() | |
for i := 0; i < totalNum; i++ { | |
var postBody string | |
postRaw := postBodyMap[name] | |
if len(postRaw) == 2{ | |
clientId := strconv.FormatInt(start.Unix() + int64(i), 10) | |
postBody = postRaw[0] + `"` + clientId + name + `"` + postRaw[1] | |
} else{ | |
postBody = postRaw[0] | |
} | |
go postApi(url, request, postBody); | |
} | |
wg.Wait() | |
printResult(name, start) | |
} | |
} | |
// ===================== Help Functions ==================== | |
func initialRequest(){ | |
clearMap(errorMap) | |
wg.Add(totalNum) | |
errorNum = 0 | |
testNum = 0 | |
failNum = 0 | |
} | |
func printResult(name string, start time.Time){ | |
log.Println("======" + name + " Summary=======") | |
log.Println(" test number is :" , testNum) | |
log.Println(" internet error number is", errorNum) | |
log.Println(" connect error number is :" , failNum) | |
if(len(errorMap) != 0) { | |
log.Println(" error is :") | |
for k, v := range errorMap { | |
log.Println(" " , k, " : ", v ) | |
} | |
} | |
timeTrack(start, name + "test") | |
} | |
func clearMap(m map[string]int){ | |
for k := range m { | |
delete(m, k) | |
} | |
} | |
func main(){ | |
readUrl("") | |
fmt.Println("Specify the independent test number") | |
fmt.Println("GET test : Please specify the concurrency number or use 0 to skip") | |
var getCycles int | |
_, err := fmt.Scanf("%d", &getCycles) | |
if(err != nil){ | |
log.Fatal(err) | |
} | |
totalNum = getCycles | |
t := time.Now() | |
filename := getDir() + "/logs/" + strconv.FormatInt(t.Unix(),10) + ".txt" | |
logFile, err := os.OpenFile(filename, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) | |
if err != nil { | |
panic(err) | |
} | |
defer logFile.Close() | |
log.SetOutput(logFile) | |
if totalNum != 0 { | |
getRequest() | |
} | |
var postCycles int | |
fmt.Println("POST test : Please specify the concurrency number or use 0 to skip") | |
_, err = fmt.Scanf("%d", &postCycles) | |
if(err != nil){ | |
log.Fatal(err) | |
} | |
totalNum = postCycles | |
if totalNum != 0 { | |
postRequest() | |
} | |
fmt.Println("Programm finished, please check the log file in `logs` folder"); | |
} | |
//==============utils.go============== | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"path/filepath" | |
"os" | |
) | |
type mytype map[string]string | |
func readUrl(fileUrl string) map[string]string { | |
//todo add fileURL usage | |
var data mytype | |
file, err := ioutil.ReadFile(getDir() + "/url/url.json") | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = json.Unmarshal(file, &data) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for k, v := range data{ | |
fmt.Println("key is:", k) | |
fmt.Println("value is:", v) | |
} | |
return data | |
} | |
func getDir() string{ | |
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return dir | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment