4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows業務PCで開発するならWSLが必須

4
Posted at

はじめに

  • 会社で支給されたPCで開発を行ってきたが、最近驚愕の事実が判明した。気にはなっていたが、実際調べてみると愕然とした。
  • そこそこのPCは与えられているが、少しでも早く、速く、ということで、WSLのツールは使わず、中間を排除して、Windows直で開発していた。それが早いと信じていた。ローカル仮想化とかも、直でやったほうが早いだろうと今まで考えていた。そこが問題
  • フロントエンドの開発でファイルが多くなってきたので、最近なんか遅い遅いと感じ始め、WSL上で開発したら速度上がるのかと思いWSLに移行してみた。
  • WSLに移行してわかったことをまとめます。
  • 今回のベンチは自身のPCのみで検証したので、他の環境にすべて当てはまるとはかぎりません。

ベンチマーク取ってみた。

内容

  • pythonスクリプトでファイルの作成、読込、編集、削除を50000ファイルで行う。
  • WindowsとWSLの時間を計測し比較する。
  • 傾向ぐらいを知りたかったので、0.01秒を争うものではなく、ちょっと実行して調べるぐらいの精度です。
  • 結果も10回ぐらい回して、平均的なものを一つ選びました。

作成したpy

  • AIに作成してもらいました。
from __future__ import annotations

import argparse
import shutil
from pathlib import Path
from time import perf_counter


FILE_COUNT = 50000
WRITE_SIZE = 1024
BENCH_DIR_NAME = "___benti"


def build_payload(write_size: int, index: int) -> str:
	"""指定サイズの書き込みデータを作る。"""
	base_text = f"benchmark-{index}-"
	repeat_count = (write_size // len(base_text)) + 1
	payload = (base_text * repeat_count)[:write_size]
	return payload


def create_files(target_dir: Path, file_count: int, write_size: int) -> None:
	"""指定数のファイルを作成する。"""
	target_dir.mkdir(parents=True, exist_ok=True)

	for index in range(file_count):
		file_path = target_dir / f"file_{index:04d}.txt"
		file_path.write_text(build_payload(write_size, index), encoding="utf-8")


def read_files(target_dir: Path) -> None:
	"""ディレクトリ内の全ファイルを読み込む。"""
	for file_path in sorted(target_dir.iterdir()):
		if file_path.is_file():
			file_path.read_text(encoding="utf-8")


def update_files(target_dir: Path, write_size: int, file_count: int) -> None:
	"""ディレクトリ内の全ファイルを書き換える。"""
	for index, file_path in enumerate(sorted(target_dir.iterdir())):
		if file_path.is_file():
			file_path.write_text(
				build_payload(write_size, index + file_count), encoding="utf-8"
			)


def delete_files(target_dir: Path) -> None:
	"""ディレクトリ内の全ファイルを削除する。"""
	for file_path in sorted(target_dir.iterdir()):
		if file_path.is_file():
			file_path.unlink()


def cleanup_directory(target_dir: Path) -> None:
	"""ベンチ用ディレクトリを片付ける。"""
	if target_dir.exists():
		shutil.rmtree(target_dir)


def run_benchmark(base_dir: Path, file_count: int, write_size: int) -> None:
	"""ファイル作成、読み込み、削除の各処理時間を計測する。"""
	target_dir = base_dir / BENCH_DIR_NAME
	cleanup_directory(target_dir)

	measurements: list[tuple[str, float]] = []

	start = perf_counter()
	create_files(target_dir, file_count, write_size)
	measurements.append(("作成", perf_counter() - start))

	start = perf_counter()
	read_files(target_dir)
	measurements.append(("読み込み", perf_counter() - start))

	start = perf_counter()
	update_files(target_dir, write_size, file_count)
	measurements.append(("更新", perf_counter() - start))

	start = perf_counter()
	delete_files(target_dir)
	measurements.append(("削除", perf_counter() - start))

	cleanup_directory(target_dir)
	total_elapsed = sum(elapsed for _, elapsed in measurements)

	for label, elapsed in measurements:
		print(f"{label}: {elapsed:.6f} 秒")
	print(f"合計: {total_elapsed:.6f} 秒")


def parse_args() -> argparse.Namespace:
	"""コマンドライン引数を解析する。"""
	parser = argparse.ArgumentParser(description="ファイル操作の簡易ベンチマーク")
	parser.add_argument(
		"--count",
		type=int,
		default=FILE_COUNT,
		help="作成・読み込み・削除するファイル数",
	)
	parser.add_argument(
		"--write-size",
		type=int,
		default=WRITE_SIZE,
		help="1ファイルあたりの書き込み容量(バイト)",
	)
	parser.add_argument(
		"--base-dir",
		type=Path,
		default=Path(__file__).resolve().parent.parent,
		help="___benti ディレクトリを作成する基準ディレクトリ",
	)
	return parser.parse_args()


def main() -> None:
	"""ベンチマークを実行する。"""
	args = parse_args()
	run_benchmark(args.base_dir, args.count, args.write_size)


if __name__ == "__main__":
	main()

結果

Windows上で実行

作成: 35.720847 秒
読み込み: 25.800299 秒
更新: 112.636500 秒
削除: 80.074438 秒
合計: 254.232084 秒

WSL上で実行

作成: 1.089341 秒
読み込み: 0.702191 秒
更新: 2.085664 秒
削除: 0.701737 秒
合計: 4.578932 秒

比較

倍
32.79124443
36.74256577
54.00510341
114.1089012
55.52213573

まとめ

  • あらためて数値で見ると、55倍はひどすぎる。秒で終わる処理が分かかる。読込だけでも36倍って。
  • 50000ファイル読み書きしただけで、4分近くかかるのは、やめてほしい。
  • SKYSEA Client Viewは本当に遅い。windowsで値を取っている時に、すごく頑張っていました。どれだけ足を引っ張れば、許されますか。
  • ウィルス検知ソフトもさらに頑張っていました。相乗効果で遅くしている。
  • 原因は、Windows自体ではなく、セキュリティソフトだと思われます。原因の特定はしていない。
  • 最新のPCもらったところで、焼け石に水です。
  • フロントエンド開発は、node_modulesにたくさんのファイルができます。30倍余計に時間がかかります。
  • セキュリティソフト気になる人は、確認すると、良いです。

結論

  • 業務PCでWindows直で開発を行っている人は、すぐにWSL等の仮想空間に逃げたほうがいいです。AI開発を行っている人も。
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?