Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
mockgenによるモック生成を高速化するツール bulkmockgenのご紹介 / Kyot...
Search
utagawa kiki
July 14, 2023
Programming
2
2.4k
mockgenによるモック生成を高速化するツール bulkmockgenのご紹介 / Kyoto.go #43
Kyoto.go #43
https://kyotogo.connpass.com/event/287778/
utagawa kiki
July 14, 2023
Tweet
Share
More Decks by utagawa kiki
See All by utagawa kiki
自動で //nolint を挿入する取り組み / Gopher's Gathering
utgwkk
1
940
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
2.2k
君たちはどうコードをレビューする (される) か / 大吉祥寺.pm
utgwkk
21
15k
Dive into gomock / Go Conference 2024
utgwkk
14
7.4k
Goでリフレクションする、その前に / Kansai.go #1
utgwkk
4
3.5k
Go製Webアプリケーションのエラーとの向き合い方大全、あるいはやっぱりスタックトレース欲しいやん / Kyoto.go #50
utgwkk
7
4.2k
ありがとう、create-react-app
utgwkk
4
11k
SPAでもデータをURLでシェアしたい / Kyoto.js 19
utgwkk
2
1.9k
prototype大全 / YAPC::Kyoto 2023
utgwkk
1
4.6k
Other Decks in Programming
See All in Programming
decksh - a little language for decks
ajstarks
4
21k
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
0
530
Dart 参戦!!静的型付き言語界の隠れた実力者
kno3a87
0
180
令和最新版手のひらコンピュータ
koba789
13
6.9k
変化を楽しむエンジニアリング ~ いままでとこれから ~
murajun1978
0
690
なぜ今、Terraformの本を書いたのか? - 著者陣に聞く!『Terraformではじめる実践IaC』登壇資料
fufuhu
4
530
実践 Dev Containers × Claude Code
touyu
1
160
The State of Fluid (2025)
s2b
0
110
なぜあなたのオブザーバビリティ導入は頓挫するのか
ryota_hnk
5
580
Gemini CLIの"強み"を知る! Gemini CLIとClaude Codeを比較してみた!
kotahisafuru
3
960
Claude Code と OpenAI o3 で メタデータ情報を作る
laket
0
110
AIに安心して任せるためにTypeScriptで一意な型を作ろう
arfes0e2b3c
0
340
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
223
9.9k
Scaling GitHub
holman
461
140k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.4k
Into the Great Unknown - MozCon
thekraken
40
2k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Bash Introduction
62gerente
614
210k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Automating Front-end Workflow
addyosmani
1370
200k
Transcript
mockgenによるモック生成を 高速化するツール bulkmockgenのご紹介 Kyoto.go #43 @utgwkk (うたがわきき)
自己紹介 @utgwkk (うたがわきき) 株式会社はてな Webアプリケーションエンジニア in 京都 最近はGoを書いて暮らしています
みなさん モックしていますか?
gomock (mockgen) https://github.com/uber/mock (最近 https://github.com/golang/mock がarchiveされた) mockgenでモックを生成してテストで使う
mockgenを使ったモック世界観 (1) // interfaceを定義して type UserStore interface { FindById(ctx context.Context,
id string) (*model.User, error) } // モックを生成する //go:generate mockgen -package mock_store -destination mock_store/user_store.go . UserStore
mockgenを使ったモック世界観 (2) // モックを注入する ctrl := gomock.NewController(t) m := mock_repo.NewMockUserStore(ctrl)
s := NewUserService(s) // モックが呼び出される方法を表明する m.EXPECT().FindById(gomock.Any(), "user"). Return(&model.User{Id: "user"}, nil) // モックを使うメソッドを呼び出してテストする ctx := context.Background() u, err := s.FindUserById(ctx, "user")
mockgen便利 モック生成を一手に引き受けてくれる 便利なmatcherがある (gomock.Any(), gomock.InAnyOrder(), …) 呼び出し方が不正だったらテストを落としてくれる
mockgenの課題 go generateが直列に実行されるので遅い reflect modeだと都度コンパイルされるので遅い
モック生成コマンドが多くなると遅い //go:generate mockgen -package mock_store -destination mock_store/a.go . StoreA //go:generate
mockgen -package mock_store -destination mock_store/b.go . StoreB //go:generate mockgen -package mock_store -destination mock_store/c.go . StoreC go generateによるコード生成は直列に実行される Proposal: cmd/go: parallel execution of //go:generate · Issue #20520 · golang/go
mockgenのreflect modeの仕組み上遅い モックするinterfaceの情報を得るためにGoのプログラムをコンパイルしている mockgenを実行したらコンパイルが走る!!
どんどん遅くなるgo generate 77.70s user 39.76s system 143% cpu 1:21.75 total
https://xkcd.com/303/
go:generate をまとめることはできるが //go:generate mockgen -package mock_store -destination mock_store/store.go . StoreA,StoreB,StoreC
人間がこの1行を編集しまくる必要がある? うまくコンフリクトを解消できる??
bulkmockgen https://github.com/utgwkk/bulkmockgen mockgenのコード生成を1回にまとめて高速化するツールbulkmockgenを作った - 私が 歌川です モック対象のinterfaceをスライスに列挙して一度にコード生成する 移行ツールもある (mockgen-to-bulkmockgen)
仕組み モック対象のinterfaceをスライスに列挙する 静的解析 (go/parser, go/ast) でinterfaceのリストを取得する スライスに渡したinterface名を結合してmockgenに渡す
デモ 大量のinterface定義に対するモック生成を一括で行う https://github.com/utgwkk/bulkmockgen/tree/main/benchmark/interfaces (カンペ: VSCodeを開いてください)
コード生成を速くして効率を上げることに成功 77.70s user 39.76s system 143% cpu 1:21.75 total (before)
52.18s user 19.93s system 209% cpu 34.397 total (after) 関わっているプロジェクトで47秒ほど高速化できた
課題 mockgenが生成するコード中のコメントがコンフリクトする!! // Code generated by MockGen. DO NOT EDIT.
// Source: example.com/test/repo (interfaces: IFoo,IBar,IBaz…)
workaround go generateしたあとにコメントを消す for go_file in `git grep --name-only '^//
Code generated by MockGen. DO NOT EDIT.' -- '*.go'`; do perl -i -nlpe '$_="" if m{// Source: example.com/test/repo}' $go_file gofmt -w $go_file done
まとめ mockgenによるコード生成をまとめるツールbulkmockgenをご紹介 複数のinterfaceのモックを一度に生成することでコード生成を高速化できた interface一覧を1行にまとめる必要がないので人間に優しい
参考 • mockgenのコード生成を1回にまとめて高速化するツールbulkmockgenを作った - 私が歌川です • gomockを完全に理解する • go generateに関するproposal
◦ Proposal: cmd/go: parallel execution of //go:generate · Issue #20520 · golang/go ◦ proposal: cmd/go: generate allow arguments to span multiple lines · Issue #46050 · golang/go
先行研究: gomockhandler Goで大量のモックをより統一的に管理し、もっと高速に生成したい!そうだ!! gomockhandlerを使おう!! | メルカリエンジニアリング mockgenコマンドを並列実行する go generateではなく独自CLIによるモック管理
なぜbulkmockgenを作ったのか go generateの仕組みに乗ったまま高速化できないか考えた 既存の仕組みからジャンプが少ないと導入しやすい モックしたいinterfaceをGoのコードとして列挙するのでrenameにも強い
構想 gomockhandlerとbulkmockgenを組み合わせることができると爆速でモックを生成でき るのでは??