Windows10 本地只能通过 localhost 访问 WSL2 容器?
https://gist.github.com/Double1996/6a2178313b44c6bbdc90f0b9f881981a
加上 WSL2 资源消耗大、启动慢(虽然也只有几秒钟,但是比 WSL1 慢多了),所以不是必须用 WSL2 (比如 Docker )的场合,我都选择用 WSL1 。我的默认实例也是 WSL1 。
1. WSL1 的 IO 性能要远高于 WSL2,因为 WSL2 走的是 9P 协议,而 WSL1 用的是 lxfs,性能更高(差异非常明显,比如你用的是 zsh 加自动补全,那在 /mnt/c 下,WSL1 接近可用,而 WSL2 会非常卡)。我测试下来,WSL1 下,访问 NTFS 分区的性能,WSL1 接近于原生速度( NVME 的硬盘,写入速度接近 1GB/S ),但是在 WSL2 下就只有 97.3M/S (数据见后)
2. WSL2 的 CPU 性能比 WSL1 高,因为不用经过内核转换(但是实际看起来不是特别明显),在 CPU 密集型的应用下会有影响,比如编译
WSL1 下测试磁盘性能和结果:
time dd if=/dev/zero of=/mnt/c/TEMP/wsl bs=8k count=100000
100000+0 records in
100000+0 records out
819200000 bytes (819 MB, 781 MiB) copied, 0.829945 s, 987 MB/s
dd if=/dev/zero of=/mnt/c/TEMP/wsl bs=8k count=100000 0.11s user 1.42s system 99% cpu 1.538 total
WSL2 下测试磁盘性能和结果:
time dd if=/dev/zero of=/mnt/c/TEMP/wsl bs=8k count=100000
100000+0 records in
100000+0 records out
819200000 bytes (819 MB, 781 MiB) copied, 8.41766 s, 97.3 MB/s
dd if=/dev/zero of=/mnt/c/TEMP/wsl bs=8k count=100000 0.34s user 1.32s system 19% cpu 8.479 total
结果如上,表示看不懂这是 wsl1 还是 2 。。
wsl -l -v
NAME STATE VERSION
* Ubuntu Running 1
Ubuntu-20.04 Stopped 2
比如,我这么个简单的问题至今没有人能解决(估计微软也不想解决)
https://www.v2ex.com/t/668362
你如果追求 IO 性能,则不应该把 WSL2 的文件放在 /mnt/<drive>下面,而是保持在 ext4 文件系统下。另外好像 WSL2 还有方法 mount 一个新的.vhdx 作为数据盘。
总之我用 WSL 最大的意义在于互通访问,且能使用 Linux 的工具集。在这前提下,除非是需要底层的功能,否则 WSL2 对我没有多大意义。
The actual speed increase will depend on which app you’re running and how it is interacting with the file system. Initial versions of WSL 2 run up to 20x faster compared to WSL 1 when unpacking a zipped tarball, and around 2-5x faster when using git clone, npm install and cmake on various projects.”
比如试想这个场景,我宿主机上还有一台 windows 虚拟机(或者我在局域网有另一台电脑),我想在 windows 虚拟机里或者局域网里访问 wsl2 中的文件,不给 wsl2 一个真正的静态 ip 是没法解决这个问题的