From 38d3e84e9a2666e844fc1dee302c6c7ddf909838 Mon Sep 17 00:00:00 2001 From: qijun Date: Mon, 17 Apr 2017 20:04:16 +0800 Subject: [PATCH] add profiling scripts --- .tools/build_docker.sh | 3 ++- .tools/profile/get_stats.py | 12 ++++++++++++ .tools/profile/profile.sh | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100755 .tools/profile/get_stats.py create mode 100755 .tools/profile/profile.sh diff --git a/.tools/build_docker.sh b/.tools/build_docker.sh index 7a06c18..a277d57 100755 --- a/.tools/build_docker.sh +++ b/.tools/build_docker.sh @@ -32,9 +32,10 @@ RUN ${update_mirror_cmd} apt-get update && \ apt-get install -y locales && \ apt-get -y install gcc && \ + apt-get -y install graphviz && \ apt-get -y clean && \ localedef -f UTF-8 -i en_US en_US.UTF-8 && \ - pip install -U matplotlib jupyter numpy requests scipy + pip install -U matplotlib jupyter numpy requests scipy gprof2dot EXPOSE 8888 CMD ["sh", "-c", "jupyter notebook --ip=0.0.0.0 --no-browser --allow-root --NotebookApp.token='' --NotebookApp.disable_check_xsrf=True /book/"] diff --git a/.tools/profile/get_stats.py b/.tools/profile/get_stats.py new file mode 100755 index 0000000..f10fd9e --- /dev/null +++ b/.tools/profile/get_stats.py @@ -0,0 +1,12 @@ +#!/usr/local/bin/python + +import pstats +import sys + +file_readable = sys.argv[1] + ".readable" +stream = open(file_readable, "w") + +p = pstats.Stats(sys.argv[1], stream=stream) +p.strip_dirs().sort_stats("time").print_stats(20) +p.strip_dirs().sort_stats("cumtime").print_stats(20) +stream.close() diff --git a/.tools/profile/profile.sh b/.tools/profile/profile.sh new file mode 100755 index 0000000..f3543bd --- /dev/null +++ b/.tools/profile/profile.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +book_dir=/book +work_dir=/book/.tools/profile +pushd $book_dir +array=() +while IFS= read -r -d $'\0'; do + array+=("$REPLY") +done < <(find ${book_dir} -name train.py -print0) + +for file in "${array[@]}"; do + echo $file + dir=$(dirname "${file}") + result=${file%.*}'.prof' + result_png=${result}'.png' + pushd $dir + python -m cProfile -o $result $file + popd + ${work_dir}/get_stats.py $result + gprof2dot -f pstats $result | dot -Tpng -o $result_png +done +popd -- GitLab