はじめに
ソースコードや設定ファイルを検索するスピードはシステム開発・運用に欠かせない。
例えば以下のような場面が挙げられる。
- キーワードを含む ソースコード の検索
- 問題のエラーを吐いている ログファイル の検索
- 特定の設定値が記述されている 設定ファイル の検索
随分前は grep コマンドを使っていたが the_silver_searcher を見つけてからは ag コマンドに置き換えた。
その the_silver_searcher よりも更に高速な the_platinum_searcher なるものを見つけたので導入してみる。
特徴
一部抜粋
* It searches code about 3–5× faster than ack.
* It searches code as fast as the_silver_searcher(ag).
* It ignores file patterns from your .gitignore.
* It ignores directories with names that start with ., eg .config. Use --hidden option, if you want to search.
* It searches UTF-8, EUC-JP and Shift_JIS files.
* It provides binaries for multi platform (macOS, Windows, Linux).
日本語訳すると以下のようになる。
* ackよりも3〜5倍速くコードを検索します。
* the_silver_searcher(ag)と同じくらい速くコードを検索します。
* .gitignoreからのファイルパターンは無視されます。
* .で始まる名前のディレクトリは無視されます(例:.config)。検索したい場合は、 --hiddenオプションを使用してください。
* UTF-8、EUC-JP、Shift_JISファイルを検索します。
*マルチプラットフォーム(macOS、Windows、Linux)用のバイナリを提供します。
環境
$ uname -a
Darwin NP178 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64 i386 MacBookPro11,4 Darwin
$ zsh --version
zsh 5.6.2 (x86_64-apple-darwin17.7.0)
インストール
以下を参照
the_platinum_searcher/README.md at master · monochromegane/the_platinum_searcher · GitHub
Macだと Homebrew で一発。もちろん他のプラットフォーム向けのインストール方法も記載されている。
$ brew install pt
以下のコマンドでバージョンが表示されればインストールOK。
$ pt --version
pt version 2.2.0
結果
自分のMacBook上の /etc
から特定の文字列を含むファイルを探してみる。
$ sudo pt cron /etc/*
/etc/php.ini.default
1441:; collection through a shell script, cron entry, or some other method.
/etc/php.ini.default-previous~orig
1531:; collection through a shell script, cron entry, or some other method.
/etc/php.ini.default-previous
1441:; collection through a shell script, cron entry, or some other method.
/etc/security/audit_event
575:6147:AUE_cron_invoke:cron-invoke:ad
576:6148:AUE_crontab_create:crontab-crontab created:ad
577:6149:AUE_crontab_delete:crontab-crontab deleted:ad
578:6150:AUE_crontab_perm:crontab-permission:no
598:6170:AUE_crontab_mod:crontab-modify:ad
従来の grep コマンドっぽく出力するには以下のようにオプションを付与する
$ sudo pt --nogroup cron /etc/*
/etc/php.ini.default:1441:; collection through a shell script, cron entry, or some other method.
/etc/php.ini.default-previous:1441:; collection through a shell script, cron entry, or some other method.
/etc/php.ini.default-previous~orig:1531:; collection through a shell script, cron entry, or some other method.
/etc/security/audit_event:575:6147:AUE_cron_invoke:cron-invoke:ad
/etc/security/audit_event:576:6148:AUE_crontab_create:crontab-crontab created:ad
/etc/security/audit_event:577:6149:AUE_crontab_delete:crontab-crontab deleted:ad
/etc/security/audit_event:578:6150:AUE_crontab_perm:crontab-permission:no
/etc/security/audit_event:598:6170:AUE_crontab_mod:crontab-modify:ad
※aliasを設定しておくのがよいと思われる
echo "alias pt='pt --nogroup'" >> ~/.bashrc
ちなみにファイル名のみを出力
$ sudo pt --files-with-matches cron /etc/*
/etc/php.ini.default
/etc/php.ini.default-previous
/etc/php.ini.default-previous~orig
/etc/security/audit_event
その他
time コマンドで grep や ag と比較すると、 pt のほうがSystem CPUの使用率が高い(300%超えたりする)ので、並列処理で高速化させるような仕組みなのか?