-
Notifications
You must be signed in to change notification settings - Fork 2.2k
cli/command/formatter: sort published ports numerically by IP #7144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "encoding/json" | ||
| "fmt" | ||
| "net/netip" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -946,10 +947,72 @@ func TestDisplayablePorts(t *testing.T) { | |
| }, | ||
| expected: "80/tcp, 80/udp, 1024/tcp, 1024/udp, 12345/sctp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp", //nolint:revive // ignore line-length-limit (revive) | ||
| }, | ||
| { | ||
| // host IPs are ordered numerically, not lexicographically: | ||
| // "10.0.0.2" sorts as a string before "9.0.0.1". | ||
| ports: []container.PortSummary{ | ||
| { | ||
| IP: netip.MustParseAddr("10.0.0.2"), | ||
| PublicPort: 8080, | ||
| PrivatePort: 80, | ||
| Type: "tcp", | ||
| }, { | ||
| IP: netip.MustParseAddr("9.0.0.1"), | ||
| PublicPort: 8081, | ||
| PrivatePort: 80, | ||
| Type: "tcp", | ||
| }, | ||
| }, | ||
| expected: "9.0.0.1:8081->80/tcp, 10.0.0.2:8080->80/tcp", | ||
| }, | ||
| } | ||
|
|
||
| for _, port := range cases { | ||
| actual := DisplayablePorts(port.ports) | ||
| assert.Check(t, is.Equal(port.expected, actual)) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkDisplayablePorts(b *testing.B) { | ||
| // Every port shares a container port, so | ||
| // each comparison falls through to the | ||
| // host-IP comparison. | ||
| // Descending order keeps the input unsorted. | ||
| const ( | ||
| sharedContainerPort = 80 | ||
| firstHostPort = 30000 | ||
| ) | ||
|
Comment on lines
+976
to
+984
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit on the fence on the benchmark; it's nice, but maybe a bit too much for this code (and mostly we're now benchmarking stdlib). @vvoland WDYT?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I don't think it's worth benchmarking |
||
|
|
||
| // Distinct host IP per port: 10.0.0.1, 10.0.0.2, ... | ||
| hostIP := func(n int) netip.Addr { | ||
| return netip.AddrFrom4([4]byte{10, 0, byte(n >> 8), byte(n)}) | ||
| } | ||
|
|
||
| for _, numPorts := range []int{64, 256} { | ||
| b.Run(strconv.Itoa(numPorts), func(b *testing.B) { | ||
| ports := make([]container.PortSummary, numPorts) | ||
|
|
||
| for i := range ports { | ||
| n := numPorts - i | ||
| ports[i] = container.PortSummary{ | ||
| IP: hostIP(n), | ||
| PublicPort: uint16(firstHostPort + n), | ||
| PrivatePort: sharedContainerPort, | ||
| Type: "tcp", | ||
| } | ||
| } | ||
|
|
||
| scratch := make([]container.PortSummary, numPorts) | ||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit; we can probably use |
||
| // DisplayablePorts sorts in place; | ||
| // restore the unsorted input. | ||
| copy(scratch, ports) | ||
|
|
||
| _ = DisplayablePorts(scratch) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered why this was there, but looks like previously IP was just a basic string, so when it changed, this was probably used as closest match (f81816e);