Last active
August 22, 2019 01:37
-
-
Save micahhausler/fd2fefbcd9483a91a3c7ebc4dc4e59a0 to your computer and use it in GitHub Desktop.
CloudConfig loop vs global map benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws/endpoints" | |
"gopkg.in/gcfg.v1" | |
) | |
type CloudConfig struct { | |
Global struct { | |
VPC string | |
// ... other fields | |
} | |
ServiceOverride map[string]*struct { | |
Region string | |
URL string | |
SigningRegion string | |
Service string | |
} | |
} | |
func (c *CloudConfig) getResolver() endpoints.ResolverFunc { | |
return func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) { | |
for _, override := range c.ServiceOverride { | |
if service == override.Service && region == override.Region { | |
return endpoints.ResolvedEndpoint{ | |
URL: override.URL, | |
SigningRegion: override.SigningRegion, | |
}, nil | |
} | |
} | |
return endpoints.DefaultResolver().EndpointFor(service, region, optFns...) | |
} | |
} | |
// example global map | |
var overrides map[string]endpoints.ResolvedEndpoint = map[string]endpoints.ResolvedEndpoint{} | |
// example of populating map | |
func populateMap(c *CloudConfig) { | |
for _, override := range c.ServiceOverride { | |
signature := fmt.Sprintf("%s_%s", override.Service, override.Region) | |
overrides[signature] = endpoints.ResolvedEndpoint{URL: override.URL, SigningRegion: override.SigningRegion} | |
} | |
} | |
func main() { | |
cfgStr := ` | |
[Global] | |
vpc = vpc-abc1234567 | |
[serviceoverride "ec2_us-west-2"] | |
URL = https://ec2.foo.bar | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = ec2 | |
[serviceoverride "ecr_us-west-2"] | |
URL = https://ecr.foo.bar | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = ecr | |
[serviceoverride "ecr_us-east-1"] | |
URL = https://ecr.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-east-1 | |
Service = ecr | |
` | |
// change the CloudConfig type to have the "ServiceOverride" map | |
cfg := &CloudConfig{} | |
err := gcfg.ReadStringInto(cfg, cfgStr) | |
if err != nil { | |
panic(fmt.Errorf("Failed to parse gcfg data: %s", err)) | |
} | |
cases := []struct { | |
service string | |
region string | |
}{ | |
{"ec2", "us-east-1"}, // not in config above | |
{"ec2", "us-west-2"}, | |
{"ecr", "us-west-2"}, | |
{"ecr", "us-east-1"}, | |
} | |
resolver := cfg.getResolver() | |
for _, c := range cases { | |
ep, err := resolver.EndpointFor(c.service, c.region) | |
fmt.Printf("EndpointFor(%s, %s) got %+v, %+v\n", c.service, c.region, ep, err) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"testing" | |
"gopkg.in/gcfg.v1" | |
) | |
// Use all AWS services the CloudProvider uses as of 1.13 | |
// plus ECR overrides for every region | |
var cfgStr string = `[Global] | |
vpc = vpc-abc1234567 | |
[serviceoverride "ec2_us-west-2"] | |
URL = https://ec2.foo.bar | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = ec2 | |
[serviceoverride "asg_us-west-2"] | |
URL = https://asg.foo.bar | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = autoscaling | |
[serviceoverride "elb_us-west-2"] | |
URL = https://elb.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = elb | |
[serviceoverride "elbv2_us-west-2"] | |
URL = https://elbv2.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = elbv2 | |
[serviceoverride "kms_us-west-2"] | |
URL = https://kms.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = kms | |
[serviceoverride "metadata_us-west-2"] | |
URL = http://169.254.169.254 | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = metadata | |
[serviceoverride "ecr_ap-south-1"] | |
URL = https://ecr.ap-south-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = ap-south-1 | |
Service = ecr | |
[serviceoverride "ecr_eu-west-3"] | |
URL = https://ecr.eu-west-3.bar.foo | |
SigningRegion = custom-signing-region | |
Region = eu-west-3 | |
Service = ecr | |
[serviceoverride "ecr_eu-north-1"] | |
URL = https://ecr.eu-north-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = eu-north-1 | |
Service = ecr | |
[serviceoverride "ecr_eu-west-2"] | |
URL = https://ecr.eu-west-2.bar.foo | |
SigningRegion = custom-signing-region | |
Region = eu-west-2 | |
Service = ecr | |
[serviceoverride "ecr_eu-west-1"] | |
URL = https://ecr.eu-west-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = eu-west-1 | |
Service = ecr | |
[serviceoverride "ecr_ap-northeast-3"] | |
URL = https://ecr.ap-northeast-3.bar.foo | |
SigningRegion = custom-signing-region | |
Region = ap-northeast-3 | |
Service = ecr | |
[serviceoverride "ecr_ap-northeast-2"] | |
URL = https://ecr.ap-northeast-2.bar.foo | |
SigningRegion = custom-signing-region | |
Region = ap-northeast-2 | |
Service = ecr | |
[serviceoverride "ecr_ap-northeast-1"] | |
URL = https://ecr.ap-northeast-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = ap-northeast-1 | |
Service = ecr | |
[serviceoverride "ecr_sa-east-1"] | |
URL = https://ecr.sa-east-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = sa-east-1 | |
Service = ecr | |
[serviceoverride "ecr_ca-central-1"] | |
URL = https://ecr.ca-central-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = ca-central-1 | |
Service = ecr | |
[serviceoverride "ecr_ap-southeast-1"] | |
URL = https://ecr.ap-southeast-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = ap-southeast-1 | |
Service = ecr | |
[serviceoverride "ecr_ap-southeast-2"] | |
URL = https://ecr.ap-southeast-2.bar.foo | |
SigningRegion = custom-signing-region | |
Region = ap-southeast-2 | |
Service = ecr | |
[serviceoverride "ecr_eu-central-1"] | |
URL = https://ecr.eu-central-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = eu-central-1 | |
Service = ecr | |
[serviceoverride "ecr_us-east-1"] | |
URL = https://ecr.us-east-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-east-1 | |
Service = ecr | |
[serviceoverride "ecr_us-east-2"] | |
URL = https://ecr.us-east-2.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-east-2 | |
Service = ecr | |
[serviceoverride "ecr_us-west-1"] | |
URL = https://ecr.us-west-1.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-west-1 | |
Service = ecr | |
[serviceoverride "ecr_us-west-2"] | |
URL = https://ecr.us-west-2.bar.foo | |
SigningRegion = custom-signing-region | |
Region = us-west-2 | |
Service = ecr | |
` | |
func BenchmarkGetResolverMethod(b *testing.B) { | |
cfg := &CloudConfig{} | |
err := gcfg.ReadStringInto(cfg, cfgStr) | |
if err != nil { | |
b.Errorf("Error reading strings: %v", err) | |
} | |
resolver := cfg.getResolver() | |
cases := []struct { | |
region string | |
service string | |
}{ | |
{"us-west-2", "metadata"}, | |
{"us-west-2", "kms"}, | |
{"us-west-2", "elbv2"}, | |
{"us-west-2", "elb"}, | |
{"us-west-2", "autoscaling"}, | |
{"us-west-2", "ec2"}, | |
{"ap-south-1", "ecr"}, | |
{"eu-west-3", "ecr"}, | |
{"eu-north-1", "ecr"}, | |
{"eu-west-2", "ecr"}, | |
{"eu-west-1", "ecr"}, | |
{"ap-northeast-3", "ecr"}, | |
{"ap-northeast-2", "ecr"}, | |
{"ap-northeast-1", "ecr"}, | |
{"sa-east-1", "ecr"}, | |
{"ca-central-1", "ecr"}, | |
{"ap-southeast-1", "ecr"}, | |
{"ap-southeast-2", "ecr"}, | |
{"eu-central-1", "ecr"}, | |
{"us-east-1", "ecr"}, | |
{"us-east-2", "ecr"}, | |
{"us-west-1", "ecr"}, | |
{"us-west-2", "ecr"}, | |
} | |
// reset the timer to only test the lookup | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
for _, c := range cases { | |
resolver(c.service, c.region) | |
} | |
} | |
} | |
func BenchmarkGlobalMap(b *testing.B) { | |
cfg := &CloudConfig{} | |
err := gcfg.ReadStringInto(cfg, cfgStr) | |
if err != nil { | |
b.Errorf("Error reading strings: %v", err) | |
} | |
populateMap(cfg) | |
cases := []struct { | |
region string | |
service string | |
}{ | |
{"us-west-2", "metadata"}, | |
{"us-west-2", "kms"}, | |
{"us-west-2", "elbv2"}, | |
{"us-west-2", "elb"}, | |
{"us-west-2", "autoscaling"}, | |
{"us-west-2", "ec2"}, | |
{"ap-south-1", "ecr"}, | |
{"eu-west-3", "ecr"}, | |
{"eu-north-1", "ecr"}, | |
{"eu-west-2", "ecr"}, | |
{"eu-west-1", "ecr"}, | |
{"ap-northeast-3", "ecr"}, | |
{"ap-northeast-2", "ecr"}, | |
{"ap-northeast-1", "ecr"}, | |
{"sa-east-1", "ecr"}, | |
{"ca-central-1", "ecr"}, | |
{"ap-southeast-1", "ecr"}, | |
{"ap-southeast-2", "ecr"}, | |
{"eu-central-1", "ecr"}, | |
{"us-east-1", "ecr"}, | |
{"us-east-2", "ecr"}, | |
{"us-west-1", "ecr"}, | |
{"us-west-2", "ecr"}, | |
} | |
// reset the timer to only test the lookup | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
for _, c := range cases { | |
signature := fmt.Sprintf("%s_%s", strings.TrimSpace(c.service), strings.TrimSpace(c.region)) | |
_, ok := overrides[signature] | |
if !ok { | |
b.Errorf("could not find %s in map", signature) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go test -bench=. -count 4 -benchtime 3s -benchmem | |
goos: darwin | |
goarch: amd64 | |
pkg: github.com/micahhausler/configbench | |
BenchmarkGetResolverMethod-4 1000000 5720 ns/op 0 B/op 0 allocs/op | |
BenchmarkGetResolverMethod-4 1000000 5388 ns/op 0 B/op 0 allocs/op | |
BenchmarkGetResolverMethod-4 1000000 5570 ns/op 0 B/op 0 allocs/op | |
BenchmarkGetResolverMethod-4 1000000 5709 ns/op 0 B/op 0 allocs/op | |
BenchmarkGlobalMap-4 1000000 5959 ns/op 1216 B/op 69 allocs/op | |
BenchmarkGlobalMap-4 1000000 5817 ns/op 1216 B/op 69 allocs/op | |
BenchmarkGlobalMap-4 1000000 5950 ns/op 1216 B/op 69 allocs/op | |
BenchmarkGlobalMap-4 1000000 6192 ns/op 1216 B/op 69 allocs/op | |
PASS | |
ok github.com/micahhausler/configbench 47.849s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment