From 8fb991cd70cfadc2117257992c728d259024e42e Mon Sep 17 00:00:00 2001 From: Vladimir Skipor Date: Tue, 20 Feb 2018 09:01:24 +0300 Subject: [PATCH] GH-85: custom_pandora example --- {example => examples}/connect.yaml | 0 examples/custom_pandora/custom.yaml | 27 ++++++++ examples/custom_pandora/custom_main.go | 89 ++++++++++++++++++++++++++ {example => examples}/example.yaml | 0 {example => examples}/http.jsonline | 0 {example => examples}/http.yaml | 0 6 files changed, 116 insertions(+) rename {example => examples}/connect.yaml (100%) create mode 100644 examples/custom_pandora/custom.yaml create mode 100644 examples/custom_pandora/custom_main.go rename {example => examples}/example.yaml (100%) rename {example => examples}/http.jsonline (100%) rename {example => examples}/http.yaml (100%) diff --git a/example/connect.yaml b/examples/connect.yaml similarity index 100% rename from example/connect.yaml rename to examples/connect.yaml diff --git a/examples/custom_pandora/custom.yaml b/examples/custom_pandora/custom.yaml new file mode 100644 index 000000000..b109629cd --- /dev/null +++ b/examples/custom_pandora/custom.yaml @@ -0,0 +1,27 @@ +pools: +- gun: + type: my-custom-gun-name + target: example.com:80 + + ammo: + type: my-custom-provider-name + source: # You may just write file path here. Or stdin. + type: inline + data: | + {"url": "url1", "queryParam": "query1"} + {"url": "url2", "queryParam": "query2"} + + result: + type: json + sink: stderr # Just for interactivity print result to stderr. Usually file user. + + rps: + - {duration: 2s, type: line, from: 1, to: 5} + - {duration: 3s, type: const, ops: 5} + - {duration: 2s, type: line, from: 5, to: 1} + startup: + type: once + times: 5 + +log: + level: debug diff --git a/examples/custom_pandora/custom_main.go b/examples/custom_pandora/custom_main.go new file mode 100644 index 000000000..d4c8b185f --- /dev/null +++ b/examples/custom_pandora/custom_main.go @@ -0,0 +1,89 @@ +// Copyright (c) 2017 Yandex LLC. All rights reserved. +// Use of this source code is governed by a MPL 2.0 +// license that can be found in the LICENSE file. +// Author: Vladimir Skipor + +package main + +import ( + "math/rand" + "time" + + "github.com/spf13/afero" + "go.uber.org/zap" + + "github.com/yandex/pandora/cli" + "github.com/yandex/pandora/components/phttp/import" + "github.com/yandex/pandora/core" + "github.com/yandex/pandora/core/import" + "github.com/yandex/pandora/core/register" +) + +type Ammo struct { + URL string + QueryParam string +} + +type Sample struct { + URL string + ShootTimeSeconds float64 +} + +type GunConfig struct { + Target string `validate:"required"` // Configuration will fail, without target defined +} + +type Gun struct { + // Configured on construction. + conf GunConfig + + // Configured on Bind, before shooting. + aggr core.Aggregator // May be your custom Aggregator. + core.GunDeps +} + +func NewGun(conf GunConfig) *Gun { + return &Gun{conf: conf} +} + +func (g *Gun) Bind(aggr core.Aggregator, deps core.GunDeps) error { + g.aggr = aggr + g.GunDeps = deps + return nil +} + +func (g *Gun) Shoot(ammo core.Ammo) { + customAmmo := ammo.(*Ammo) // Shoot will panic on unexpected ammo type. Panic cancels shooting. + g.shoot(customAmmo) +} + +func (g *Gun) shoot(ammo *Ammo) { + start := time.Now() + defer func() { + g.aggr.Report(Sample{ammo.URL, time.Since(start).Seconds()}) + }() + // Put your shoot logic here. + g.Log.Info("Custom shoot", zap.String("target", g.conf.Target), zap.Any("ammo", ammo)) + time.Sleep(time.Duration(rand.Float64() * float64(time.Second))) +} + +func main() { + // Standard imports. + fs := afero.NewOsFs() + coreimport.Import(fs) + + // May not be imported, if you don't need http guns and etc. + phttp.Import(fs) + + // Custom imports. Integrate your custom types into configuration system. + coreimport.RegisterCustomJSONProvider("my-custom-provider-name", func() core.Ammo { return &Ammo{} }) + + register.Gun("my-custom-gun-name", NewGun, func() GunConfig { + return GunConfig{ + Target: "default target", + } + }) + register.Gun("my-custom/no-default", NewGun) + + cli.Run() +} diff --git a/example/example.yaml b/examples/example.yaml similarity index 100% rename from example/example.yaml rename to examples/example.yaml diff --git a/example/http.jsonline b/examples/http.jsonline similarity index 100% rename from example/http.jsonline rename to examples/http.jsonline diff --git a/example/http.yaml b/examples/http.yaml similarity index 100% rename from example/http.yaml rename to examples/http.yaml