#47 程序员的 10 个习惯

2021-12-08

公众号四猿外的文章《这 10 个程序员的好习惯,让我变强了》,很有同感,这里总结一下:

  1. 要看官方文档,网上的资料鱼龙混杂
  2. 开发者的可靠性:自测没有问题了再交付
  3. 日志设计合理,保证调试时,有恰当的信息来定位问题
  4. 精通 Git
  5. 优先功能实现,别急着优化
    过早优化是魔鬼,应该根据实际运行的情况来优化
  6. 先做明确的需求,不确定或者模糊的需求先往后放
  7. 积极主动:发现问题,提出方案,解决问题
  8. 开发时间评估,保证有冗余时间,以处理可能的意外
  9. 学习编程主要靠上手写代码,不要光看文档
  10. 英语。不需要很好,能看懂英文文档就行

#46 Susam Pal:Curl 计时

2021-11-28

Here is a command I use often while measuring why an HTTP request is taking too long:

curl -L -w "time_namelookup: %{time_namelookup}
time_connect: %{time_connect}
time_appconnect: %{time_appconnect}
time_pretransfer: %{time_pretransfer}
time_redirect: %{time_redirect}
time_starttransfer: %{time_starttransfer}
time_total: %{time_total}
" https://example.com/

Here is the same command written as a one-liner, so that I can copy it easily from this page with a triple-click whenever I need it in future:

curl -L -w "time_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_appconnect: %{time_appconnect}\ntime_pretransfer: %{time_pretransfer}\ntime_redirect: %{time_redirect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" https://example.com/

Here is how the output of the above command typically looks:

$ curl -L -w "namelookup: %{time_namelookup}\nconnect: %{time_connect}\nappconnect: %{time_appconnect}\npretransfer: %{time_pretransfer}\nstarttransfer: %{time_starttransfer}\ntotal: %{time_total}\n" https://example.com/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
...
</html>
time_namelookup: 0.001403
time_connect: 0.245464
time_appconnect: 0.757656
time_pretransfer: 0.757823
time_redirect: 0.000000
time_starttransfer: 0.982111
time_total: 0.982326

In the output above, I have omitted most of the HTML output and replaced the omitted part with ellipsis for the sake of brevity.

The list below provides a description of each number in the output above. This information is picked straight from the manual page of curl 7.20.0. Here are the details:

  • time_namelookup: The time, in seconds, it took from the start until the name resolving was completed.
  • time_connect: The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
  • time_appconnect: The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0)
  • time_pretransfer: The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
  • time_redirect: The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3)
  • time_starttransfer: The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
  • time_total: The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.

An important thing worth noting here is that the difference in the numbers for time_appconnect and time_connect time tells us how much time is spent in SSL/TLS handshake. For a cleartext connection without SSL/TLS, this number is reported as zero. Here is an example output that demonstrates this:

$ curl -L -w "time_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_appconnect: %{time_appconnect}\ntime_pretransfer: %{time_pretransfer}\ntime_redirect: %{time_redirect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" http://example.com/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
...
</html>
time_namelookup: 0.001507
time_connect: 0.247032
time_appconnect: 0.000000
time_pretransfer: 0.247122
time_redirect: 0.000000
time_starttransfer: 0.512645
time_total: 0.512853

Also note that time_redirect is zero in both outputs above. That is because no redirection occurs while visiting example.com. Here is another example that shows how the output looks when a redirection occurs:

$ curl -L -w "time_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_appconnect: %{time_appconnect}\ntime_pretransfer: %{time_pretransfer}\ntime_redirect: %{time_redirect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" https://susam.in/blog
<!DOCTYPE HTML>
<html>
...
</html>
time_namelookup: 0.001886
time_connect: 0.152445
time_appconnect: 0.465326
time_pretransfer: 0.465413
time_redirect: 0.614289
time_starttransfer: 0.763997
time_total: 0.765413

When faced with a potential latency issue in web services, this is often one of the first commands I run several times from multiple clients because the results form this command help to get a quick sense of the layer that might be responsible for the latency issue.

#45 语言代码

2021-11-21

语言指包括语言的字符集以及有关的货币符号,日期格式等。为了区分不通的语言,人们设计了一些语言代码相关的标准来。
作为开发,只要涉及多语言,都一定会接触到语言代码。在各种不通的技术、语言框架中,这些代码并没有非常统一,比如简体中文,我见过 zh, zh_CN, zh-Hans, chs, zh_chs 几种不同的表示。

#44 如何推送消息给手机

2021-11-20

我没有做过移动端开发,我突然想到,如果我们要给手机端推送消息,会有些什么办法。
想来想去只有短信是最容易实现的,而且效果可以接受。

#42 最近工作的一次总结

2021-11-13

最近两周开发了一个功能,其实质就是做个数据统计,没啥好说的。
我是在没有产品设计的前提下开始工作,产品的设计反倒有一点依赖我所能提供的数据。
而且,和以往自己写功能逻辑不通的是,我这次只管提供数据和方案,业务逻辑的实现由别人来做。
工作过程中遇到了一些问题,这里做个总结(复盘)。

#41 我的开发机器

2021-10-24

Notebook
我的主力开发环境是大概 14 年 4 月在 DELL 官方 (dell.com) 买的一台 Inspiron 14R (5437) 笔记本。
PS: 这台笔记本原本是我老婆办公用, 用了将近五年之后, 于 2019 年 1 月在换了一台 小米 Air 13.3, 然后我就有笔记本了...到现在我也用了两年多了。

#39 DedeCMS 开始割韭菜了

2021-09-29

织梦官网放出上面这张公告:除了 “个人非盈利” 网站,其他没有拿到正式授权的织梦网站需要交 5800 授权费。

他们可能已经把清单列好了,律师团队也准备好了,正磨刀霍霍向着小站长们。

这可能就是中国特色开源协议的正确使用方式。

PS: 虽然我觉得 织梦CMS 是个垃圾,但我也不知道为啥,据说很多小站都是用的这个系统。

#37 GitHub 上那些徽章是怎么弄出来的?

2021-09-19

比如:Tornado 框架的 stars 数量:,再比如说这种:

外国人真是会玩,总有这样的好点子,在有限的环境(Markdown)中也能弄的丰富多彩的。

上面都是用的 shields.io 提供的服务,它内置了很多种类的图标,GitHub 关注数、Fork 数、Star 数、协议,Twitter 关注数、NPM 包大小、PyPI 下载数量等等,这些看他们官网提供的示例,照着用就是了。

还有一种,就是用户定制接口,格式:

  1. Static:URLPath https://img.shields.io/badge/<LABEL>-<MESSAGE>-<COLOR>
  2. Static:QueryString https://img.shields.io/static/v1?label=<LABEL>&message=<MESSAGE>&color=<COLOR>
  3. Endpoint https://img.shields.io/endpoint?url=<URL>&style<STYLE>
  4. Dynamic https://img.shields.io/badge/dynamic/json?url=<URL>&label=<LABEL>&query=<$.DATA.SUBDATA>&color=<COLOR>&prefix=<PREFIX>&suffix=<SUFFIX>

还有好多更加细致的规则,用来定制自己的图标,就不细说了,自己看文档就行。

其实我用的多的还是上面的第一种接口,因为它比较简单,只要把 label 和 message 修改就可以了。

https://img.shields.io/badge/site-markjour.com-brightgreen.svg?style=plastic&logo=nginx

https://img.shields.io/badge/-doing-green

参考资料与拓展阅读

#34 GitHub 上的一些备选方案

2021-09-13

cms

cms stars:>10000 在 GitHub 上搜索,再经过一些筛选,剩下以下项目:

  1. hugo shields.io, 静态网站生成
  2. wagtail shields.io, Django
  3. grav shields.io
  4. django-cms shields.io
  5. netlify-cms, NodeJS, MIT 静态网站生成,Git-based
  6. Keystone shields.io, NodeJS
  7. Strapi shields.io, NodeJS, headless CMS
  8. Ghost shields.io, NodeJS
  9. October shields.io, PHP, Laravel

forum

forum stars:>2000 在 GitHub 上搜索,再经过一些筛选,剩下以下项目:

web framwork / restful api (python)

  • Django shields.io
  • django-rest-framework shields.io
  • Flask shields.io
  • Sanic shields.io
  • Falcon shields.io
  • FastAPI shields.io
  • APIStar shields.io
  • Pyramid shields.io
  • Tornado shields.io
  • Twisted shields.io
  • daphne shields.io - A HTTP, HTTP2 and WebSocket protocol server for ASGI and ASGI-HTTP.
  • uvicorn shields.io - A lightning-fast ASGI server implementation, using uvloop and httptools.
  • bjoern shields.io - Asynchronous, very fast and written in C.
  • gunicorn shields.io - Pre-forked, ported from Ruby's Unicorn project.
  • uWSGI shields.io - A project aims at developing a full stack for building hosting services, written in C.
  • waitress shields.io - Multi-threaded, powers Pyramid.
  • werkzeug shields.io - A WSGI utility library for Python that powers Flask and can easily be embedded into your own projects.
  • uvloop shields.io - Ultra fast asyncio event loop.

data validation

  • Cerberus shields.io - A lightweight and extensible data validation library.
  • colander shields.io - Validating and deserializing data obtained via XML, JSON, an HTML form post.
  • jsonschema shields.io - An implementation of JSON Schema for Python.
  • schema shields.io - A library for validating Python data structures.
  • Schematics shields.io - Data Structure Validation.
  • valideer shields.io - Lightweight extensible data validation and adaptation library.
  • voluptuous shields.io - A Python data validation library.