Skip to content

Commit

Permalink
GH-85: custom_pandora example
Browse files Browse the repository at this point in the history
  • Loading branch information
skipor committed Feb 20, 2018
1 parent 983295b commit 8fb991c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
File renamed without changes.
27 changes: 27 additions & 0 deletions examples/custom_pandora/custom.yaml
Original file line number Diff line number Diff line change
@@ -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
89 changes: 89 additions & 0 deletions examples/custom_pandora/custom_main.go
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>

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()
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8fb991c

Please sign in to comment.