Closed jasondellaluce closed 2 years ago
Looking forward to gather some feedback on this!
Here's the report of the benchmarks of these two implementations on my machine. The code is inside sdk/plugins/source/instance_test.go
.
Terminology:
The benchmarks generates 1024 events, with a batch size of 128 elements and a timeout of 30 milliseconds.
goos: linux
goarch: amd64
pkg: github.com/falcosecurity/plugin-sdk-go/pkg/sdk/plugins/source
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkPullEmpty-8 14881 71888 ns/op 0 B/op 0 allocs/op
BenchmarkPushEmpty-8 2679 378497 ns/op 0 B/op 0 allocs/op
BenchmarkPullFixed-8 364 3170573 ns/op 2277739 B/op 5120 allocs/op
BenchmarkPushFixed-8 249 4100726 ns/op 2277947 B/op 5121 allocs/op
BenchmarkPullRandom-8 1 1599520398 ns/op 1931069360 B/op 14858 allocs/op
BenchmarkPushRandom-8 1 1688147566 ns/op 1995180088 B/op 14738 allocs/op
PASS
ok github.com/falcosecurity/plugin-sdk-go/pkg/sdk/plugins/source 9.950s
Takeaways:
The source
example has been updated to use the "pull" prebuilt. See: https://github.com/falcosecurity/plugin-sdk-go/pull/50/files#diff-9c5325f5e8159881ad3abf0eab413b61070b1b51276cd878f764c130cf11ac20R84
func (m *MyPlugin) Open(params string) (source.Instance, error) {
counter := 0
pull := func(ctx context.Context, evt sdk.EventWriter) error {
counter++
if err := gob.NewEncoder(evt.Writer()).Encode(counter); err != nil {
return err
}
evt.SetTimestamp(uint64(time.Now().UnixNano()))
return nil
}
return source.NewPullInstance(pull)
}
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: jasondellaluce, leogr
The full list of commands accepted by this bot can be found here.
The pull request process is described here
LGTM label has been added.
What type of PR is this?
/kind feature
Any specific area of the project related to this PR?
/area plugin-sdk
/area tests
What this PR does / why we need it:
This PR introduces two builtin implementations of the
source.Instance
interface of thesdk/plugins/source
package. This interface is responsible of representing opened event sources and of producing event data through theNextBatch()
method.There are two built-in implementations provided, each implemented for its use case:
On top of this, in-memory implementations have been added for core SDK interfaces such as
sdk.EventWriter
andsdk.EventReader
. This is now used to implement benchmarks and unit tests for the two push and pull implementations. This was a long due necessity in the SDK, and these newsdk.InMemory*
types can be used for a variety of use cases (tests being the most obvious ones).Which issue(s) this PR fixes:
Special notes for your reviewer:
After starting developing the first plugins, I came to the conclusion that implementing the
NextBatch()
is not trivial in the general case. There are many things to be aware of:Close()
is called beforeNextBatch()
has returned. This can happen if the plugin framework receives signals such asCTRL+C
from the user.As such, I though that creating an officially-maintained version of it for few use cases would be beneficial for the community. As a matter of fact, most of the plugins under the
falcosecurity/plugins
repo could be refactored to use these newly-introduced prebuilts.Obviously, the SDK leaves plugin developers the choice of implementing their own
source.Instance
type with customNextBatch()
implementations for advanced or specific use cases.Does this PR introduce a user-facing change?: