-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
259 lines (218 loc) · 7.57 KB
/
Copy pathmain.go
File metadata and controls
259 lines (218 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"bufio"
"fmt"
"io/fs"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/bytedance/sonic"
)
// OptimizedResult はPhase4専用の最適化版Result構造体
// StatusCountsをmapではなく配列にすることでメモリアロケーションとアクセス速度を改善
type OptimizedResult struct {
FileName string
TotalCount int
StatusCounts [600]int // 固定配列でステータスコード0-599をカバー
}
// MinimalLogEntry は最小限のフィールドのみをパースする構造体
// Phase4では status フィールドのみが必要なため、他のフィールドをパースしない
type MinimalLogEntry struct {
Status int `json:"status"`
}
func main() {
startTime := time.Now()
logRoot, err := os.OpenRoot("./logs")
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening log directory: %v\n", err)
os.Exit(1)
}
defer logRoot.Close()
entries, err := fs.ReadDir(logRoot.FS(), ".")
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading log directory: %v\n", err)
os.Exit(1)
}
files := make([]string, 0, len(entries))
for _, entry := range entries {
name := entry.Name()
if !entry.IsDir() && strings.HasPrefix(name, "access_") && strings.HasSuffix(name, ".json") {
files = append(files, name)
}
}
numWorkers := runtime.NumCPU()
results := processFiles(logRoot, files, numWorkers)
elapsed := time.Since(startTime)
printResults(results, elapsed)
recordResult("phase4", elapsed)
}
// processFiles は最適化されたワーカープールパターンでファイルを処理します
func processFiles(root *os.Root, files []string, numWorkers int) []*OptimizedResult {
// ジョブチャネルは小さいバッファで十分
jobs := make(chan string, numWorkers)
// ワーカーごとの集計結果を受け取る(ファイル数ではなくワーカー数)
results := make(chan *OptimizedResult, numWorkers)
var wg sync.WaitGroup
// ワーカーを起動
for range numWorkers {
wg.Go(func() {
// ワーカーごとにローカル集計(複数ファイルを1つの結果にまとめる)
localResult := &OptimizedResult{
FileName: "worker-aggregate",
}
for filename := range jobs {
if err := processFileInto(root, filename, localResult); err != nil {
fmt.Fprintf(os.Stderr, "Error processing %s: %v\n", filename, err)
}
}
// ワーカーが処理した全ファイルの集計結果を送信
results <- localResult
})
}
// 不要なgoroutineを削除して直接ジョブを送信
for _, filename := range files {
jobs <- filename
}
close(jobs)
// 全てのワーカーが完了したら結果チャネルを閉じる
go func() {
wg.Wait()
close(results)
}()
// ワーカーごとの集計結果を収集(ファイル数ではなくワーカー数分)
resultList := make([]*OptimizedResult, 0, numWorkers)
for result := range results {
resultList = append(resultList, result)
}
return resultList
}
// processFileInto は1つのログファイルを解析して既存の結果に集計します
// ワーカーごとのローカル集計に使用され、Result作成のオーバーヘッドを削減
func processFileInto(root *os.Root, filename string, result *OptimizedResult) error {
file, err := root.Open(filename)
if err != nil {
return err
}
defer file.Close()
// 256KBのバッファでI/O効率を向上
bufferedReader := bufio.NewReaderSize(file, 256*1024)
// sonic JSONデコーダーを使用(標準ライブラリより2-5倍高速)
decoder := sonic.ConfigDefault.NewDecoder(bufferedReader)
// 最小フィールドのみパースしてパース時間とメモリを削減
// 必要なのは status のみなので、他の7フィールド(timestamp, method, path, etc.)は無視
var entry MinimalLogEntry
for decoder.More() {
if err := decoder.Decode(&entry); err != nil {
continue
}
result.TotalCount++
result.StatusCounts[entry.Status]++
}
return nil
}
// printResults は処理結果を表示します
func printResults(results []*OptimizedResult, elapsed time.Duration) {
totalRequests := 0
var totalStatusCounts [600]int
for _, result := range results {
totalRequests += result.TotalCount
for status := 0; status < 600; status++ {
if count := result.StatusCounts[status]; count > 0 {
totalStatusCounts[status] += count
}
}
}
fmt.Printf("\n=== 処理結果 ===\n")
fmt.Printf("処理時間: %.2f秒\n", elapsed.Seconds())
fmt.Printf("総リクエスト数: %s件\n", formatNumber(totalRequests))
fmt.Printf("\nステータスコード別:\n")
for status := 200; status <= 599; status += 100 {
for s := status; s < status+100; s++ {
if count := totalStatusCounts[s]; count > 0 {
percentage := float64(count) / float64(totalRequests) * 100
fmt.Printf(" %d: %s件 (%.2f%%)\n", s, formatNumber(count), percentage)
}
}
}
errorCount := 0
for status := 400; status < 600; status++ {
errorCount += totalStatusCounts[status]
}
errorRate := float64(errorCount) / float64(totalRequests) * 100
fmt.Printf("\nエラー率 (4xx, 5xx): %.2f%%\n", errorRate)
}
// formatNumber は数値を3桁カンマ区切りでフォーマットします
func formatNumber(n int) string {
s := fmt.Sprintf("%d", n)
result := ""
for i, c := range s {
if i > 0 && (len(s)-i)%3 == 0 {
result += ","
}
result += string(c)
}
return result
}
// recordResult は実行時間をsolutions/results.txtに記録します
func recordResult(phase string, elapsed time.Duration) {
// 既存の結果を読み込む(なければ空のマップ)
results := loadResults("./solutions/results.txt")
// 現在のフェーズの結果を更新(冪等操作)
results[phase] = elapsed.Seconds()
// ファイルに書き戻す(全体を上書き)
if err := saveResults("./solutions/results.txt", results); err != nil {
fmt.Fprintf(os.Stderr, "Warning: Failed to save results: %v\n", err)
}
}
// loadResults はresults.txtを読み込む(ファイルがなければ空のマップ)
func loadResults(path string) map[string]float64 {
results := make(map[string]float64)
data, err := os.ReadFile(path)
if err != nil {
return results // ファイルがなければ空
}
for _, line := range strings.Split(string(data), "\n") {
if line = strings.TrimSpace(line); line == "" {
continue
}
// "phase1=10.00" or "phase2=2.00 (phase1から5.00倍高速, 80.0%改善)" の形式をパース
parts := strings.Split(line, "=")
if len(parts) == 2 {
// スペースで分割して数値部分だけを取得
fields := strings.Fields(parts[1])
if len(fields) > 0 {
if val, err := strconv.ParseFloat(fields[0], 64); err == nil {
results[parts[0]] = val
}
}
}
}
return results
}
// saveResults は結果をresults.txtに保存(冪等)
func saveResults(path string, results map[string]float64) error {
var lines []string
// Phase 1の基準値を取得
baseline, hasBaseline := results["phase1"]
// 安定したソート順(phase1, phase2, phase3...)
for _, phase := range []string{"phase1", "phase2", "phase3", "phase4"} {
if val, ok := results[phase]; ok {
line := fmt.Sprintf("%s=%.2f", phase, val)
// Phase 1以外で基準値があれば改善率を追加
if phase != "phase1" && hasBaseline && baseline > 0 {
improvement := (baseline - val) / baseline * 100
speedup := baseline / val
line += fmt.Sprintf(" (phase1から%.2f倍高速, %.1f%%改善)", speedup, improvement)
}
lines = append(lines, line)
}
}
content := strings.Join(lines, "\n")
if len(content) > 0 {
content += "\n"
}
return os.WriteFile(path, []byte(content), 0644)
}