#!/bin/sh

# 参数检查
if [ $# -ne 1 ]; then
    echo "Usage: $0 <data_file>"
    exit 1
fi

input_file=$1

# 文件存在性检查
if [ ! -f "$input_file" ]; then
    echo "Error: File '$input_file' not found!"
    exit 1
fi

# 初始化统计变量
total=0
time=0
free=0
single_throughput=0
single_free=0
num=0

while read -r line; do
    bytes=$(echo "$line" | awk '{print $2}')
    if [[ "$bytes" == *"bytes"* ]]; then
        # 使用awk提取三个数字
        bytes=$(echo "$line" | awk '{print $1}')
        usecs=$(echo "$line" | awk '{print $4}')
        kbs=$(echo "$line" | awk '{print $7}')
        
        total=$((total + kbs))
        time=$((time + usecs))
        num=$((num + 1))
        # 输出结果
        #echo "Bytes: $bytes, Microseconds: $usecs, KB/s: $kbs"
    elif [[ "$bytes" == *"usecs"* ]]; then
        usecs=$(echo "$line" | awk '{print $1}')
        free=$((free + usecs))
        #echo "free Microseconds: $usecs"
    fi
        
done < "$input_file"

echo $num
time=$(echo "scale=2; $time / 1000000" | bc)
total=$(echo "scale=2; $total / 1024" | bc)
single_throughput=$(echo "scale=2; $total / $num" | bc)
single_free=$(echo "scale=2; $free / $num" | bc)

echo "System time:               $time" "s"
echo "Sum Throughput:            $total" "MB/s"
echo "Single process Throughput: $single_throughput" "MB/s"
echo "free latency:              $single_free" "us"
