Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andrew Neudegg <andrew.neudegg@finbourne.com>
- Loading branch information
1 parent
60c323c
commit 0055238
Showing
16 changed files
with
285 additions
and
36 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,6 @@ | ||
package noop | ||
|
||
const ( | ||
// ID for this collection of resources. | ||
ID = "examples/noop" | ||
) |
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
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,6 @@ | ||
package performance1 | ||
|
||
const ( | ||
// ID for this collection of resources. | ||
ID = "utilities/performance/v1" | ||
) |
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,65 @@ | ||
package performance1 | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/andrewneudegg/delta/pkg/events" | ||
"github.com/andrewneudegg/delta/pkg/resources/definitions" | ||
"github.com/pkg/errors" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Input is simple wrapper around another input resource. | ||
type Input struct { | ||
i definitions.Input | ||
sampleWindow time.Duration | ||
} | ||
|
||
// ID defines what this thing is. | ||
func (i Input) ID() string { | ||
return ID | ||
} | ||
|
||
// Type defines what type of resource this is. | ||
func (i Input) Type() definitions.ResourceType { | ||
return definitions.InputType | ||
} | ||
|
||
// DoInput will accept collections of events, passing them into the channel. | ||
func (i Input) DoInput(ctx context.Context, ch chan<- events.Collection) error { | ||
log.Infof("starting DoInput proxy for '%s'", i.i.ID()) | ||
if i.i == nil { | ||
return errors.Errorf("'%s' cannot be used as an input resource directly", ID) | ||
} | ||
|
||
proxyCh := make(chan events.Collection) | ||
count := 0 | ||
lastTime := time.Now() | ||
|
||
go func(chIn chan events.Collection, chOut chan<- events.Collection) { | ||
for { | ||
select { | ||
case eCol := <-chIn: | ||
count++ | ||
chOut <- eCol | ||
} | ||
} | ||
}(proxyCh, ch) | ||
|
||
go func() { | ||
for { | ||
time.Sleep(i.sampleWindow) | ||
|
||
tDiff := time.Now().Sub(lastTime) | ||
metricFrame := float64(count) / tDiff.Seconds() | ||
log.Warnf("'%s' at '%f' tx/s (%d transactions / %f seconds)", i.i.ID(), metricFrame, count, tDiff.Seconds()) | ||
|
||
lastTime = time.Now() | ||
count = 0 | ||
} | ||
}() | ||
|
||
return i.i.DoInput(ctx, proxyCh) | ||
} |
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,84 @@ | ||
package performance1 | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/andrewneudegg/delta/pkg/resources/definitions" | ||
"github.com/pkg/errors" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Meta is simple noop. | ||
type Meta struct { | ||
SampleWindow string `mapstructure:"sampleWindow"` // SampleWindow is the time that metrics are collected before reporting (i.e. 60s) | ||
} | ||
|
||
// ID defines what this thing is. | ||
func (m Meta) ID() string { | ||
return ID | ||
} | ||
|
||
// Type defines what type of resource this is. | ||
func (m Meta) Type() definitions.ResourceType { | ||
return definitions.MetaType | ||
} | ||
|
||
// I accepts inputs returning an input representing the given collection. | ||
func (m Meta) I(i []definitions.Input) (definitions.Input, error) { | ||
if len(i) != 1 { | ||
return nil, fmt.Errorf("'%s' can only accept one input, got '%d'", ID, len(i)) | ||
} | ||
|
||
dur, err := time.ParseDuration(m.SampleWindow) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not parse '%s' as duration", m.SampleWindow) | ||
} | ||
|
||
log.Debugf("returning meta input for '%s'", i[0].ID()) | ||
|
||
return Input{ | ||
i: i[0], | ||
sampleWindow: dur, | ||
}, nil | ||
} | ||
|
||
// P accepts process returning an process representing the given collection. | ||
func (m Meta) P(p []definitions.Process) (definitions.Process, error) { | ||
if len(p) != 1 { | ||
return nil, fmt.Errorf("'%s' can only accept one process, got '%d'", ID, len(p)) | ||
} | ||
|
||
dur, err := time.ParseDuration(m.SampleWindow) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not parse '%s' as duration", m.SampleWindow) | ||
} | ||
|
||
return Process{ | ||
p: p[0], | ||
sampleWindow: dur, | ||
}, nil | ||
} | ||
|
||
// O accepts outputs returning an output representing the given collection. | ||
func (m Meta) O(o []definitions.Output) (definitions.Output, error) { | ||
if len(o) != 1 { | ||
return nil, fmt.Errorf("'%s' can only accept one output, got '%d'", ID, len(o)) | ||
} | ||
|
||
dur, err := time.ParseDuration(m.SampleWindow) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not parse '%s' as duration", m.SampleWindow) | ||
} | ||
|
||
return Output{ | ||
o: o[0], | ||
sampleWindow: dur, | ||
}, nil | ||
} | ||
|
||
// M accepts meta resources returning a meta input for the given collection. | ||
func (m Meta) M(me []definitions.Meta) (definitions.Meta, error) { | ||
return Meta{}, fmt.Errorf("'%s' is not designed to return a meta resource", ID) | ||
} |
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,36 @@ | ||
package performance1 | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/andrewneudegg/delta/pkg/events" | ||
"github.com/andrewneudegg/delta/pkg/resources/definitions" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Output is simple noop. | ||
type Output struct { | ||
o definitions.Output | ||
sampleWindow time.Duration | ||
} | ||
|
||
// ID defines what this thing is. | ||
func (o Output) ID() string { | ||
return ID | ||
} | ||
|
||
// Type defines what type of resource this is. | ||
func (o Output) Type() definitions.ResourceType { | ||
return definitions.OutputType | ||
} | ||
|
||
// DoOutput will perform its function on each collection placed into the channel. | ||
func (o Output) DoOutput(ctx context.Context, ch <-chan events.Collection) error { | ||
if o.o == nil { | ||
return errors.Errorf("'%s' cannot be used as an output resource directly", ID) | ||
} | ||
|
||
<-ctx.Done() | ||
return nil | ||
} |
Oops, something went wrong.