Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package bp2build |
| 16 | |
| 17 | // to run the benchmarks in this file, you must run go test with the -bench. |
| 18 | // The benchmarked portion will run for the specified time (can be set via -benchtime) |
| 19 | // This can mean if you are benchmarking a faster portion of a larger operation, it will take |
| 20 | // longer. |
| 21 | // If you are seeing a small number of iterations for a specific run, the data is less reliable, to |
| 22 | // run for longer, set -benchtime to a larger value. |
| 23 | |
| 24 | import ( |
| 25 | "android/soong/android" |
| 26 | "fmt" |
| 27 | "math" |
| 28 | "strings" |
| 29 | "testing" |
| 30 | ) |
| 31 | |
| 32 | func genCustomModule(i int, convert bool) string { |
| 33 | var conversionString string |
| 34 | if convert { |
| 35 | conversionString = `bazel_module: { bp2build_available: true },` |
| 36 | } |
| 37 | return fmt.Sprintf(` |
| 38 | custom { |
| 39 | name: "arch_paths_%[1]d", |
| 40 | string_list_prop: ["\t", "\n"], |
| 41 | string_prop: "a\t\n\r", |
| 42 | arch_paths: ["outer", ":outer_dep_%[1]d"], |
| 43 | arch: { |
| 44 | x86: { |
| 45 | arch_paths: ["abc", ":x86_dep_%[1]d"], |
| 46 | }, |
| 47 | x86_64: { |
| 48 | arch_paths: ["64bit"], |
| 49 | arch_paths_exclude: ["outer"], |
| 50 | }, |
| 51 | }, |
| 52 | %[2]s |
| 53 | } |
| 54 | |
| 55 | custom { |
| 56 | name: "outer_dep_%[1]d", |
| 57 | %[2]s |
| 58 | } |
| 59 | |
| 60 | custom { |
| 61 | name: "x86_dep_%[1]d", |
| 62 | %[2]s |
| 63 | } |
| 64 | `, i, conversionString) |
| 65 | } |
| 66 | |
| 67 | func genCustomModuleBp(pctConverted float64) string { |
| 68 | modules := 100 |
| 69 | |
| 70 | bp := make([]string, 0, modules) |
| 71 | toConvert := int(math.Round(float64(modules) * pctConverted)) |
| 72 | |
| 73 | for i := 0; i < modules; i++ { |
| 74 | bp = append(bp, genCustomModule(i, i < toConvert)) |
| 75 | } |
| 76 | return strings.Join(bp, "\n\n") |
| 77 | } |
| 78 | |
| 79 | var pctToConvert = []float64{0.0, 0.01, 0.05, 0.10, 0.25, 0.5, 0.75, 1.0} |
| 80 | |
| 81 | func BenchmarkManyModulesFull(b *testing.B) { |
| 82 | dir := "." |
| 83 | for _, tcSize := range pctToConvert { |
| 84 | |
| 85 | b.Run(fmt.Sprintf("pctConverted %f", tcSize), func(b *testing.B) { |
| 86 | for n := 0; n < b.N; n++ { |
| 87 | b.StopTimer() |
| 88 | // setup we don't want to measure |
| 89 | config := android.TestConfig(buildDir, nil, genCustomModuleBp(tcSize), nil) |
| 90 | ctx := android.NewTestContext(config) |
| 91 | |
| 92 | registerCustomModuleForBp2buildConversion(ctx) |
| 93 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 94 | |
| 95 | b.StartTimer() |
| 96 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 97 | if len(errs) > 0 { |
| 98 | b.Fatalf("Unexpected errors: %s", errs) |
| 99 | } |
| 100 | |
| 101 | _, errs = ctx.ResolveDependencies(config) |
| 102 | if len(errs) > 0 { |
| 103 | b.Fatalf("Unexpected errors: %s", errs) |
| 104 | } |
| 105 | |
| 106 | generateBazelTargetsForDir(codegenCtx, dir) |
| 107 | b.StopTimer() |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func BenchmarkManyModulesResolveDependencies(b *testing.B) { |
| 114 | dir := "." |
| 115 | for _, tcSize := range pctToConvert { |
| 116 | |
| 117 | b.Run(fmt.Sprintf("pctConverted %f", tcSize), func(b *testing.B) { |
| 118 | for n := 0; n < b.N; n++ { |
| 119 | b.StopTimer() |
| 120 | // setup we don't want to measure |
| 121 | config := android.TestConfig(buildDir, nil, genCustomModuleBp(tcSize), nil) |
| 122 | ctx := android.NewTestContext(config) |
| 123 | |
| 124 | registerCustomModuleForBp2buildConversion(ctx) |
| 125 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 126 | |
| 127 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 128 | if len(errs) > 0 { |
| 129 | b.Fatalf("Unexpected errors: %s", errs) |
| 130 | } |
| 131 | |
| 132 | b.StartTimer() |
| 133 | _, errs = ctx.ResolveDependencies(config) |
| 134 | b.StopTimer() |
| 135 | if len(errs) > 0 { |
| 136 | b.Fatalf("Unexpected errors: %s", errs) |
| 137 | } |
| 138 | |
| 139 | generateBazelTargetsForDir(codegenCtx, dir) |
| 140 | } |
| 141 | }) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func BenchmarkManyModulesGenerateBazelTargetsForDir(b *testing.B) { |
| 146 | dir := "." |
| 147 | for _, tcSize := range pctToConvert { |
| 148 | |
| 149 | b.Run(fmt.Sprintf("pctConverted %f", tcSize), func(b *testing.B) { |
| 150 | for n := 0; n < b.N; n++ { |
| 151 | b.StopTimer() |
| 152 | // setup we don't want to measure |
| 153 | config := android.TestConfig(buildDir, nil, genCustomModuleBp(tcSize), nil) |
| 154 | ctx := android.NewTestContext(config) |
| 155 | |
| 156 | registerCustomModuleForBp2buildConversion(ctx) |
| 157 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 158 | |
| 159 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 160 | if len(errs) > 0 { |
| 161 | b.Fatalf("Unexpected errors: %s", errs) |
| 162 | } |
| 163 | |
| 164 | _, errs = ctx.ResolveDependencies(config) |
| 165 | if len(errs) > 0 { |
| 166 | b.Fatalf("Unexpected errors: %s", errs) |
| 167 | } |
| 168 | |
| 169 | b.StartTimer() |
| 170 | generateBazelTargetsForDir(codegenCtx, dir) |
| 171 | b.StopTimer() |
| 172 | } |
| 173 | }) |
| 174 | } |
| 175 | } |