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
ユニットテスト並行化の導入と運用:Goでの事例紹介
Search
Shoki Hata
September 12, 2023
Programming
1
610
ユニットテスト並行化の導入と運用:Goでの事例紹介
テストパフォーマンス向上のためのテスト戦略 〜自動化の秘訣 Lunch LT〜 の登壇資料になります。
Shoki Hata
September 12, 2023
Tweet
Share
More Decks by Shoki Hata
See All by Shoki Hata
無理なく始めるGoでのユニットテストの並行化戦略
shohata
2
1.3k
Contribute to Hasura
shohata
0
530
Hasura Conference 2022 Shoki_Hata
shohata
0
190
最近話題のGraphQLサーバー、Hasuraを半年使ってみて
shohata
1
330
Other Decks in Programming
See All in Programming
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
390
Let's learn code review
riofujimon
2
560
Sheets API使ってみた
toshi0383
2
150
GitHub Copilotのススメ
marcy731
1
210
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
680
Deep Dive into React Stream/Serialize
mugi_uno
2
390
大規模UIKitベースアプリへのTCAの段階的導入/gradual-adoption-of-tca-in-a-large-scale-uikit-based-app
takehilo
2
200
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
200
Polars入門
daikikatsuragawa
1
160
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
850
使ってみよう Azure AI Document Intelligence
kosmosebi
2
350
VS Code をプロダクトにどう取り込むか
onomax
1
630
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
133
6.3k
Ruby is Unlike a Banana
tanoku
96
10k
The Brand Is Dead. Long Live the Brand.
mthomps
49
29k
How STYLIGHT went responsive
nonsquared
92
4.8k
Optimizing for Happiness
mojombo
370
69k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
275
13k
Building Effective Engineering Teams - LeadDev
addyosmani
31
1.9k
Six Lessons from altMBA
skipperchong
22
3k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
64
14k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
660
120k
Optimising Largest Contentful Paint
csswizardry
12
2.4k
Transcript
ユニットテスト並行化の 導入と運用:Goでの事例紹介 Shoki Hata テストパフォーマンス向上のためのテスト戦略 〜自動化の秘訣 Lunch LT〜 September 13
wed
Copyright Kanmu, Inc. All right reserved. 2 畠 翔紀 software
engineer at Kanmu, Inc. @sho-hata @sho_hata_ 自己紹介
Copyright Kanmu, Inc. All right reserved. 3 本発表の目的 ユニットテストの並行化を 無理なく導入・運用するための情報を提供すること
Copyright Kanmu, Inc. All right reserved. 4 おことわり ユニットテストの並行化を 無理なく導入・運用するための情報を提供すること
・Go言語でのユースケースをお伝えします ・本紹介で紹介するアプローチは、他言語でも 可能ですので適宜読み替えていただければと思います
Copyright Kanmu, Inc. All right reserved. プロダクトの紹介 5 1
Copyright Kanmu, Inc. All right reserved. Poolチーム紹介 6 1 ・PdM:1名
・デザイナー:1名 ・Bizdev(事業開発):1名 ・フロント/バックエンドエンジニア: 4名 ・SRE:1名
Copyright Kanmu, Inc. All right reserved. ユニットテスト並行化とは 2
Copyright Kanmu, Inc. All right reserved. 「個々のユニットテストを独立に実行できる構成に すること」 と本発表では定義します Goのユニットテスト並行化とは
2
Copyright Kanmu, Inc. All right reserved. 並列・並行という用語について 9 2 テキストを入れたり。テキストを入れたり。テキストを入れた
り。テキストを入れたり。テキストを入れたり。テキストを入 れたり。テキストを入れたり。テキストを入れたり。 ・並行:複数の処理を独立に実行できる構成のこと ・並列:複数の処理を同時に実行すること 参考: “Concurrency is not Parallelism”, Heroku’s Waza conference,Rob Pike(2012)
Copyright Kanmu, Inc. All right reserved. テストを並行化すると、どのようなメリットがある? 10 2 テキストを入れたり。テキストを入れたり。テキストを入れた
り。テキストを入れたり。テキストを入れたり。テキストを入 れたり。テキストを入れたり。テキストを入れたり。 ・テスト時間の短縮が期待できる ・テスト対象/テストコードが並列実行に対応している ことを担保できる ・システムリソースの効率的な利用ができる ・CI/CDパイプラインを最適化できる
Copyright Kanmu, Inc. All right reserved. どうやってGoのテストは並列実行される? 11 2 Goでは、テストコマンド実行時オプション
「-parallel」でサポートされている -parallel オプション パッケージ内のテストごとの 並列実行をサポート
Copyright Kanmu, Inc. All right reserved. Aパッケージ パッケージ内のテストの並列実行数を指定する-parallel オプション 12
2 TestX TestY Aパッケージ TestX TestY ※マルチコア環境を想定
Copyright Kanmu, Inc. All right reserved. -parallelは「t.Parallel」を呼んでいるテスト関数のみ対象 13 2 case3
case1 case2 テストコード テスト実行 ※マルチコア環境を想定
Copyright Kanmu, Inc. All right reserved. ユニットテスト並行化に伴う課題 3
Copyright Kanmu, Inc. All right reserved. 課題:並行化コードを記述するのが面倒 15 3 テスト対象に並行化マーカーを
チマチマ記述していかないといけない
Copyright Kanmu, Inc. All right reserved. 16 3 課題:データベース接続テストの並行化 id
name 1 “NIGHT FISHING” 2 “KikUUiki” 3 “DocumentaLy” 4 “GO TO THE FUTURE” 5 ???? album テーブル 並列実行 テストA テストB あるテストの変更が他のテストケースに影響を及ぼしてしまう! ※マルチコア環境を想定
Copyright Kanmu, Inc. All right reserved. 時間も人も有限!プロダクト改善真っ只中! 今はユニットテストの改善に大きなリソースを割けない... 課題をどう解決していくか 3
Copyright Kanmu, Inc. All right reserved. テストの並行化にそんなにリソースを割けない 3 テスト並行化のメリット ・テスト時間の短縮が期待できる
・テスト対象/テストコードが並列実行に対応している ことを担保できるetc…
Copyright Kanmu, Inc. All right reserved. テストの並行化にそんなにリソースを割けない 3 テスト並行化のメリット ・テスト時間の短縮が期待できる
・テスト対象/テストコードが並列実行に対応している ことを担保できるetc… (今は)なるべくプロダクトの成長に直結する施策に 開発リソースを割きたい! メリットはわかる(重要)けど....
Copyright Kanmu, Inc. All right reserved. ・並行化コードを記述するのが面倒! ・データベース接続テストの並行化 テスト並行化の導入・運用のアプローチ 3
人力ではなく、ツールで一気に テストを並行化する
Copyright Kanmu, Inc. All right reserved. Goのテストコードを一気に 並行化するツール「tparagen」 4
Copyright Kanmu, Inc. All right reserved. 22 4 tparagenの紹介 ・https://github.com/sho-hata/tparagen
・対象ディレクトリ配下のGoファイルのテスト関数 に t.Parallel(=並行化マーカー)を埋め込む ・ソースコードを静的解析。テスト関数が並行化 できる条件に一致していればASTをいじって埋め込む
Copyright Kanmu, Inc. All right reserved. 23 4 tparagenの紹介:動作
Copyright Kanmu, Inc. All right reserved. ・並行化コードを記述するのが面倒! ・テストコード上での並行化特有の挙動 ・データベース接続テストの並行化 データベース接続テスト並行化に対してのアプローチ
4 各テストケースごとに独立した データ操作環境にする
Copyright Kanmu, Inc. All right reserved. 事例紹介:Pool開発チーム
Copyright Kanmu, Inc. All right reserved. データベースと接続するテストの並行化には、 テストケースごとに独立した環境の 構築がポイント
Copyright Kanmu, Inc. All right reserved. 27 4 事例紹介:テストケースごとに独立した環境の構築 実現したいこと
・テスト終了後にデータ変更が残らない ・並列実行時に他のテストに影響を与えない 方針 ・テストケースごとにトランザクションを貼る ・テストケース終了後、ロールバック ・テスト対象がトランザクション処理をしている場合、 動作をモック化
Copyright Kanmu, Inc. All right reserved. 28 4 事例紹介:pgtxdbを利用したテスト環境の構築 https://github.com/achiku/pgtxdbを使う
以下の特徴をもつDBコネクションを生成する ・接続が開かれるとトランザクションを開始 ・接続が閉じるとロールバック ・トランザクションがモック化される ・BEGIN -> SAVEPOINT ・COMMIT -> 何もしない ・ROLLBACK -> ROLLBACK TO SAVEPOINT どうやるか?
Copyright Kanmu, Inc. All right reserved. 29 4 事例紹介:pgtxdbを利用したテスト環境 テストA
テストB トランザクションは互いに独立。並列実行時にも問題なし
Copyright Kanmu, Inc. All right reserved. まとめ ・テスト並行化は、開発サイクルの速度向上や並列実行時の動作 担 保が期待できる ・無理なく導入・運用するには、並行化コードの自動挿入ツールの導
入を検討する ・データベースと接続するテストを並行化するには、 テストケースごとに独立したデータ操作環境を整備する
Copyright Kanmu, Inc. All right reserved. - 無理なく始めるGoでのユニットテスト並行化 - カンムテックブログ
- LTではお伝えできなかった部分に踏み込んで、より詳細に説明しています - https://tech.kanmu.co.jp/entry/2023/06/02/172458 - 無理なく始めるGoでのユニットテスト並行化戦略 - Go Conference 2023でお話しした内容です - https://speakerdeck.com/shohata/go-conference-2023 参考資料
Copyright Kanmu, Inc. All right reserved.