txxxxc / isucon11-qualify

0 stars 0 forks source link

pproteinを入れる #29

Closed txxxxc closed 1 month ago

txxxxc commented 1 month ago

まじでわからんけど頑張ります

txxxxc commented 1 month ago

一旦これ読むか pprotein でボトルネックを探して ISUCON で優勝する

txxxxc commented 1 month ago

そもそもpprofがなにか分かってない performance profilingの略っぽい。

Flame Graphの読み方もめっちゃざっくり分かった image

横幅: 関数の実行時間の長さを表す 縦方向: 関数の呼び出し階層を表す 色: 特定の意味はない

txxxxc commented 1 month ago

ざっくりいうとpprof周りの設定をやってくれてるらしい

echov4.EnableDebugHandler(e)
txxxxc commented 1 month ago

[Go] pprofでのプロファイル(計測)のやり方を改めて整理した #Go - Qiita

HTTPサーバなど定常的に動いているプログラムに対して一定時間の間プロファイルしたい場合はnet/http/pprofを利用します。 importに_ net/http/pprofを追加し、対象の定常的なプログラムに

    go func() {
            log.Println(http.ListenAndServe("localhost:6060", nil))
    }()

追加することで、http://localhost:6060/debug/pprofというエンドポイントが開放されます

これ意味わからん過ぎる

txxxxc commented 1 month ago

initでエンドポイントを追加してるっぽいです

func init() {
    http.HandleFunc("/debug/pprof/", Index)
    http.HandleFunc("/debug/pprof/cmdline", Cmdline)
    http.HandleFunc("/debug/pprof/profile", Profile)
    http.HandleFunc("/debug/pprof/symbol", Symbol)
    http.HandleFunc("/debug/pprof/trace", Trace)
}
txxxxc commented 1 month ago

mysqlとnginxのログの設定を終わってるから一旦これで終わりっぽい?

txxxxc commented 1 month ago

一旦pproteinのweb uiを起動してみても良いかもしれん

txxxxc commented 1 month ago

Cloud Formationとかsystemdの例はあるけど、dockerはないから頑張る まあいうても本家のリポジトリにあるけど https://github.com/kaz/pprotein/blob/master/compose.yaml

txxxxc commented 1 month ago

一旦本家のDockerfileパクったけど、マルチステージビルドがわからんすぎる

txxxxc commented 1 month ago

Dockerのマルチステージビルドでimageを軽量化する

txxxxc commented 1 month ago

なかなかわかりやすかった マルチステージビルドの利用 | Docker ドキュメント

txxxxc commented 1 month ago

WORKDIRで存在しないディレクトリを指定したら、ディレクトリを作ってくれるらしい

The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn't exist, it will be created even if it's not used in any subsequent Dockerfile instruction.

txxxxc commented 1 month ago

一旦サーバーは起動できた ログファイルをhttpから取得できるようにしないといけない(めんどいかもしれん)

image
txxxxc commented 1 month ago

pprotein-agentがそれをやってくれるかもしれんから調べてみましょう

txxxxc commented 1 month ago

tailHandlerってやつでファイルを読んでresponseとして返すっていう動きをしてるっぽいです https://github.com/kaz/pprotein/blob/bc90ef9cd3e0df4deba81f367c177c5539b87def/internal/tail/tail.go#L61-L85

txxxxc commented 1 month ago

ファイルパスは以下 https://github.com/kaz/pprotein/blob/b3704745a714103ceb691e218777113d0304098a/integration/integration.go#L16-L20

var (
    httplogPath       = getEnvOrDefault("PPROTEIN_HTTPLOG", "/var/log/nginx/access.log")
    slowlogPath       = getEnvOrDefault("PPROTEIN_SLOWLOG", "/var/log/mysql/mysql-slow.log")
    gitRepositoryPath = getEnvOrDefault("PPROTEIN_GIT_REPOSITORY", ".")
)
txxxxc commented 1 month ago

ほなpproteinのagentも起動する必要がある?

txxxxc commented 1 month ago

pprotein-agent意味わからんすぎたけどなんとなく理解できた

  1. リクエストがあった時点でファイルを最後までSeek
  2. 30ほど待って差分を読み込んで分析 みたいな感じか https://github.com/kaz/pprotein/blob/bc90ef9cd3e0df4deba81f367c177c5539b87def/internal/tail/tail.go#L73-L94
txxxxc commented 1 month ago

pprotein本家の方はまだあんまり理解してないから一旦読んでみるか

txxxxc commented 1 month ago

まあでも一旦動くか試してみても良い

txxxxc commented 1 month ago

nginxのログはlstvに変えないと行けないっぽい

txxxxc commented 1 month ago

便利すぎる

e.GET("/*", echo.WrapHandler(http.FileServer(http.FS(fs))))

http.FileServerは、Goの標準ライブラリに含まれるHTTPハンドラで、指定されたディレクトリ内のファイルを提供するためのものです。このハンドラは、リクエストされたファイルをディレクトリから探し、見つかった場合はそのファイルを返します。例えば、静的なウェブサイトのファイルやリソースを提供するのに便利です。 具体的には、以下のように動作します: http.FileServer(http.FS(fs))は、fsで指定されたファイルシステムからファイルを取得し、HTTPリクエストに応じてそのファイルを返します。 e.GET("/*", ...)は、すべてのリクエストに対してこのファイルサーバーを適用し、リクエストされたパスに基づいて適切なファイルを提供します。 このようにして、アプリケーションは静的ファイルを簡単に配信できます。

txxxxc commented 1 month ago

普通にgo:embedを調べた

go:embedは、Go言語の機能で、ファイルやディレクトリをバイナリに埋め込むために使用されます。このディレクティブを使うことで、外部ファイルをプログラムの実行時に直接利用できるようになります。例えば、HTML、CSS、画像ファイルなどを埋め込むことができます。 上記の例では、distディレクトリ内のすべてのファイルがdistという変数に埋め込まれています。これにより、埋め込まれたファイルにアクセスするためのembed.FS型のファイルシステムが作成されます。

package main

import (
    "embed"
    "fmt"
    "net/http"
)

//go:embed index.html
var content embed.FS

func handler(w http.ResponseWriter, r *http.Request) {
    data, err := content.ReadFile("index.html")
    if err != nil {
        http.Error(w, "File not found", http.StatusNotFound)
        return
    }
    w.Write(data)
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Println("Server is running on :8080")
    http.ListenAndServe(":8080", nil)
}
txxxxc commented 1 month ago

pproteinがログファイルとかをどうやって取得しているかについてはなんとなくわかりました。

image

txxxxc commented 1 month ago

あとはこいつが何をやってるか調べる

echov4.EnableDebugHandler(e)
txxxxc commented 1 month ago

こいつが何をやっているかは分かった とりあえずこれらのところにファイルがあれば耐えそう

httplogPath       = getEnvOrDefault("PPROTEIN_HTTPLOG", "/var/log/nginx/access.log")
slowlogPath       = getEnvOrDefault("PPROTEIN_SLOWLOG", "/var/log/mysql/mysql-slow.log")
gitRepositoryPath = getEnvOrDefault("PPROTEIN_GIT_REPOSITORY", ".")

image

txxxxc commented 1 month ago

一旦導入DONEかなり便利かもしれない