From 82faa4e700cf2718a61758e0402d6105c91930f3 Mon Sep 17 00:00:00 2001 From: YAEGASHI Takeshi Date: Tue, 18 Feb 2020 14:38:04 +0900 Subject: [PATCH] Add tests/benchmark --- tests/benchmark/azureadtf/azuread.tf | 40 ++++++++ tests/benchmark/msgraphgo/.gitignore | 1 + tests/benchmark/msgraphgo/main.go | 131 +++++++++++++++++++++++++++ tests/benchmark/msgraphtf/msgraph.tf | 36 ++++++++ 4 files changed, 208 insertions(+) create mode 100644 tests/benchmark/azureadtf/azuread.tf create mode 100644 tests/benchmark/msgraphgo/.gitignore create mode 100644 tests/benchmark/msgraphgo/main.go create mode 100644 tests/benchmark/msgraphtf/msgraph.tf diff --git a/tests/benchmark/azureadtf/azuread.tf b/tests/benchmark/azureadtf/azuread.tf new file mode 100644 index 0000000..511f7e1 --- /dev/null +++ b/tests/benchmark/azureadtf/azuread.tf @@ -0,0 +1,40 @@ +provider "azuread" { + // Define environment variables + // ARM_TENANT_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET, ARM_SUBSCRIPTION_ID (dummy) +} + +variable "config" { + type = object({ domain = string, name = string, user_count = number, group_count = number }) + default = { domain = "l0wdev.onmicrosoft.com", name = "azureadtf", user_count = 100, group_count = 10 } +} + +provider random {} + +resource "random_password" "password" { + length = 16 +} + +resource "azuread_user" "demo_users" { + count = var.config.user_count + user_principal_name = "${var.config.name}user${count.index}@${var.config.domain}" + display_name = "${var.config.name} user ${count.index}" + mail_nickname = "${var.config.name}user${count.index}" + password = random_password.password.result +} + +resource "azuread_group" "demo_groups" { + count = var.config.group_count + name = "${var.config.name} group ${count.index}" +} + +resource "azuread_group_member" "demo_group_user_members" { + count = var.config.user_count + group_object_id = azuread_group.demo_groups[count.index % var.config.group_count].id + member_object_id = azuread_user.demo_users[count.index].id +} + +resource "azuread_group_member" "demo_group_group_members" { + count = var.config.group_count - 1 + group_object_id = azuread_group.demo_groups[count.index].id + member_object_id = azuread_group.demo_groups[count.index + 1].id +} diff --git a/tests/benchmark/msgraphgo/.gitignore b/tests/benchmark/msgraphgo/.gitignore new file mode 100644 index 0000000..58fc1d2 --- /dev/null +++ b/tests/benchmark/msgraphgo/.gitignore @@ -0,0 +1 @@ +msgraphgo diff --git a/tests/benchmark/msgraphgo/main.go b/tests/benchmark/msgraphgo/main.go new file mode 100644 index 0000000..679e7d3 --- /dev/null +++ b/tests/benchmark/msgraphgo/main.go @@ -0,0 +1,131 @@ +package main + +import ( + "context" + "flag" + "fmt" + "log" + "os" + + "github.com/google/uuid" + "github.com/yaegashi/msgraph.go/jsonx" + "github.com/yaegashi/msgraph.go/msauth" + P "github.com/yaegashi/msgraph.go/ptr" + msgraph "github.com/yaegashi/msgraph.go/v1.0" + "golang.org/x/oauth2" +) + +func dump(o interface{}) { + enc := jsonx.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + enc.Encode(o) +} + +func main() { + var tenantDomain, tenantID, clientID, clientSecret string + var userCount, groupCount int + var clean bool + flag.StringVar(&tenantDomain, "tenant-domain", "l0wdev.onmicrosoft.com", "Tenant Domain") + flag.StringVar(&tenantID, "tenant-id", os.Getenv("ARM_TENANT_ID"), "Tenant ID") + flag.StringVar(&clientID, "client-id", os.Getenv("ARM_CLIENT_ID"), "Client ID") + flag.StringVar(&clientSecret, "client-secret", os.Getenv("ARM_CLIENT_SECRET"), "Client Secret") + flag.IntVar(&userCount, "user-count", 100, "User count") + flag.IntVar(&groupCount, "group-count", 10, "Group count") + flag.BoolVar(&clean, "clean", false, "Clean") + flag.Parse() + + ctx := context.Background() + m := msauth.NewManager() + scopes := []string{msauth.DefaultMSGraphScope} + ts, err := m.ClientCredentialsGrant(ctx, tenantID, clientID, clientSecret, scopes) + if err != nil { + log.Fatal(err) + } + + httpClient := oauth2.NewClient(ctx, ts) + graphClient := msgraph.NewClient(httpClient) + + if clean { + for i := 0; i < userCount; i++ { + r := graphClient.Users().Request() + r.Filter(fmt.Sprintf("mailNickname eq 'msgraphgouser%d'", i)) + users, _ := r.Get(ctx) + for _, user := range users { + log.Printf("Delete user %d", i) + graphClient.Users().ID(*user.ID).Request().Delete(ctx) + } + } + for i := 0; i < groupCount; i++ { + r := graphClient.Groups().Request() + r.Filter(fmt.Sprintf("mailNickname eq 'msgraphgogroup%d'", i)) + groups, _ := r.Get(ctx) + for _, group := range groups { + log.Printf("Delete group %d", i) + graphClient.Groups().ID(*group.ID).Request().Delete(ctx) + } + } + return + } + + users := make([]*msgraph.User, userCount) + for i := 0; i < userCount; i++ { + user := &msgraph.User{ + AccountEnabled: P.Bool(true), + UserPrincipalName: P.String(fmt.Sprintf("msgraphgouser%d@%s", i, tenantDomain)), + MailNickname: P.String(fmt.Sprintf("msgraphgouser%d", i)), + DisplayName: P.String(fmt.Sprintf("msgraphgo user %d", i)), + PasswordProfile: &msgraph.PasswordProfile{ + ForceChangePasswordNextSignIn: P.Bool(false), + Password: P.String(uuid.New().String()), + }, + } + log.Printf("Create user %d", i) + users[i], err = graphClient.Users().Request().Add(ctx, user) + if err != nil { + log.Fatal(err) + } + } + + groups := make([]*msgraph.Group, groupCount) + for i := 0; i < groupCount; i++ { + group := &msgraph.Group{ + SecurityEnabled: P.Bool(true), + MailEnabled: P.Bool(false), + MailNickname: P.String(fmt.Sprintf("msgraphgogroup%d", i)), + DisplayName: P.String(fmt.Sprintf("msgraphgo group %d", i)), + } + log.Printf("Create group %d", i) + groups[i], err = graphClient.Groups().Request().Add(ctx, group) + if err != nil { + log.Fatal(err) + } + } + + for i := 0; i < userCount; i++ { + member := users[i] + group := groups[i%groupCount] + log.Printf("Add user %d to group %d", i, i%groupCount) + reqObj := map[string]interface{}{ + "@odata.id": graphClient.DirectoryObjects().ID(*member.ID).Request().URL(), + } + r := graphClient.Groups().ID(*group.ID).Members().Request() + err := r.JSONRequest(ctx, "POST", "/$ref", reqObj, nil) + if err != nil { + log.Fatal(err) + } + } + + for i := 0; i < groupCount-1; i++ { + member := groups[i+1] + group := groups[i] + log.Printf("Add group %d to group %d", i+1, i) + reqObj := map[string]interface{}{ + "@odata.id": graphClient.DirectoryObjects().ID(*member.ID).Request().URL(), + } + r := graphClient.Groups().ID(*group.ID).Members().Request() + err := r.JSONRequest(ctx, "POST", "/$ref", reqObj, nil) + if err != nil { + log.Fatal(err) + } + } +} diff --git a/tests/benchmark/msgraphtf/msgraph.tf b/tests/benchmark/msgraphtf/msgraph.tf new file mode 100644 index 0000000..310caf4 --- /dev/null +++ b/tests/benchmark/msgraphtf/msgraph.tf @@ -0,0 +1,36 @@ + +provider "msgraph" { + // Define environment variables: + // ARM_TENANT_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET +} + +variable "config" { + type = object({ domain = string, name = string, user_count = number, group_count = number }) + default = { domain = "l0wdev.onmicrosoft.com", name = "msgraphtf", user_count = 100, group_count = 10 } +} + +resource "msgraph_user" "demo_users" { + count = var.config.user_count + user_principal_name = "${var.config.name}user${count.index}@${var.config.domain}" + display_name = "${var.config.name} user ${count.index}" + mail_nickname = "${var.config.name}user${count.index}" + account_enabled = true +} + +resource "msgraph_group" "demo_groups" { + count = var.config.group_count + display_name = "${var.config.name} group ${count.index}" + mail_nickname = "${var.config.name}group${count.index}" +} + +resource "msgraph_group_member" "demo_group_user_members" { + count = var.config.user_count + group_id = msgraph_group.demo_groups[count.index % var.config.group_count].id + member_id = msgraph_user.demo_users[count.index].id +} + +resource "msgraph_group_member" "demo_group_group_members" { + count = var.config.group_count - 1 + group_id = msgraph_group.demo_groups[count.index].id + member_id = msgraph_group.demo_groups[count.index + 1].id +}