From e9180e4ebde0898abbcb658365740beeb53bce7d Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 12 Aug 2024 15:14:58 -0400 Subject: [PATCH] benchmark for plan eval --- internal/terraform/plan_benchmark_test.go | 74 +++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 internal/terraform/plan_benchmark_test.go diff --git a/internal/terraform/plan_benchmark_test.go b/internal/terraform/plan_benchmark_test.go new file mode 100644 index 0000000000..b3be27cc40 --- /dev/null +++ b/internal/terraform/plan_benchmark_test.go @@ -0,0 +1,74 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package terraform + +import ( + "testing" + + "github.com/hashicorp/terraform/internal/addrs" + "github.com/hashicorp/terraform/internal/configs/configschema" + "github.com/hashicorp/terraform/internal/providers" + testing_provider "github.com/hashicorp/terraform/internal/providers/testing" + "github.com/hashicorp/terraform/internal/states" + "github.com/zclconf/go-cty/cty" +) + +// Benchmark that stresses resource instance evaluation during plan +func BenchmarkPlanLargeCountRefs(b *testing.B) { + m := testModuleInline(b, map[string]string{ + "main.tf": ` +resource "test_resource" "a" { + count = 512 + input = "ok" +} + +resource "test_resource" "b" { + count = length(test_resource.a) + input = test_resource.a +} + +module "mod" { + count = length(test_resource.a) + source = "./mod" + in = [test_resource.a[count.index].id, test_resource.b[count.index].id] +} + +output out { + value = module.mod +}`, + "./mod/main.tf": ` +variable "in" { +} + +output "out" { + value = var.in +} +`}) + + p := new(testing_provider.MockProvider) + p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_resource": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Computed: true}, + "input": {Type: cty.DynamicPseudoType, Optional: true}, + }, + }, + }, + }) + + ctx := testContext2(b, &ContextOpts{ + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + + b.ResetTimer() + for range b.N { + _, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts) + if diags.HasErrors() { + b.Fatal(diags.Err()) + } + } +}