西维蜀黍

【Golang】性能调优 - 分析测试用例

安装 PProf

$ go get -u github.com/google/pprof

编写测试用例

新建 data/d_test.go,文件内容:

package data

import "testing"

const url = "https://github.com/EDDYCJY"

func TestAdd(t *testing.T) {
    s := Add(url)
    if s == "" {
        t.Errorf("Test.Add error!")
    }
}

func BenchmarkAdd(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Add(url)
    }
}
  ...


【Golang】性能调优 - 分析耗时函数

Demo 0

$ go tool pprof http://localhost:6060/debug/pprof/profile\?seconds\=60

Fetching profile over HTTP from http://localhost:6060/debug/pprof/profile?seconds=60
Saved profile in /Users/weishi/pprof/pprof.samples.cpu.001.pb.gz
Type: cpu
Time: Aug 15, 2020 at 1:17pm (+08)
Duration: 1mins, Total samples = 2.95s ( 4.92%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top10
Showing nodes accounting for 2810ms, 95.25% of 2950ms total
Dropped 11 nodes (cum <= 14.75ms)
Showing top 10 nodes out of 50
      flat  flat%   sum%        cum   cum%
    1610ms 54.58% 54.58%     1760ms 59.66%  syscall.syscall
     330ms 11.19% 65.76%      330ms 11.19%  runtime.nanotime
     210ms  7.12% 72.88%      210ms  7.12%  runtime.usleep
     160ms  5.42% 78.31%      310ms 10.51%  runtime.notetsleep
     150ms  5.08% 83.39%      150ms  5.08%  runtime.exitsyscallfast
     150ms  5.08% 88.47%      150ms  5.08%  runtime.pthread_cond_timedwait_relative_np
     100ms  3.39% 91.86%      100ms  3.39%  runtime.memmove
      40ms  1.36% 93.22%       40ms  1.36%  runtime.pthread_cond_signal
      30ms  1.02% 94.24%       60ms  2.03%  runtime.findObject
      30ms  1.02% 95.25%       30ms  1.02%  runtime.spanOf
(pprof)

执行该命令后,需等待 60 秒(可调整 seconds 的值),pprof 会进行 CPU Profiling。结束后将默认进入 pprof 的交互式命令模式,可以对分析的结果进行查看或导出。

  ...


【MySQL】索引命中

like 关键字和索引命中

Example

Table Schema

mysql> show create table task2.core_user;
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                        |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| core_user | CREATE TABLE `core_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  ...
  PRIMARY KEY (`id`),
  UNIQUE KEY `core_user_name_2b52f76e_uniq` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

可以看到,对 idname 都有索引。

  ...


【Git】Duplicate a Git Repo

Before you can duplicate a repository and push to your new copy, or mirror, of the repository, you must create the new repository on GitHub. In these examples, exampleuser/new-repository or exampleuser/mirrored are the mirrors.

Mirroring a repository

  1. Open Terminal.

  2. Create a bare clone of the repository.

    $ git clone --bare https://github.com/exampleuser/old-repository.git
    
  3. Mirror-push to the new repository.

    $ cd old-repository.git
    $ git push --mirror https://github.com/exampleuser/new-repository.git
    
  4. Remove the temporary local repository you created earlier.

    $ cd ..
    $ rm -rf old-repository.git
    
  ...


【Golang】同步机制 - WaitGroup

Usage

To wait for multiple goroutines to finish, we can use a wait group.

This is the function we’ll run in every goroutine. Note that a WaitGroup must be passed to functions by pointer.

On return, notify the WaitGroup that we’re done.

package main

import (
    "fmt"
    "sync"
    "time"
)

func worker(id int, wg *sync.WaitGroup) {

    defer wg.Done()

    fmt.Printf("Worker %d starting\n", id)

    time.Sleep(time.Second)
    fmt.Printf("Worker %d done\n", id)
}

func main() {

    var wg sync.WaitGroup

    for i := 1; i <= 5; i++ {
        wg.Add(1)
        // Launch several goroutines and increment the WaitGroup counter for each.
        go worker(i, &wg)
    }
		
    // Block until the WaitGroup counter goes back to 0; all the workers notified they’re done.
    wg.Wait()
}
  ...


【Linux】命令 - rename

Installation

$ brew install rename
  ...


【Linux】命令 - rm

rm -f - force - 强行删除

Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error. The -f option overrides any previous -i options.

rm -d - directory - 删除文件夹

Attempt to remove directories as well as other types of files.

  ...


【Golang】依赖 - Checksum Mismatch

Context

执行 go mod tidy 报错

$ go mod tidy
go: verifying github.com/docker/docker@v1.13.1: checksum mismatch

Expect (go version go1.12.9 darwin/amd64):

$ go mod tidy -v
go: downloading github.com/panjf2000/ants v1.2.0
go: extracting github.com/panjf2000/ants v1.2.0

Actual (go version go1.13 darwin/amd64)

$ go mod tidy -v
verifying github.com/panjf2000/ants@v1.2.0: checksum mismatch
        downloaded: h1:Ufw4aDz9RqH1RVblx2W9L9Uv5vSX5apbX5+peR7LQ5k=
        sum.golang.org: h1:pMQ1/XpSgnWx3ro4y1xr/uA3jXUsTuAaU3Dm0JjwggE=

SECURITY ERROR
This download does NOT match the one reported by the checksum server.
The bits may have been replaced on the origin server, or an attacker may
have intercepted the download attempt.

For more information, see 'go help module-auth'.
  ...


【hugo】hugo 博客加载速度优化

Background

在将博客从 hexo 迁移到 hugo 下之后,顺便把使用的 CDN 从 GitHub 迁到了 AWS S3,再测了一下速度(via https://www.uptrends.com/tools/cdn-performance-check):

看起来速度还可以。

  ...


【AWS】使用 AWS Lambda Functions

Requirements and Restrictions on Lambda Functions

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html

Using AWS Lambda with CloudFront Lambda@Edge

https://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html

Lambda@Edge lets you run Node.js and Python Lambda functions to customize content that CloudFront delivers, executing the functions in AWS locations closer to the viewer. The functions run in response to CloudFront events, without provisioning or managing servers. You can use Lambda functions to change CloudFront requests and responses at the following points:

  • After CloudFront receives a request from a viewer (viewer request)
  • Before CloudFront forwards the request to the origin (origin request)
  • After CloudFront receives the response from the origin (origin response)
  • Before CloudFront forwards the response to the viewer (viewer response)

Customizing Content at the Edge with Lambda@Edge

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html

  ...