Value generator initial commit

This commit is contained in:
2025-05-30 10:40:20 +04:00
parent 924bd819ee
commit f248c2af96
5 changed files with 81 additions and 50 deletions

View File

@@ -1,14 +1,18 @@
package requests
import (
"bytes"
"context"
"math/rand"
"net/url"
"strings"
"text/template"
"time"
"github.com/aykhans/dodo/config"
"github.com/aykhans/dodo/types"
"github.com/aykhans/dodo/utils"
"github.com/brianvoe/gofakeit/v7"
"github.com/valyala/fasthttp"
)
@@ -21,6 +25,11 @@ type Request struct {
getRequest RequestGeneratorFunc
}
type keyValueGenerator struct {
key func() string
value func() string
}
// Send sends the HTTP request using the fasthttp client with a specified timeout.
// It returns the HTTP response or an error if the request fails or times out.
func (r *Request) Send(ctx context.Context, timeout time.Duration) (*fasthttp.Response, error) {
@@ -101,17 +110,10 @@ func getRequestGeneratorFunc(
bodies []string,
localRand *rand.Rand,
) RequestGeneratorFunc {
bodiesLen := len(bodies)
getBody := func() string { return "" }
if bodiesLen == 1 {
getBody = func() string { return bodies[0] }
} else if bodiesLen > 1 {
getBody = utils.RandomValueCycle(bodies, localRand)
}
getParams := getKeyValueGeneratorFunc(params, localRand)
getHeaders := getKeyValueGeneratorFunc(headers, localRand)
getCookies := getKeyValueGeneratorFunc(cookies, localRand)
getBody := getValueFunc(bodies, newFuncMap(localRand), localRand)
return func() *fasthttp.Request {
return newFasthttpRequest(
@@ -199,45 +201,75 @@ func getKeyValueGeneratorFunc[
keyValueSlice []types.KeyValue[string, []string],
localRand *rand.Rand,
) func() T {
getKeyValueSlice := []map[string]func() string{}
isRandom := false
keyValueGenerators := make([]keyValueGenerator, len(keyValueSlice))
for _, kv := range keyValueSlice {
if valuesLen := len(kv.Value); valuesLen > 1 {
isRandom = true
funcMap := newFuncMap(localRand)
for i, kv := range keyValueSlice {
keyValueGenerators[i] = keyValueGenerator{
key: getKeyFunc(kv.Key, funcMap),
value: getValueFunc(kv.Value, funcMap, localRand),
}
getKeyValueSlice = append(
getKeyValueSlice,
map[string]func() string{
kv.Key: utils.RandomValueCycle(kv.Value, localRand),
},
)
}
if isRandom {
return func() T {
keyValues := make(T, len(getKeyValueSlice))
for i, keyValue := range getKeyValueSlice {
for key, value := range keyValue {
keyValues[i] = types.KeyValue[string, string]{
Key: key,
Value: value(),
}
}
}
return keyValues
}
} else {
keyValues := make(T, len(getKeyValueSlice))
for i, keyValue := range getKeyValueSlice {
for key, value := range keyValue {
keyValues[i] = types.KeyValue[string, string]{
Key: key,
Value: value(),
}
return func() T {
keyValues := make(T, len(keyValueGenerators))
for i, keyValue := range keyValueGenerators {
keyValues[i] = types.KeyValue[string, string]{
Key: keyValue.key(),
Value: keyValue.value(),
}
}
return func() T { return keyValues }
return keyValues
}
}
func getKeyFunc(key string, funcMap template.FuncMap) func() string {
t, err := template.New("default").Funcs(funcMap).Parse(key)
if err != nil {
panic(err)
}
return func() string {
var buf bytes.Buffer
_ = t.Execute(&buf, nil)
return buf.String()
}
}
func getValueFunc(
values []string,
funcMap template.FuncMap,
localRand *rand.Rand,
) func() string {
templates := make([]*template.Template, len(values))
for i, value := range values {
t, err := template.New("default").Funcs(funcMap).Parse(value)
if err != nil {
panic(err)
}
templates[i] = t
}
randomTemplateFunc := utils.RandomValueCycle(templates, localRand)
return func() string {
if tmpl := randomTemplateFunc(); tmpl == nil {
return ""
} else {
var buf bytes.Buffer
_ = tmpl.Execute(&buf, nil)
return buf.String()
}
}
}
func newFuncMap(localRand *rand.Rand) template.FuncMap {
localFaker := gofakeit.NewFaker(localRand, false)
return template.FuncMap{
"upper": strings.ToUpper,
"fakeit_Name": localFaker.Name,
}
}

View File

@@ -72,7 +72,7 @@ func releaseDodos(
wg.Add(int(dodosCount))
streamWG.Add(1)
streamCtx, streamCtxCancel := context.WithCancel(context.Background())
streamCtx, streamCtxCancel := context.WithCancel(ctx)
go streamProgress(streamCtx, &streamWG, requestConfig.RequestCount, "Dodos Working🔥", increase)