11cb0ef41Sopenharmony_ci#1/bin/env bash
21cb0ef41Sopenharmony_ciset -e
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciusage() {
51cb0ef41Sopenharmony_cicat << EOF
61cb0ef41Sopenharmony_ciusage: $0 OPTIONS RESULTS_DIR | TRACE_JSON
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciConvert telemetry json trace result to callstats.html compatible
91cb0ef41Sopenharmony_civersions ot ./out.json
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciOPTIONS:
121cb0ef41Sopenharmony_ci  -h           Show this message.
131cb0ef41Sopenharmony_ci  RESULTS_DIR  tools/perf/artifacts/run_XXX
141cb0ef41Sopenharmony_ci  TRACE_JSON   .json trace files
151cb0ef41Sopenharmony_ciEOF
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciwhile getopts ":h" OPTION ; do
201cb0ef41Sopenharmony_ci  case $OPTION in
211cb0ef41Sopenharmony_ci    h)  usage
221cb0ef41Sopenharmony_ci        exit 0
231cb0ef41Sopenharmony_ci        ;;
241cb0ef41Sopenharmony_ci    ?)  echo "Illegal option: -$OPTARG"
251cb0ef41Sopenharmony_ci        usage
261cb0ef41Sopenharmony_ci        exit 1
271cb0ef41Sopenharmony_ci        ;;
281cb0ef41Sopenharmony_ci  esac
291cb0ef41Sopenharmony_cidone
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci# =======================================================================
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciif [[ "$1" == *.json ]]; then
341cb0ef41Sopenharmony_ci  echo "Converting json files"
351cb0ef41Sopenharmony_ci  JSON=$1
361cb0ef41Sopenharmony_cielif [[ -e "$1" ]]; then
371cb0ef41Sopenharmony_ci  echo "Converting reults dir"
381cb0ef41Sopenharmony_ci  RESULTS_DIR=$1
391cb0ef41Sopenharmony_cielse
401cb0ef41Sopenharmony_ci  echo "RESULTS_DIR '$RESULTS_DIR' not found";
411cb0ef41Sopenharmony_ci  usage;
421cb0ef41Sopenharmony_ci  exit 1;
431cb0ef41Sopenharmony_cifi
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciOUT=out.json
471cb0ef41Sopenharmony_ciif [[ -e $OUT ]]; then
481cb0ef41Sopenharmony_ci  echo "# Creating backup for $OUT"
491cb0ef41Sopenharmony_ci  cp $OUT $OUT.bak
501cb0ef41Sopenharmony_cifi
511cb0ef41Sopenharmony_ciecho "# Writing to $OUT"
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cifunction convert {
551cb0ef41Sopenharmony_ci  NAME=$1
561cb0ef41Sopenharmony_ci  JSON=$2
571cb0ef41Sopenharmony_ci  # Check if any json file exists:
581cb0ef41Sopenharmony_ci  if ls $JSON 1> /dev/null 2>&1; then
591cb0ef41Sopenharmony_ci    du -sh $JSON;
601cb0ef41Sopenharmony_ci    echo "Converting NAME=$NAME";
611cb0ef41Sopenharmony_ci    echo "," >> $OUT;
621cb0ef41Sopenharmony_ci    echo "\"$NAME\": " >> $OUT;
631cb0ef41Sopenharmony_ci    jq '[.traceEvents[].args | select(."runtime-call-stats" != null) | ."runtime-call-stats"]' $JSON >> $OUT;
641cb0ef41Sopenharmony_ci  fi
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciecho '{ "telemetry-results": { "placeholder":{}' > $OUT
691cb0ef41Sopenharmony_ciif [[ $RESULTS_DIR ]]; then
701cb0ef41Sopenharmony_ci  for PAGE_DIR in $RESULTS_DIR/*_1; do
711cb0ef41Sopenharmony_ci    NAME=`basename $PAGE_DIR`;
721cb0ef41Sopenharmony_ci    JSON="$PAGE_DIR/trace/traceEvents/*_converted.json";
731cb0ef41Sopenharmony_ci    convert $NAME $JSON
741cb0ef41Sopenharmony_ci  done
751cb0ef41Sopenharmony_cielse
761cb0ef41Sopenharmony_ci  for JSON in $@; do
771cb0ef41Sopenharmony_ci    convert $JSON $JSON
781cb0ef41Sopenharmony_ci  done
791cb0ef41Sopenharmony_cifi
801cb0ef41Sopenharmony_ciecho '}}' >> $OUT
81