|
| 1 | +#!/bin/bash |
| 2 | +##===----------------------------------------------------------------------===## |
| 3 | +## |
| 4 | +## This source file is part of the SwiftNIO open source project |
| 5 | +## |
| 6 | +## Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors |
| 7 | +## Licensed under Apache License v2.0 |
| 8 | +## |
| 9 | +## See LICENSE.txt for license information |
| 10 | +## See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 11 | +## |
| 12 | +## SPDX-License-Identifier: Apache-2.0 |
| 13 | +## |
| 14 | +##===----------------------------------------------------------------------===## |
| 15 | + |
| 16 | +# This script allows you to consume any Jenkins/alloc counter run output and |
| 17 | +# convert it into the right for for the docker-compose script. |
| 18 | + |
| 19 | +set -eu |
| 20 | + |
| 21 | +mode_flag=${1---docker-compose} |
| 22 | + |
| 23 | +function usage() { |
| 24 | + echo >&1 "Usage: $0 [--docker-compose|--export|--json]" |
| 25 | + echo >&1 |
| 26 | + echo >&1 "Example:" |
| 27 | + echo >&1 " # copy the output from the Jenkins CI into your clipboard, then" |
| 28 | + echo >&1 " pbpaste | $0 --docker-compose" |
| 29 | +} |
| 30 | + |
| 31 | +function die() { |
| 32 | + echo >&2 "ERROR: $*" |
| 33 | + exit 1 |
| 34 | +} |
| 35 | + |
| 36 | +case "$mode_flag" in |
| 37 | + --docker-compose) |
| 38 | + mode=docker |
| 39 | + ;; |
| 40 | + --export) |
| 41 | + mode="export" |
| 42 | + ;; |
| 43 | + --json) |
| 44 | + mode=json |
| 45 | + ;; |
| 46 | + *) |
| 47 | + usage |
| 48 | + exit 1 |
| 49 | + ;; |
| 50 | +esac |
| 51 | + |
| 52 | +function allow_slack() { |
| 53 | + raw="$1" |
| 54 | + if [[ ! "$raw" =~ ^[0-9]+$ ]]; then |
| 55 | + die "not a malloc count: '$raw'" |
| 56 | + fi |
| 57 | + if [[ "$raw" -lt 1000 ]]; then |
| 58 | + echo "$raw" |
| 59 | + return |
| 60 | + fi |
| 61 | + |
| 62 | + allocs=$raw |
| 63 | + while true; do |
| 64 | + allocs=$(( allocs + 1 )) |
| 65 | + if [[ "$allocs" =~ [0-9]+00$ || "$allocs" =~ [0-9]+50$ ]]; then |
| 66 | + echo "$allocs" |
| 67 | + return |
| 68 | + fi |
| 69 | + done |
| 70 | +} |
| 71 | + |
| 72 | +json_blob="{" |
| 73 | + |
| 74 | +lines=$(grep -e "total number of mallocs" -e ".total_allocations" -e "export MAX_ALLOCS_ALLOWED_" | \ |
| 75 | + sed -e "s/: total number of mallocs: /=/g" \ |
| 76 | + -e "s/.total_allocations: /=/g" \ |
| 77 | + -e "s/info: /test_/g" \ |
| 78 | + -e "s/export MAX_ALLOCS_ALLOWED_/test_/g" | \ |
| 79 | + grep -Eo 'test_[a-zA-Z0-9_-]+=[0-9]+' | sort | uniq) |
| 80 | + |
| 81 | +while read -r info; do |
| 82 | + test_name=$(echo "$info" | sed "s/test_//g" | cut -d= -f1 ) |
| 83 | + allocs=$(allow_slack "$(echo "$info" | cut -d= -f2 | sed "s/ //g")") |
| 84 | + case "$mode" in |
| 85 | + docker) |
| 86 | + echo " - MAX_ALLOCS_ALLOWED_$test_name=$allocs" |
| 87 | + ;; |
| 88 | + export) |
| 89 | + echo "export MAX_ALLOCS_ALLOWED_$test_name=$allocs" |
| 90 | + ;; |
| 91 | + json) |
| 92 | + json_blob="${json_blob}"$'\n'" \"$test_name\": $allocs," |
| 93 | + ;; |
| 94 | + *) |
| 95 | + die "Unexpected mode: $mode" |
| 96 | + ;; |
| 97 | + esac |
| 98 | +done <<< "$lines" |
| 99 | + |
| 100 | +if [[ "$mode" == "json" ]]; then |
| 101 | + json_blob=$(sed '$ s/,$//g' <<< "$json_blob") |
| 102 | + json_blob+=$'\n'"}" |
| 103 | + echo "$json_blob" |
| 104 | +fi |
0 commit comments