Skip to content

Commit 4f0df13

Browse files
authored
Merge pull request #8 from dmcgowan/perf-optimization
Add benchmark and optimize performance of common functions
2 parents 052f853 + 60d3c68 commit 4f0df13

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (registry Registry) Graph(filter DisableFilter) []Registration {
135135
func children(reg *Registration, registry []*Registration, added, disabled map[*Registration]bool, ordered *[]Registration) {
136136
for _, t := range reg.Requires {
137137
for _, r := range registry {
138-
if !disabled[r] && r.URI() != reg.URI() && (t == "*" || r.Type == t) {
138+
if (t == "*" || r.Type == t) && r != reg && !disabled[r] {
139139
children(r, registry, added, disabled, ordered)
140140
if !added[r] {
141141
*ordered = append(*ordered, *r)
@@ -170,7 +170,7 @@ func (registry Registry) Register(r *Registration) Registry {
170170

171171
func checkUnique(registry Registry, r *Registration) error {
172172
for _, registered := range registry {
173-
if r.URI() == registered.URI() {
173+
if r.Type == registered.Type && r.ID == registered.ID {
174174
return fmt.Errorf("%s: %w", r.URI(), ErrIDRegistered)
175175
}
176176
}

plugin_test.go

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ func mockPluginFilter(*Registration) bool {
2626
return false
2727
}
2828

29-
// TestContainerdPlugin tests the logic of Graph, use the containerd's plugin
30-
func TestContainerdPlugin(t *testing.T) {
31-
// Plugin types commonly used by containerd
32-
const (
33-
InternalPlugin Type = "io.containerd.internal.v1"
34-
RuntimePlugin Type = "io.containerd.runtime.v1"
35-
RuntimePluginV2 Type = "io.containerd.runtime.v2"
36-
ServicePlugin Type = "io.containerd.service.v1"
37-
GRPCPlugin Type = "io.containerd.grpc.v1"
38-
SnapshotPlugin Type = "io.containerd.snapshotter.v1"
39-
TaskMonitorPlugin Type = "io.containerd.monitor.v1"
40-
DiffPlugin Type = "io.containerd.differ.v1"
41-
MetadataPlugin Type = "io.containerd.metadata.v1"
42-
ContentPlugin Type = "io.containerd.content.v1"
43-
GCPlugin Type = "io.containerd.gc.v1"
44-
LeasePlugin Type = "io.containerd.lease.v1"
45-
TracingProcessorPlugin Type = "io.containerd.tracing.processor.v1"
46-
)
29+
// Plugin types commonly used by containerd
30+
const (
31+
InternalPlugin Type = "io.containerd.internal.v1"
32+
RuntimePlugin Type = "io.containerd.runtime.v1"
33+
RuntimePluginV2 Type = "io.containerd.runtime.v2"
34+
ServicePlugin Type = "io.containerd.service.v1"
35+
GRPCPlugin Type = "io.containerd.grpc.v1"
36+
SnapshotPlugin Type = "io.containerd.snapshotter.v1"
37+
TaskMonitorPlugin Type = "io.containerd.monitor.v1"
38+
DiffPlugin Type = "io.containerd.differ.v1"
39+
MetadataPlugin Type = "io.containerd.metadata.v1"
40+
ContentPlugin Type = "io.containerd.content.v1"
41+
GCPlugin Type = "io.containerd.gc.v1"
42+
LeasePlugin Type = "io.containerd.lease.v1"
43+
TracingProcessorPlugin Type = "io.containerd.tracing.processor.v1"
44+
)
45+
46+
func testRegistry() Registry {
4747

4848
var register Registry
49-
register = register.Register(&Registration{
49+
return register.Register(&Registration{
5050
Type: TaskMonitorPlugin,
5151
ID: "cgroups",
5252
}).Register(&Registration{
@@ -223,7 +223,11 @@ func TestContainerdPlugin(t *testing.T) {
223223
TracingProcessorPlugin,
224224
},
225225
})
226+
}
226227

228+
// TestContainerdPlugin tests the logic of Graph, use the containerd's plugin
229+
func TestContainerdPlugin(t *testing.T) {
230+
register := testRegistry()
227231
ordered := register.Graph(mockPluginFilter)
228232
expectedURI := []string{
229233
"io.containerd.monitor.v1.cgroups",
@@ -512,3 +516,22 @@ func testPlugin(t Type, id string, i interface{}, err error) *Plugin {
512516
err: err,
513517
}
514518
}
519+
520+
func BenchmarkGraph(b *testing.B) {
521+
register := testRegistry()
522+
b.ResetTimer()
523+
for i := 0; i < b.N; i++ {
524+
register.Graph(mockPluginFilter)
525+
}
526+
}
527+
528+
func BenchmarkUnique(b *testing.B) {
529+
register := testRegistry()
530+
b.ResetTimer()
531+
for i := 0; i < b.N; i++ {
532+
checkUnique(register, &Registration{
533+
Type: InternalPlugin,
534+
ID: "new",
535+
})
536+
}
537+
}

0 commit comments

Comments
 (0)