-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from ddosify/develop
Develop
- Loading branch information
Showing
26 changed files
with
968 additions
and
52 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"iteration_count": 4, | ||
"load_type": "waved", | ||
"duration": 1, | ||
"steps": [ | ||
{ | ||
"id": 2, | ||
"url": "{{LOCAL}}/body", | ||
"name": "JSON", | ||
"method": "GET", | ||
"others": { | ||
"h2": false, | ||
"keep-alive": true, | ||
"disable-redirect": true, | ||
"disable-compression": false | ||
}, | ||
"payload_file": "../config/config_testdata/data_json_payload.json", | ||
"timeout": 10 | ||
} | ||
], | ||
"output": "stdout", | ||
"env":{ | ||
"HTTPBIN" : "https://httpbin.ddosify.com", | ||
"LOCAL" : "http://localhost:8084", | ||
"RANDOM_NAMES" : ["kenan","fatih","kursat","semih","sertac"] , | ||
"RANDOM_INT" : [52,99,60,33], | ||
"RANDOM_BOOL" : [true,true,true,false] | ||
}, | ||
"data":{ | ||
"info": { | ||
"path" : "../config/config_testdata/test.csv", | ||
"src" : "local", | ||
"delimiter": ";", | ||
"vars": { | ||
"0":{"tag":"name"}, | ||
"1":{"tag":"city"}, | ||
"2":{"tag":"team"}, | ||
"3":{"tag":"payload", "type":"json"}, | ||
"4":{"tag":"age", "type":"int"} | ||
}, | ||
"allowQuota" : true, | ||
"order": "random", | ||
"skipFirstLine" : true | ||
} | ||
}, | ||
"debug" : false | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name" : "{{data.info.name}}", | ||
"team" : "{{data.info.team}}", | ||
"city" : "{{data.info.city}}", | ||
"payload" : "{{rand(data.info.payload)}}", | ||
"age" : "{{data.info.age}}" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Username;City;Team;Payload;Age;Percent;BoolField;;; | ||
Kenan;Tokat;Galatasaray;{"data":{"profile":{"name":"Kenan"}}};25;22.3;true;;; | ||
Fatih;Bolu;Galatasaray;[5,6,7];29;44.3;false;;; | ||
Kursat;Samsun;Besiktas;{"a":"b"};28;12.54;True;;; | ||
Semih;Duzce;Besiktas;{"a":"b"};27;663.67;False;;; | ||
;;;;;;;;; | ||
;;;;;;;;; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func validateConf(conf CsvConf) error { | ||
if !(conf.Order == "random" || conf.Order == "sequential") { | ||
return fmt.Errorf("unsupported order %s, should be random|sequential", conf.Order) | ||
} | ||
return nil | ||
} | ||
|
||
func readCsv(conf CsvConf) ([]map[string]interface{}, error) { | ||
err := validateConf(conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var reader io.Reader | ||
|
||
if _, err = url.ParseRequestURI(conf.Path); err == nil { // url | ||
req, err := http.NewRequest(http.MethodGet, conf.Path, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) { | ||
return nil, fmt.Errorf("request to remote url failed: %d", resp.StatusCode) | ||
} | ||
reader = resp.Body | ||
defer resp.Body.Close() | ||
} else if _, err = os.Stat(conf.Path); err == nil { // local file path | ||
f, err := os.Open(conf.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reader = f | ||
defer f.Close() | ||
} else { | ||
return nil, err | ||
} | ||
|
||
// read csv values using csv.Reader | ||
csvReader := csv.NewReader(reader) | ||
csvReader.Comma = []rune(conf.Delimiter)[0] | ||
csvReader.TrimLeadingSpace = true | ||
csvReader.LazyQuotes = conf.AllowQuota | ||
|
||
data, err := csvReader.ReadAll() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if conf.SkipFirstLine { | ||
data = data[1:] | ||
} | ||
|
||
rt := make([]map[string]interface{}, 0) // unclear how many empty line exist | ||
|
||
for _, row := range data { | ||
if conf.SkipEmptyLine && emptyLine(row) { | ||
continue | ||
} | ||
x := map[string]interface{}{} | ||
for index, tag := range conf.Vars { | ||
i, err := strconv.Atoi(index) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if i >= len(row) { | ||
return nil, fmt.Errorf("index number out of range, check your vars or delimiter") | ||
} | ||
|
||
// convert | ||
var val interface{} | ||
switch tag.Type { | ||
case "json": | ||
err := json.Unmarshal([]byte(row[i]), &val) | ||
if err != nil { | ||
return nil, fmt.Errorf("can not convert %s to json,%v", row[i], err) | ||
} | ||
case "int": | ||
var err error | ||
val, err = strconv.Atoi(row[i]) | ||
if err != nil { | ||
return nil, fmt.Errorf("can not convert %s to int,%v", row[i], err) | ||
} | ||
case "float": | ||
var err error | ||
val, err = strconv.ParseFloat(row[i], 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("can not convert %s to float,%v", row[i], err) | ||
} | ||
case "bool": | ||
var err error | ||
val, err = strconv.ParseBool(row[i]) | ||
if err != nil { | ||
return nil, fmt.Errorf("can not convert %s to bool,%v", row[i], err) | ||
} | ||
default: | ||
val = row[i] | ||
} | ||
x[tag.Tag] = val | ||
} | ||
rt = append(rt, x) | ||
} | ||
|
||
return rt, nil | ||
} | ||
|
||
func emptyLine(row []string) bool { | ||
for _, field := range row { | ||
if field != "" { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
Oops, something went wrong.