西维蜀黍

【Golang】性能调优 - Golang 程序内存使用分析

Memory Profiling

默认情况下,统计的是当前内存使用数(字节数或对象数量),即使用 --inuse_objects 。

除此之外,我们还可以使用--alloc-space 来查看和分析当前截止每当前时刻已分配内存的总和(字节数或对象数量),比如每次为一个struct 分配一块新内存空间,都会计数在内(即使在分配之后,马上被GC回收了。而前者来说,就会考虑)。

Name Meaning
inuse_space amount of memory allocated and not released yet
inuse_objects amount of objects allocated and not released yet
alloc_space total amount of memory allocated (regardless of released)
alloc_objects total amount of objects allocated (regardless of released)
  ...


【Golang】性能调优 - PProf

PProf

pprof is a tool for visualization and analysis of profiling data.

pprof reads a collection of profiling samples in profile.proto format and generates reports to visualize and help analyze the data. It can generate both text and graphical reports (through the use of the dot visualization package).

profile.proto is a protocol buffer that describes a set of callstacks and symbolization information. A common usage is to represent a set of sampled callstacks from statistical profiling. The format is described on the proto/profile.proto file. For details on protocol buffers, see https://developers.google.com/protocol-buffers

Profiles can be read from a local file, or over http. Multiple profiles of the same type can be aggregated or compared.

If the profile samples contain machine addresses, pprof can symbolize them through the use of the native binutils tools (addr2line and nm).

想要进行性能优化,首先瞩目在 Go 自身提供的工具链来作为分析依据,本文将带你学习、使用 Go 后花园,涉及如下:

  • runtime/pprof:适用于对一次性运行程序(即你的应用是运行一段时间后就结束了)的运行数据进行分析
  • net/http/pprof:适用于对长时间运行程序(即你的应用是一直不断在运行的),监控程序会启动一个 HTTP Server 以暴露一个提供运行时采样数据的HTTP接口
  ...


【Linux】命令 - date

date是Linux系统里自带的一个系统命令,用来显示当前的系统时间,不过默认显示的结果里包括很多信息,特别是做为文件名输出时,不是很方便 好在date命令里包含格式化输出的选项。

$ date "+%Y-%m-%d"
2020-08-09
$ date "+%Y-%m-%d %H:%M:%S"
2020-08-09 17:51:17
  ...


【Python】Python3 的时间

当前时间

>>> import datetime
>>> datetime.datetime.today()       # 不包含时区信息的当前时间(Naive time)
datetime.datetime(2020, 8, 9, 17, 36, 6, 921616)

>>> datetime.datetime.now()         # 不包含时区信息的当前时间(当不传入 tz 参数时,和datetime.today()没有区别)
datetime.datetime(2019, 10, 19, 10, 23, 38, 117425)

>>> import time
>>> time.time()                     # 获取当前时间对应 Unix time 时间戳(Naive time),这意味着,即使修改电脑的时区,获取的这个值也不会变化(因为 Unix time 是绝对时间,即在同一个时刻,在不同时区获取到的 Unix time 也是完全一样的)
1596965822.509548                  

From https://docs.python.org/3/library/time.html#time.time

  ...


【Python】Framework - Logging

解决方案

打印日志最简单方式是使用 logging 模块。例如:

import logging

def main():
    # Configure the logging system
    logging.basicConfig(
        filename='app.log',
        level=logging.ERROR
    )

    # Variables (to make the calls that follow work)
    hostname = 'www.python.org'
    item = 'spam'
    filename = 'data.csv'
    mode = 'r'

    # Example logging calls (insert into your program)
    logging.critical('Host %s unknown', hostname)
    logging.error("Couldn't find %r", item)
    logging.warning('Feature is deprecated')
    logging.info('Opening file %r, mode=%r', filename, mode)
    logging.debug('Got here')

if __name__ == '__main__':
    main()

上面五个日志调用(critical(), error(), warning(), info(), debug())以降序方式表示不同的严重级别。 basicConfig() 的 level 参数是一个过滤器。 所有级别低于此级别的日志消息都会被忽略掉。 每个logging操作的参数是一个消息字符串,后面再跟一个或多个参数。 构造最终的日志消息的时候我们使用了%操作符来格式化消息字符串。

  ...


【hugo】使用

Installation

# macOS
$ brew install hugo

# Ubuntu
$ sudo apt-get install hugo

Operations

Refer to https://gohugo.io/getting-started/quick-start/.

创建新站点

# 创建新站点
$ hugo new site "$mysite"
  ...


【Python】Django - Django Shell

shell

$ python manage.py shell
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:
  ...


【Python】安装 Python

Ubuntu

Python2

$ sudo apt install python2

Check the Python version.

$ python2 -V
Python 2.7.17
  ...


【Python】升级Python版本

macOS

$ brew install python3
$ brew link python@3.8
  ...


【Golang】关键字 - defer

defer Keyword

Defer is used to ensure that a function call is performed later in a program’s execution, usually for purposes of cleanup. defer is often used where e.g. ensure and finally would be used in other languages.

The canonical examples are unlocking a mutex or closing a file.

  ...