rob05c commented on a change in pull request #5247:
URL: https://github.com/apache/trafficcontrol/pull/5247#discussion_r525506341
##########
File path: lib/go-atscfg/atscfg.go
##########
@@ -22,56 +22,90 @@ package atscfg
import (
"encoding/json"
"errors"
+ "fmt"
"net"
"sort"
"strconv"
"strings"
- "time"
- "github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-tc"
)
const InvalidID = -1
-
const DefaultATSVersion = "5" // TODO Emulates Perl; change to 6? ATC no
longer officially supports ATS 5.
-
const HeaderCommentDateFormat = "Mon Jan 2 15:04:05 MST 2006"
-
const ContentTypeTextASCII = `text/plain; charset=us-ascii`
-
const LineCommentHash = "#"
+const ConfigSuffix = ".config"
+
+type DeliveryServiceID int
+type ProfileID int
+type ServerID int
type TopologyName string
type CacheGroupType string
type ServerCapability string
-type ServerInfo struct {
- CacheGroupID int
- CacheGroupName string
- CDN tc.CDNName
- CDNID int
- DomainName string
- HostName string
- HTTPSPort int
- ID int
- IP string
- ParentCacheGroupID int
- ParentCacheGroupType string
- ProfileID ProfileID
- ProfileName string
- Port int
- SecondaryParentCacheGroupID int
- SecondaryParentCacheGroupType string
- Type string
-}
-
-func (s *ServerInfo) IsTopLevelCache() bool {
- return (s.ParentCacheGroupType == tc.CacheGroupOriginTypeName ||
s.ParentCacheGroupID == InvalidID) &&
- (s.SecondaryParentCacheGroupType == tc.CacheGroupOriginTypeName
|| s.SecondaryParentCacheGroupID == InvalidID)
-}
-
-func MakeCGMap(cgs []tc.CacheGroupNullable)
(map[tc.CacheGroupName]tc.CacheGroupNullable, error) {
+// Server is a tc.Server for the latest lib/go-tc and traffic_ops/vx-client
type.
+// This allows atscfg to not have to change the type everywhere it's used,
every time ATC changes the base type,
+// but to only have to change it here, and the places where breaking symbol
changes were made.
+type Server tc.ServerV30
+
+// DeliveryService is a tc.DeliveryService for the latest lib/go-tc and
traffic_ops/vx-client type.
+// This allows atscfg to not have to change the type everywhere it's used,
every time ATC changes the base type,
+// but to only have to change it here, and the places where breaking symbol
changes were made.
+type DeliveryService tc.DeliveryServiceNullableV30
+
+// ToDeliveryServices converts a slice of the latest lib/go-tc and
traffic_ops/vx-client type to the local alias.
+func ToDeliveryServices(dses []tc.DeliveryServiceNullableV30)
[]DeliveryService {
+ ad := []DeliveryService{}
+ for _, ds := range dses {
+ ad = append(ad, DeliveryService(ds))
+ }
+ return ad
+}
+
+// OldToDeliveryServices converts a slice of the old traffic_ops/client type
to the local alias.
+func OldToDeliveryServices(dses []tc.DeliveryServiceNullable)
[]DeliveryService {
+ ad := []DeliveryService{}
+ for _, ds := range dses {
+ upgradedDS :=
tc.DeliveryServiceNullableV30{DeliveryServiceNullableV15:
tc.DeliveryServiceNullableV15(ds)}
+ ad = append(ad, DeliveryService(upgradedDS))
+ }
+ return ad
+}
+
+// ToServers converts a slice of the latest lib/go-tc and
traffic_ops/vx-client type to the local alias.
+func ToServers(servers []tc.ServerV30) []Server {
+ as := []Server{}
+ for _, sv := range servers {
+ as = append(as, Server(sv))
+ }
+ return as
+}
Review comment:
```
func ToServers(servers []tc.ServerV30) []Server {
as := []Server{}
for _, sv := range servers {
as = append(as, Server(sv))
}
return as
}
func ToServersMake(servers []tc.ServerV30) []Server {
as := make([]Server, 0, len(servers))
for _, sv := range servers {
as = append(as, Server(sv))
}
return as
}
func BenchmarkToServers1mil(b *testing.B) {
num := 1000000
for i := 0; i < b.N; i++ {
servers := make([]tc.ServerV30, num, num)
ToServers(servers)
}
}
func BenchmarkToServersMake1mil(b *testing.B) {
num := 1000000
for i := 0; i < b.N; i++ {
servers := make([]tc.ServerV30, num, num)
ToServersMake(servers)
}
}
func BenchmarkToServers2k(b *testing.B) {
num := 2000
for i := 0; i < b.N; i++ {
servers := make([]tc.ServerV30, num, num)
ToServers(servers)
}
}
func BenchmarkToServersMake2k(b *testing.B) {
num := 2000
for i := 0; i < b.N; i++ {
servers := make([]tc.ServerV30, num, num)
ToServersMake(servers)
}
}
```
```
$ go test -bench=.
DEBUG moc 42 usesmid true assignedmids 3
DEBUG moc 0 usesmid true assignedmids 0
goos: darwin
goarch: amd64
pkg: github.com/apache/trafficcontrol/lib/go-atscfg
BenchmarkToServers1mil-16 1 1577990978 ns/op
BenchmarkToServersMake1mil-16 3 387240011 ns/op
BenchmarkToServers2k-16 1321 821199 ns/op
BenchmarkToServersMake2k-16 2414 417031 ns/op
PASS
ok github.com/apache/trafficcontrol/lib/go-atscfg 8.505s
```
So on my laptop, it takes 0.0016 milliseconds without `make` vs 0.00039
milliseconds with `make` per server, for a million servers,
and 0.00041 milliseconds without vs 0.00021 milliseconds per server, for
2,000 servers.
I think we can spare the performance to improve the readability.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]