Last active
August 17, 2018 13:46
-
-
Save TheoBrigitte/212fc1ff17b9f954833e37f4d078125e to your computer and use it in GitHub Desktop.
Program used to attest memory leak in Azure sdk when not using "*Responder" functions after issuing API calls
This file contains 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 ( | |
"context" | |
"log" | |
"os" | |
"runtime" | |
"runtime/pprof" | |
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network" | |
"github.com/Azure/go-autorest/autorest" | |
"github.com/Azure/go-autorest/autorest/adal" | |
"github.com/Azure/go-autorest/autorest/azure" | |
"github.com/giantswarm/azure-operator/client" | |
) | |
const ( | |
azureClientID = "<your azure client id>" | |
azureClientSecret = "<your azure secret key>" | |
azureSubscriptionID = "<your azure subscription id>" | |
azureTenantID = "<your azure tenant id>" | |
resourceGroup = "<your azure resource group name>" | |
virtualNetwork = "<your azure virtual network name>" | |
memoryProf0 = "mem0.prof" | |
memoryProf1 = "mem1.prof" | |
) | |
func listPeeringNoResponder(client network.VirtualNetworkPeeringsClient) error { | |
ctx := context.Background() | |
req, err := client.ListPreparer(ctx, resourceGroup, virtualNetwork) | |
if err != nil { | |
return err | |
} | |
_, err = client.ListSender(req) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func listPeeringWithResponder(client network.VirtualNetworkPeeringsClient) error { | |
ctx := context.Background() | |
req, err := client.ListPreparer(ctx, resourceGroup, virtualNetwork) | |
if err != nil { | |
return err | |
} | |
resp, err := client.ListSender(req) | |
if err != nil { | |
return err | |
} | |
_, err = client.ListResponder(resp) | |
if err != nil { | |
panic(err) | |
} | |
return nil | |
} | |
func writeMemoryHeap(fileName string) error { | |
f, err := os.Create(fileName) | |
defer f.Close() | |
if err != nil { | |
return err | |
} | |
runtime.GC() | |
if err := pprof.WriteHeapProfile(f); err != nil { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
azureConfig := client.AzureClientSetConfig{ | |
ClientID: azureClientID, | |
ClientSecret: azureClientSecret, | |
SubscriptionID: azureSubscriptionID, | |
TenantID: azureTenantID, | |
} | |
log.Println("start") | |
env := azure.PublicCloud | |
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, azureConfig.TenantID) | |
if err != nil { | |
panic(err) | |
} | |
token, err := adal.NewServicePrincipalToken(*oauthConfig, azureConfig.ClientID, azureConfig.ClientSecret, env.ServiceManagementEndpoint) | |
if err != nil { | |
panic(err) | |
} | |
client := network.NewVirtualNetworkPeeringsClient(azureConfig.SubscriptionID) | |
client.Authorizer = autorest.NewBearerAuthorizer(token) | |
err = writeMemoryHeap(memoryProf0) | |
if err != nil { | |
panic(err) | |
} | |
for i := 0; i < 1000; i++ { | |
// Run this programm once with listPeeringNoResponder and once with listPeeringWithResponder. | |
// err = listPeeringNoResponder(client) | |
err = listPeeringWithResponder(client) | |
if err != nil { | |
panic(err) | |
} | |
} | |
err = writeMemoryHeap(memoryProf1) | |
if err != nil { | |
panic(err) | |
} | |
log.Println("end") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above program produce not output beside
start
andend
but it produces two filesmem0.prof
andmem1.prof
Use this command to visualize memory consumption
go tool pprof -top -base mem0.prof mem1.prof