Skip to content

Conversation

@mariomac
Copy link

@mariomac mariomac commented Sep 9, 2022

The previous version of the netdb.go file required to iterate across of tenths of thousands (worst case) of services and protocols on each query.

This version preloads all the services and protocols information as maps for a O(1) lookup on each flow information.

Benchmarks comparing new vs old version:

BenchmarkGetProtoByNumber-12            162093156               74.11 ns/op
BenchmarkGetProtoByName-12              93009231               126.4 ns/op

BenchmarkGetProtoByNumber_Old-12          945291             12846 ns/op
BenchmarkGetProtoByName_Old-12          17034988               706.8 ns/op

To reproduce all the benchmarks, you should checkout commit 5ac2502081c1a47101f758030b988466cbf554ec, as the old code is removed in the head commit.

@mariomac mariomac changed the title Optimize netdb Services databas Optimize netdb Services database Sep 9, 2022
@mariomac mariomac added the ok-to-test To set manually when a PR is safe to test. Triggers image build on PR. label Sep 9, 2022
@github-actions
Copy link

github-actions bot commented Sep 9, 2022

New image: ["quay.io/netobserv/flowlogs-pipeline:9621604"]. It will expire after two weeks.

Copy link
Contributor

@ronensc ronensc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great optimization! :)

Comment on lines 1 to 2
# /etc/protocols:
# $Id: protocols,v 1.12 2016/07/08 12:27 ovasik Exp $
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in go help test, I think testdata is more idiomatic name for the fixtures directory.

The go tool will ignore a directory named "testdata", making it available
to hold ancillary data needed by the tests.

https://pkg.go.dev/cmd/go/internal/test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Didn't know it. Thanks!

// verify it also finds service name by protocol alias
assert.Equal(t, "netbios-dgm", db.ByPortAndProtocolName(138, "TCP"))

// verify multiple ports can be associated to the same protocol
Copy link
Contributor

@ronensc ronensc Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the comment

// verify multiple ports can be associated to the same protocol

The cases below all have the same port but different protocols. In contrast to the comment that states multiple ports and the same protocol.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I guess that's a typo in comment, @mariomac ? Should be multiple protocols to same port ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah you are right. Fixing

Comment on lines 223 to 227
protos, err := os.Open("/etc/protocols")
if err != nil {
return nil, fmt.Errorf("opening /etc/protocols: %w", err)
}
services, err := os.Open("/etc/services")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these os.Open() be followed by defer file.Close() as done in the tests?

Comment on lines 223 to 227
protos, err := os.Open("/etc/protocols")
if err != nil {
return nil, fmt.Errorf("opening /etc/protocols: %w", err)
}
services, err := os.Open("/etc/services")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the old implementation, the paths to the protocols file and the services file were configurable (jsonNetworkTransform.ProtocolsFile and jsonNetworkTransform.ServicesFile). Don't we want to keep them configurable (rather than hardcoded)?

@github-actions github-actions bot removed the ok-to-test To set manually when a PR is safe to test. Triggers image build on PR. label Sep 19, 2022
@codecov-commenter
Copy link

codecov-commenter commented Sep 19, 2022

Codecov Report

Merging #304 (f09cb78) into main (554b9b5) will increase coverage by 0.05%.
The diff coverage is 68.93%.

@@            Coverage Diff             @@
##             main     #304      +/-   ##
==========================================
+ Coverage   68.67%   68.73%   +0.05%     
==========================================
  Files          81       81              
  Lines        4699     4679      -20     
==========================================
- Hits         3227     3216      -11     
+ Misses       1276     1269       -7     
+ Partials      196      194       -2     
Flag Coverage Δ
unittests 68.73% <68.93%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/pipeline/transform/transform_network.go 59.58% <55.55%> (-1.19%) ⬇️
pkg/pipeline/transform/netdb/netdb.go 73.68% <73.68%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mariomac mariomac merged commit 7401e74 into netobserv:main Sep 22, 2022
@mariomac mariomac deleted the optimize-sdb branch September 28, 2022 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants