Skip to content

Commit 77dae88

Browse files
authored
Update binary size tool to connect to the Metrics Service in postsubmits. (#8550)
Update binary size tool to connect to the Metrics Service. Update condition and set postsubmit flag
1 parent 31f1740 commit 77dae88

File tree

7 files changed

+226
-108
lines changed

7 files changed

+226
-108
lines changed

.github/workflows/health_metrics.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ env:
1111
METRICS_SERVICE_SECRET: ${{ secrets.GHASecretsGPGPassphrase1 }}
1212

1313
jobs:
14+
# Check all the modified SDKs, the flags will be true if changed files match patterns in the file
15+
# scripts/health_metrics/code_coverage_file_list.json
1416
check:
1517
if: github.repository == 'Firebase/firebase-ios-sdk' && (github.event.action == 'opened' || github.event.action == 'synchronize')
1618
name: Check changed files
@@ -44,13 +46,26 @@ jobs:
4446
4547
binary_size_metrics:
4648
needs: check
47-
if: always() && github.repository == 'Firebase/firebase-ios-sdk' && (github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.pull_request.merged)
49+
# Prevent the job from being triggered in fork.
50+
if: always() && github.repository == 'Firebase/firebase-ios-sdk' && github.event.pull_request.merged
4851
runs-on: macos-11
4952
strategy:
5053
matrix:
5154
target: [iOS]
5255
steps:
5356
- uses: actions/checkout@v2
57+
- name: Access to Metrics Service
58+
run: |
59+
# Install gcloud sdk
60+
curl https://sdk.cloud.google.com > install.sh
61+
bash install.sh --disable-prompts
62+
echo "${HOME}/google-cloud-sdk/bin/" >> $GITHUB_PATH
63+
export PATH="${HOME}/google-cloud-sdk/bin/:${PATH}"
64+
65+
# Activate the service account for Metrics Service.
66+
scripts/decrypt_gha_secret.sh scripts/gha-encrypted/metrics_service_access.json.gpg \
67+
metrics-access.json "${{ env.METRICS_SERVICE_SECRET }}"
68+
gcloud auth activate-service-account --key-file metrics-access.json
5469
- name: Build and test
5570
run: |
5671
FirebaseABTesting=${{ needs.check.outputs.abtesting_run_job }} \
@@ -65,6 +80,11 @@ jobs:
6580
FirebaseRemoteConfig=${{ needs.check.outputs.remoteconfig_run_job }} \
6681
FirebaseStorage=${{ needs.check.outputs.storage_run_job }} \
6782
./scripts/health_metrics/create_binary_size_report.sh
83+
env:
84+
PULL_REQUEST_NUM: ${{ github.event.pull_request.number }}
85+
BASE_COMMIT: ${{ github.event.pull_request.base.sha }}
86+
POSTSUBMIT: ${{ github.event.pull_request.merged }}
87+
SOURCE_BRANCH: ${{ github.base_ref }}
6888

6989
pod-lib-lint-abtesting:
7090
needs: check

scripts/health_metrics/create_binary_size_report.sh

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,59 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
# USAGE: git diff -U0 [base_commit] HEAD | get_diff_lines.sh
18-
#
19-
# This will generate a JSON output of changed files and their newly added
20-
# lines.
17+
# This is to generate pod size report and send to the Metrics Service.
18+
# The Metrics Service will either
19+
# 1. save binary size data into the databasae when POSTSUBMIT is true, or
20+
# 2. post a report in a PR when POSTSUBMIT is not true.
21+
22+
set -ex
2123

2224
BINARY_SIZE_SDK=()
23-
if $FirebaseABTesting == 'true'; then
25+
26+
# In presubmits, `check` job in the health_metrics.yml workflow will turn on SDK flags if a corresponding
27+
# file path, in `scripts/health_metrics/code_coverage_file_list.json` is updated.
28+
# In postsubmits, all SDKs should be measured, so binary size data of all SDKs should be uploaded to a
29+
# merged commit. Next time a new PR can compare the head of the PR to a commit on the base branch.
30+
if [[ "${POSTSUBMIT}" == true || "${FirebaseABTesting}" == 'true' ]]; then
2431
BINARY_SIZE_SDK+=('FirebaseABTesting')
2532
fi
26-
if $FirebaseAuth == 'true'; then
33+
if [[ "${POSTSUBMIT}" == true || "${FirebaseAuth}" == 'true' ]]; then
2734
BINARY_SIZE_SDK+=('FirebaseAuth')
2835
fi
29-
if $FirebaseDatabase == 'true'; then
36+
if [[ "${POSTSUBMIT}" == true || "${FirebaseDatabase}" == 'true' ]]; then
3037
BINARY_SIZE_SDK+=('FirebaseDatabase')
3138
fi
32-
if $FirebaseDynamicLinks == 'true'; then
39+
if [[ "${POSTSUBMIT}" == true || "${FirebaseDynamicLinks}" == 'true' ]]; then
3340
BINARY_SIZE_SDK+=('FirebaseDynamicLinks')
3441
fi
35-
if $FirebaseFirestore == 'true'; then
42+
if [[ "${POSTSUBMIT}" == true || "${FirebaseFirestore}" == 'true' ]]; then
3643
BINARY_SIZE_SDK+=('FirebaseFirestore')
3744
fi
38-
if $FirebaseFunctions == 'true'; then
45+
if [[ "${POSTSUBMIT}" == true || "${FirebaseFunctions}" == 'true' ]]; then
3946
BINARY_SIZE_SDK+=('FirebaseFunctions')
4047
fi
41-
if $FirebaseInAppMessaging == 'true'; then
48+
if [[ "${POSTSUBMIT}" == true || "${FirebaseInAppMessaging}" == 'true' ]]; then
4249
BINARY_SIZE_SDK+=('FirebaseInAppMessaging')
4350
fi
44-
if $FirebaseMessaging == 'true'; then
51+
if [[ "${POSTSUBMIT}" == true || "${FirebaseMessaging}" == 'true' ]]; then
4552
BINARY_SIZE_SDK+=('FirebaseMessaging')
4653
fi
47-
if $FirebasePerformance == 'true'; then
54+
if [[ "${POSTSUBMIT}" == true || "${FirebasePerformance}" == 'true' ]]; then
4855
BINARY_SIZE_SDK+=('FirebasePerformance');
4956
fi
50-
if $FirebaseRemoteConfig == 'true'; then
57+
if [[ "${POSTSUBMIT}" == true || "${FirebaseRemoteConfig}" == 'true' ]]; then
5158
BINARY_SIZE_SDK+=('FirebaseRemoteConfig')
5259
fi
53-
if $FirebaseStorage == 'true'; then
60+
if [[ "${POSTSUBMIT}" == true || "${FirebaseStorage}" == 'true' ]]; then
5461
BINARY_SIZE_SDK+=('FirebaseStorage')
5562
fi
5663
if [ -n "$BINARY_SIZE_SDK" ]; then
5764
cd scripts/health_metrics/generate_code_coverage_report/
5865
git clone https://github.com/google/cocoapods-size
5966
swift build
60-
.build/debug/BinarySizeReportGenerator --binary-size-tool-dir cocoapods-size/ --sdk-repo-dir "${GITHUB_WORKSPACE}" --sdk ${BINARY_SIZE_SDK[@]} --log-path "https://github.com/firebase/firebase-ios-sdk/actions/runs/${GITHUB_RUN_ID}"
67+
if [[ "${POSTSUBMIT}" == true ]]; then
68+
.build/debug/BinarySizeReportGenerator --binary-size-tool-dir cocoapods-size/ --sdk-repo-dir "${GITHUB_WORKSPACE}" --sdk ${BINARY_SIZE_SDK[@]} --merge "firebase/firebase-ios-sdk" --head-commit "${GITHUB_SHA}" --token $(gcloud auth print-identity-token) --log-link "https://github.com/firebase/firebase-ios-sdk/actions/runs/${GITHUB_RUN_ID}" --source-branch "${SOURCE_BRANCH}"
69+
else
70+
.build/debug/BinarySizeReportGenerator --binary-size-tool-dir cocoapods-size/ --sdk-repo-dir "${GITHUB_WORKSPACE}" --sdk ${BINARY_SIZE_SDK[@]} --presubmit "firebase/firebase-ios-sdk" --head-commit "${GITHUB_SHA}" --token $(gcloud auth print-identity-token) --log-link "https://github.com/firebase/firebase-ios-sdk/actions/runs/${GITHUB_RUN_ID}" --pull-request-num "${PULL_REQUEST_NUM}" --base-commit "${BASE_COMMIT}"
71+
fi
6172
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import ArgumentParser
18+
import Foundation
19+
import Utils
20+
21+
private enum Constants {}
22+
23+
extension Constants {
24+
// Binary Size Metrics flag for the Metrics Service.
25+
static let metric = "BinarySize"
26+
static let cocoapodSizeReportFile = "binary_report.json"
27+
}
28+
29+
/// Pod Config
30+
struct PodConfigs: Codable {
31+
let pods: [Pod]
32+
}
33+
34+
struct Pod: Codable {
35+
let sdk: String
36+
let path: String
37+
}
38+
39+
/// Cocoapods-size tool report,
40+
struct SDKBinaryReport: Codable {
41+
let combinedPodsExtraSize: Int
42+
43+
enum CodingKeys: String, CodingKey {
44+
case combinedPodsExtraSize = "combined_pods_extra_size"
45+
}
46+
}
47+
48+
/// Metrics Service API request data
49+
public struct BinaryMetricsReport: Codable {
50+
let metric: String
51+
let results: [Result]
52+
let log: String
53+
}
54+
55+
public struct Result: Codable {
56+
let sdk, type: String
57+
let value: Int
58+
}
59+
60+
extension BinaryMetricsReport {
61+
func toData() -> Data {
62+
let jsonData = try! JSONEncoder().encode(self)
63+
return jsonData
64+
}
65+
}
66+
67+
func CreatePodConfigJSON(of sdks: [String], from sdk_dir: URL) throws {
68+
var pods: [Pod] = []
69+
for sdk in sdks {
70+
let pod: Pod = Pod(sdk: sdk, path: sdk_dir.path)
71+
pods.append(pod)
72+
}
73+
let podConfigs: PodConfigs = PodConfigs(pods: pods)
74+
try JSONParser.writeJSON(of: podConfigs, to: "./cocoapods_source_config.json")
75+
}
76+
77+
// Create a JSON format data prepared to send to the Metrics Service.
78+
func CreateMetricsRequestData(of sdks: [String], type: String,
79+
log: String) throws -> BinaryMetricsReport {
80+
var reports: [Result] = []
81+
// Create a report for each individual SDK and collect all of them into reports.
82+
for sdk in sdks {
83+
// Create a report, generated by cocoapods-size, for each SDK. `.stdout` is used
84+
// since `pipe` could cause the tool to hang. That is probably caused by cocopod-size
85+
// is using pipe and a pipe is shared by multiple parent/child process and cause
86+
// deadlock. `.stdout` is a quick way to resolve at the moment. The difference is
87+
// that `.stdout` will print out logs in the console while pipe can assign logs a
88+
// variable.
89+
Shell.run(
90+
"cd cocoapods-size && python3 measure_cocoapod_size.py --cocoapods \(sdk) --cocoapods_source_config ../cocoapods_source_config.json --json \(Constants.cocoapodSizeReportFile)",
91+
stdout: .stdout
92+
)
93+
let SDKBinarySize = try JSONParser.readJSON(
94+
of: SDKBinaryReport.self,
95+
from: "cocoapods-size/\(Constants.cocoapodSizeReportFile)"
96+
)
97+
// Append reports for data for API request to the Metrics Service.
98+
reports.append(Result(sdk: sdk, type: type, value: SDKBinarySize.combinedPodsExtraSize))
99+
}
100+
let metricsRequestReport = BinaryMetricsReport(
101+
metric: Constants.metric,
102+
results: reports,
103+
log: log
104+
)
105+
return metricsRequestReport
106+
}
107+
108+
public func CreateMetricsRequestData(SDK: [String], SDKRepoDir: URL,
109+
logPath: String) throws -> BinaryMetricsReport? {
110+
try CreatePodConfigJSON(of: SDK, from: SDKRepoDir)
111+
let data = try CreateMetricsRequestData(
112+
of: SDK,
113+
type: "firebase-ios-sdk-testing",
114+
log: logPath
115+
)
116+
return data
117+
}

0 commit comments

Comments
 (0)