Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eureka additional uri param #3

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ or:
| `-reportLatenciesCSV` | `<none>` | Filename to write CSV latency values. Format of CSV is millisecond buckets with number of requests in each bucket. |
| `-timeout` | 10s | Individual request timeout. |
| `-totalRequests` | `<none>` | Exit after sending this many requests. |
| `-useEureka ` | `<none>` | If set, slow_cooker will try to get data from EurekaAPI by url argument.|
| `-eurekaService ` | `<none>` | If set, slow_cooker will filter instances list by app tag.|
| `-eurekaExtraUri ` | `<none>` | If set, slow_cooker will append it to url from EurekaAPI.|
| `-help` | `<unset>` | If set, print all available flags and exit. |

# Using a URL file
Expand Down
92 changes: 92 additions & 0 deletions eurekaUrlsProvider/eurekaUrlsProvider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package eurekaurlsprovider

import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)

type Applications struct {
XMLName xml.Name `xml:"applications"`
Applications []Application `xml:"application"`
}
type Application struct {
XMLName xml.Name `xml:"application"`
Name string `xml:"name"`
Instance Instance `xml:"instance"`
}
type Instance struct {
XMLName xml.Name `xml:"instance"`
App string `xml:"app"`
HostName string `xml:"hostName"`
IpAddr string `xml:"ipAddr"`
Status string `xml:"status"`
Port Port `xml:"port"`
SecurePort SecurePort `xml:"securePort"`
}

type Port struct {
XMLName xml.Name `xml:"port"`
Enabled bool `xml:"enabled,attr"`
PortValue string `xml:",innerxml"`
}

type SecurePort struct {
XMLName xml.Name `xml:"securePort"`
Enabled bool `xml:"enabled,attr"`
PortValue string `xml:",innerxml"`
}

func LoadEurekaURLs(urldest string, eurekaService string, eurekaExtraUri string) []*url.URL {
var urls []*url.URL
resp, err := http.Get(urldest)

if err != nil {
panic(err)
}

b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}

defer resp.Body.Close()

var applications Applications
err = xml.Unmarshal(b, &applications)
if err != nil {
fmt.Println(err)
panic(err)
}

for i := 0; i < len(applications.Applications); i++ {
if strings.Contains(applications.Applications[i].Name, eurekaService) {
rawUrl := applications.Applications[i].Instance.HostName
if !strings.HasPrefix(rawUrl, "http") {
if applications.Applications[i].Instance.Port.Enabled {
rawUrl = "http://" + rawUrl
} else if applications.Applications[i].Instance.SecurePort.Enabled {
rawUrl = "https://" + rawUrl
}
}

if applications.Applications[i].Instance.Port.Enabled {
rawUrl = rawUrl + ":" + applications.Applications[i].Instance.Port.PortValue
} else if applications.Applications[i].Instance.SecurePort.Enabled {
rawUrl = rawUrl + ":" + applications.Applications[i].Instance.SecurePort.PortValue
}

URL, err := url.Parse(rawUrl + eurekaExtraUri)
if err != nil {
panic(err)
}

urls = append(urls, URL)
}
}

return urls
}
23 changes: 16 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"syscall"
"time"

eurekaurlsprovider "github.com/buoyantio/slow_cooker/eurekaUrlsProvider"
"github.com/buoyantio/slow_cooker/hdrreport"
"github.com/buoyantio/slow_cooker/ring"
"github.com/buoyantio/slow_cooker/window"
Expand Down Expand Up @@ -322,6 +323,9 @@ func main() {
metricAddr := flag.String("metric-addr", "", "address to serve metrics on")
hashValue := flag.Uint64("hashValue", 0, "fnv-1a hash value to check the request body against")
hashSampleRate := flag.Float64("hashSampleRate", 0.0, "Sampe Rate for checking request body's hash. Interval in the range of [0.0, 1.0]")
useEureka := flag.Bool("useEureka", false, "Eureka will be used for getting urls list by a specific service")
eurekaService := flag.String("eurekaService", "", "Specify service from Eureka's list for testing. % may be used as wildcard")
eurekaExtraUri := flag.String("eurekaExtraUri", "", "If set, slow_cooker will append it to url from EurekaAPI.")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <url> [flags]\n", path.Base(os.Args[0]))
Expand All @@ -340,7 +344,13 @@ func main() {
}

urldest := flag.Arg(0)
dstURLs := loadURLs(urldest)

var dstURLs []*url.URL
if *useEureka {
dstURLs = eurekaurlsprovider.LoadEurekaURLs(urldest, *eurekaService, *eurekaExtraUri)
} else {
dstURLs = loadURLs(urldest)
}

if *qps < 1 {
exUsage("qps must be at least 1")
Expand Down Expand Up @@ -407,10 +417,8 @@ func main() {
}

fmt.Printf("# %s iter good/b/f t goal%% %s min [p50 p95 p99 p999] max bhash change\n", timePadding, intPadding)
stride := *concurrency
if stride > len(dstURLs) {
stride = 1
}

callTimes := make([]int, len(dstURLs))
for i := 0; i < *concurrency; i++ {
ticker := time.NewTicker(timeToWait)
go func(offset int) {
Expand All @@ -429,15 +437,16 @@ func main() {
shouldFinishLock.RLock()
if !shouldFinish {
shouldFinishLock.RUnlock()
callTimes[y]++
sendRequest(client, *method, dstURLs[y], hosts[rand.Intn(len(hosts))], headers, requestData, atomic.AddUint64(&reqID, 1), *noreuse, *hashValue, checkHash, hasher, received, bodyBuffer)
} else {
shouldFinishLock.RUnlock()
sendTraffic.Done()
return
}
y += stride
y++
if y >= len(dstURLs) {
y = offset
y = 0
}
}
}(i % len(dstURLs))
Expand Down