0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ホットスポットプロファイルの可視化を試してみたメモ

Last updated at Posted at 2024-11-27

概要

犯罪捜査技術を活用したソフトウェア開発手法 ~ フォレンジックアプローチによるソースコード分析 ~ 」を読んでいる。

ホットスポットプロファイルの可視化を試してみた。

ソースコード

フォルダ

下記のような構造になっているとする。
ここで、code-maatはadamtornhill/code-maatをcloneしたもの、projectはプロファイルを作成したい自分のプロジェクトとする。
project/docs/hotspotsにホットスポットプロファイル作成用のファイルを置くとする。

- project
  - docs
     + hotspots
  - src
- code-maat
  - Dockerfile 

プロファイル作成手順

code-maatのDockerfileを作成

project/docs/hotsports/bin/build-docker.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
maat_dir=$(cd $bin_dir/../../../../code-maat && pwd)

cd $maat_dir

docker build -t code-maat-app .

gitのログ1年分を出力

project/docs/hotsports/bin/create_log.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)

git log --all --numstat --date=short --pretty=format:'--%h--%ad--%aN' --no-renames --after=2023-01-01 > $bin_dir/../data/logfile.log

gitのログからコミット頻度の抽出とソースコードの行数出力

project/docs/hotsports/bin/summary-csv.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
data_dir=$(cd $bin_dir/../data && pwd)
root_dir=$(cd $bin_dir/../../.. && pwd)

docker run -v $data_dir:/data -it code-maat-app -l /data/logfile.log -c git2 > $data_dir/logfile.csv
docker run -v $data_dir:/data -it code-maat-app -l /data/logfile.log -c git2 -a revisions > $data_dir/revisions.csv
docker run --rm -v $root_dir:/tmp aldanial/cloc --unix --by-file --csv --quiet --timeout 10 --vcs=git --exclude-dir=docs,.vscode,.github --not-match-f=package-lock\.json --report-file=./docs/hotsports/data/complexity.csv

D3に読み込ませるようのJSONに成形

csv_as_enclosure_json.py

project/docs/hotsports/bin/csv-to-d3-json.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
py_dir=$(cd $bin_dir/../python && pwd)

cd $py_dir && python csv_as_enclosure_json.py --structure ../data/complexity.csv --weights ../data/revisions.csv > ../public/hotspots.json

ローカル環境で確認

index.html

ここまでの手順でhotspots.jsonができたら下記で確認できる。
実行はprojects/docs/code-maatフォルダにいる状態で行ったものとsる。

python -m http.server 8080

実行すると下記のようなホットスポットプロファイルが作成される。

image.png

2024.11.30 追記 PythonもDockerで立てたバージョン

bin/csv-to-d3-json.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
parent_dir=$(cd $bin_dir/.. && pwd)

cd $parent_dir && docker run -v $(pwd):/work python:3.12 python /work/python/csv_as_enclosure_json.py --structure /work/data/complexity.csv --weights /work/data/revisions.csv > ./crime-scene-hotspots/hotspots.json
bin/http.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
parent_dir=$(cd $bin_dir/.. && pwd)

cd $parent_dir && docker run -it --rm -p 8080:8080 -v $(pwd)/crime-scene-hotspots:/crime-scene-hotspots python:3-alpine python -m http.server 8080

2024.12.12追記 code-maatのjarを使用する

bin/summary-csv.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
data_dir=$(cd $bin_dir/../data && pwd)
root_dir=$(cd $bin_dir/../../.. && pwd)

if [ ! -e $data_dir/code-maat-1.0.4-standalone.jar ]; then
  wget https://github.com/adamtornhill/code-maat/releases/download/v1.0.4/code-maat-1.0.4-standalone.jar -P $data_dir
fi

docker run -v $data_dir:/data -it eclipse-temurin:21 java -jar /data/code-maat-1.0.4-standalone.jar -l /data/logfile.log -c git2 -a revisions > $data_dir/revisions.csv
docker run --rm -v $root_dir:/tmp aldanial/cloc --unix --by-file --csv --quiet --timeout 10 --vcs=git --exclude-dir=docs,.vscode,.github --not-match-f=\.json --report-file=./docs/code-maat/data/complexity.csv

ソースコード

参考

https://github.com/adamtornhill/code-maat
https://github.com/adamtornhill/maat-scripts

https://github.com/AlDanial/cloc
https://tinyurl.com/merge-complexity-script
https://tinyurl.com/visualizeenclosure-json

clocでgit管理下にあるファイルだけを対象とする
clocで *.spec.ts と /node_modules/ を除いたプログラム規模算出

Line count, exceeded timeout

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?