# Redis 메모리 & 시간 측정 스크립트

 

Python으로 redis 테스트 시 메모리와 시간 측정을 위한 스크립트

#!/bin/sh
 
### help message
function help() {
/bin/cat << EOF
Usage :
    redis_test.sh [-n/--filename] filename [-f/--flushall] [-h/--help]
EOF
}
    
### flag
INPUT_FILE=False
  
### check parameter
while (("$#")); do
    case "$1" in
        -n|--filename)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                INPUT_FILE=$2
                shift 2
            else
                echo "[Error] Argument for $1 is missing" >&2
                exit 1
            fi
            ;;
        -f|--flushall)
            FLUSHALL=True
            shift
            ;;
        -h|--help)
            help
            exit 1
            ;;
        -*|--*) # unsupported flags
            echo "[Error] Unsupported flag: $1" >&2
            echo "$0 -h for help message" >&2
            exit 1
            ;;
        *)
            echo "[Error]: Arguments with not proper flag: $1" >&2
            echo "$0 -h for help message" >&2
            exit 1
            ;;
    esac
done
   
  
### check if input filename
if [ $# -eq 0 ] && [ "$INPUT_FILE" == "False" ]
  then
    echo "[Error] Please give me test file"
    help;
    exit 1;
fi
   
 
### flushall execution
if [[ "$FLUSHALL" == True ]]
then
    ok=`redis-cli FLUSHALL`
    echo "[Info] redis memory flush all : ${ok}"
fi
 
  
### check memory and time before test
before=`redis-cli INFO memory | grep 'used_memory:' | awk -F: '{print $2}'`
b=`echo "${before//[$'\t\r\n ']}"`
StartTime=$(date +%s)
  
### test execution
echo "[Info] test start"
################################################ 테스트 실행 파일
python3 $INPUT_FILE
echo "[Info] test end"
  
### check memory and time after test
after=`redis-cli INFO memory | grep 'used_memory:' | awk -F: '{print $2}'`
a=`echo "${after//[$'\t\r\n ']}"`
EndTime=$(date +%s)
  
used=$(expr $a - $b)
  
echo ""
echo ""
echo "####################################[result]################################################"
echo "[result] redis used memory ${used}"
echo "[result] It takes $(($EndTime - $StartTime)) seconds to complete this task."

 

 

[참고문서]
https://tyanjournal.com/tips/bash-command-line-option-parsing%ED%95%98%EA%B8%B0-%EC%98%B5%EC%85%98-%EB%B0%9B%EA%B8%B0/
https://knight76.tistory.com/entry/Redis-%EB%A0%88%EB%94%94%EC%8A%A4-%ED%85%8C%EC%8A%A4%ED%8A%B8%ED%9B%84-%EB%A9%94%EB%AA%A8%EB%A6%AC-%EC%B8%A1%EC%A0%95%ED%95%98%EA%B8%B0-redis-used-memory

 

To be continued.........

 

 

Made by 꿩

'IT > Bash' 카테고리의 다른 글

[Python] Database connection  (0) 2022.05.09
[Linux] SSH 공개키 설정  (0) 2022.03.28
[Bash] 스크립트 #! 의미  (0) 2021.10.19
[Bash] 터미널 jar 파일 생성  (0) 2021.10.05
[centos] java openjdk 설치 및 삭제  (0) 2021.10.05

+ Recent posts