8

每个程序员都应该知道的 latency time

 3 years ago
source link: https://mp.weixin.qq.com/s?__biz=Mzg3NjU1NzM1Mw%3D%3D&%3Bmid=2247487575&%3Bidx=1&%3Bsn=7d1e4c2d3fc3be1bbfd75dad4e2c3944&%3Bchksm=cf313511f846bc07c47b444aa884c060474f24994bf1b2243c311f2347be985307a27147e2d3&%3Btoken=1688181212&%3Blang=zh_CN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

QjEzIjm.jpg!mobile

Photo by  Denny Müller  on  Unsplash

如果你是计算机系毕业的,可以不会修电脑,但应该了解几组关于  “Latency time”  的数字, 诸如 L1 cache reference L 2  c a c he  reference Disk seek   等操作的耗时情况。

2001  年, Peter Norvig 曾任  Google  搜索质量总监,经典教材《人工智能:一种现代方法》的作者之一 )在 “  Teach Yourself Programming in Ten Years”  一文中,首次讨论了 计算机领域 中  memory Cache Disk network 等相关延时的数据( By the way ,“  Teach Yourself Programming in Ten Years ”这篇文章本身也是神文, 20 年过去了,依旧经典

femQBvj.jpg!mobile

而让这些数字再次受关注的,是  Google 的另一位 大神。 2010  年, Jeff  Dean  在斯坦福做了一次精彩的演讲:“ Building Software Systems at Google and Lessons Learned” ,其中又讨论了这些计算机领域的 latency 。毕竟,要做出优秀的架构,反而需要更加关注那些基础的领域知识。

JrYjYrI.png!mobile

随后, 一名  UC Berkeley   的学生 将这些计算机系统中的  latency time  做了一个可交互的网页, 得出这些指标的 计算公式也写在网页的 源码里 https://colin-scott.github.io/personal_website/research/interactive_latency.html )。

拖动标记年份的滚动条,  我们可以很直观地看到,不同年份下, CPU cache main memory network 这些技术发展 对系统延迟时间的影响。

QJZVra.png!mobile

下面是  2020  的数据:

L1 cache reference

读取 CPU  一级缓存  latency

1 ns

1 纳秒 = 10 亿分之一秒

L2 cache  reference

读取  CPU 二级 缓存  latency

4 ns

CPU Branch mispredict

CPU 分支错误 测耗时

3 ns

Mutex Lo ck / Unlock

加互斥锁 / 解锁

17 n s

Main memory  reference

读取主内存数据

100 ns

Compress 1 KB with Zippy

1 k 字节压缩

2000 ns = 2 μs

1 微秒 = 100 万分之一秒

Read 1,000,000 bytes sequentially from main memory

从主内存中顺序读取 1 M  字节数据

3   μs

Solid State Drive (SSD) random read

SSD 中读取数据

16  μs

Read 1,000,000 bytes sequentially from SSD

SSD 中顺序读取 1 M  字节数据

4 9   μs

Read 1,000,000 bytes sequentially from disk

从机械 中顺序读取 1 M  字节数据

8 25  μs

Disk (Hard / magnetic drive) seek

从机械硬盘中随机读取数

2 ms

1 毫秒  = 1000 分之一秒

网络 latency

Send 2000 bytes over commodity 

network

通过商用网络发送 2k 字节数据

44 ns

Round trip network request in same 

data centre

在同一数据中心的 络请 往返 耗时

500 μs

Packet roundtrip CA to Netherlands  从加拿大到荷兰的 一次 络请 往返耗时

150 ms

通过分析这些变化,可以发现一些有意思的事情:

1.   2006 年开始,前两列操作的数值( L1 cache L2 cache ,解压缩)基本不再变化。从另一侧面反映了  CPU 芯片发展的时代烙印:从注重提升主频到往 多核方向发展;

2.   机械硬盘的 随机读写 性能 很差 ,比内存慢 1000 倍。而异地访问的网络请求时延,又比硬盘读慢 100 SSD   的随机读取速度 20 年间基本 没有提升,而顺序读取速度却从 50 ms 提升到 49 μs ,提升了 1000 倍。 在某些场景下,基于 SSD 的顺序读性能甚至好于内存的随机读;

3.  不管是在同一数据中心还是异地数据中心, RTT 基本不变, 一致保持 500   μs 150 ms 。假设信号在光纤中以近似光速传播,理论上 RTT 由物理规律决定。比如: 杭州到北京 距离大约为 1500   km RTT  延迟至少在: (  2 * 1500 km ) / ( 300,000  km/s ) = 10  ms

以上, Enjoy ~

参考资源:

1. http://norvig.com/21-days.html

2.  https://static.googleusercontent.com/media/research.google.com/en//people/jeff/Stanford-DL-Nov-2010.pdf

3.  https://colin-scott.github.io/personal_website/research/ interactive_latency.html


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK