Get number of warnings and errors from Xcodebuild as metrics.txt

Some CI servers or plugins provide support for metrics graphs. In order to have some metrics we simply need to generate a file that conforms to the OpenMetrics format which is a mere text file in the form:

key1 value1
key2 value2
key3 value3
...
Gitlab CI (Silver and Gold tiers) support this.

Simple metrics for iOS projects

I do this in two separate steps.
  1. Extract information to later build the metrics.txt file

    Build project and save log somewhere so it is usable later.

    xcrun xcodebuild -project ProjectNameHere.xcodeproj/ -scheme SchemeNameHere -destination "platform=iOS Simulator,name=iPhone 8,OS=latest" build -quiet | tee xcodebuild.log 
  2. Build the metrics.txt

    With a script like the following (generate-metrics.sh) we can grep the warnings and errors number. and write each as different key.

    #! /usr/bin/env bash
    # set -x # For debug
    set -e
    
    BUILD_LOG_FILE="$1"
    METRICS_FILE="${2}/metrics.txt"
    
    # Remove if found
    rm -rf "$METRICS_FILE"
    
    # Count warnings
    echo Counting warnings
    WARNING_COUNT=`egrep '^(/.+:[0-9+:[0-9]+:.(warning):|fatal|===)' "$BUILD_LOG_FILE" | uniq | wc -l`
    echo "warnings" $WARNING_COUNT >> "$METRICS_FILE"
    
    # Count errors
    echo Counting errors
    ERROR_COUNT=`egrep '^(/.+:[0-9+:[0-9]+:.(error):|fatal|===)' "$BUILD_LOG_FILE" | uniq | wc -l`
    echo "errors" $ERROR_COUNT >> "$METRICS_FILE"
    
    # Print results
    cat "$METRICS_FILE"

    Usage:

    generate-metrics.sh xcodebuild.log . 

Collecting metrics in each merge request or after merges are done might be useful to have an idea how application evolves through time. I am collecting warnings and errors count only but various things could be measured like application load time, application size, time to do certain operation, number of total tests, number of failed tests, number of crash reports per version, etc.
Not all of them would need xcodebuild command and not all of them would be calculated at the same time but this should be doable.

0 comments :

This work is licensed under BSD Zero Clause License | nacho4d ®