test-allocation-counts.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # Copyright 2021, gRPC Authors All rights reserved.
  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. # This script was adapted from SwiftNIO's test_01_allocation_counts.sh. The
  16. # license for the original work is reproduced below. See NOTICES.txt for more.
  17. ##===----------------------------------------------------------------------===##
  18. ##
  19. ## This source file is part of the SwiftNIO open source project
  20. ##
  21. ## Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
  22. ## Licensed under Apache License v2.0
  23. ##
  24. ## See LICENSE.txt for license information
  25. ## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
  26. ##
  27. ## SPDX-License-Identifier: Apache-2.0
  28. ##
  29. ##===----------------------------------------------------------------------===##
  30. set -eu
  31. here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  32. tmp="/tmp"
  33. source "$here/test-utils.sh"
  34. all_tests=()
  35. for file in "$here/tests/"test_*.swift; do
  36. # Extract the "TESTNAME" from "test_TESTNAME.swift"
  37. test_name=$(basename "$file")
  38. test_name=${test_name#test_*}
  39. test_name=${test_name%*.swift}
  40. all_tests+=( "$test_name" )
  41. done
  42. # Run all the tests.
  43. "$here/tests/run-allocation-counter-tests.sh" -t "$tmp" | tee "$tmp/output"
  44. # Dump some output from each, check for allocations.
  45. for test in "${all_tests[@]}"; do
  46. while read -r test_case; do
  47. test_case=${test_case#test_*}
  48. total_allocations=$(grep "^test_$test_case.total_allocations:" "$tmp/output" | cut -d: -f2 | sed 's/ //g')
  49. not_freed_allocations=$(grep "^test_$test_case.remaining_allocations:" "$tmp/output" | cut -d: -f2 | sed 's/ //g')
  50. max_allowed_env_name="MAX_ALLOCS_ALLOWED_$test_case"
  51. info "$test_case: allocations not freed: $not_freed_allocations"
  52. info "$test_case: total number of mallocs: $total_allocations"
  53. assert_less_than "$not_freed_allocations" 5 # allow some slack
  54. assert_greater_than "$not_freed_allocations" -5 # allow some slack
  55. if [[ -z "${!max_allowed_env_name+x}" ]]; then
  56. if [[ -z "${!max_allowed_env_name+x}" ]]; then
  57. warn "no reference number of allocations set (set to \$$max_allowed_env_name)"
  58. warn "to set current number:"
  59. warn " export $max_allowed_env_name=$total_allocations"
  60. fi
  61. else
  62. max_allowed=${!max_allowed_env_name}
  63. assert_less_than_or_equal "$total_allocations" "$max_allowed"
  64. assert_greater_than "$total_allocations" "$(( max_allowed - 1000))"
  65. fi
  66. done < <(grep "^test_$test[^\W]*.total_allocations:" "$tmp/output" | cut -d: -f1 | cut -d. -f1 | sort | uniq)
  67. done