This is a tool to generate test fixtures for the drain package.
()
| 21 | |
| 22 | // This is a tool to generate test fixtures for the drain package. |
| 23 | func main() { |
| 24 | // For each .txt file in the testdata directory, read the file and run DRAIN on it. |
| 25 | // The output should be written to a new file in the testdata directory with the same name but with the suffix .drain |
| 26 | |
| 27 | files, err := os.ReadDir("pkg/drain/testdata") |
| 28 | if err != nil { |
| 29 | log.Fatalf("failed to read testdata directory: %v", err) |
| 30 | } |
| 31 | |
| 32 | for _, file := range files { |
| 33 | if file.IsDir() { |
| 34 | continue |
| 35 | } |
| 36 | if !strings.HasSuffix(file.Name(), ".json") { |
| 37 | continue |
| 38 | } |
| 39 | linesJSON, err := os.ReadFile(filepath.Join("pkg/drain/testdata", file.Name())) |
| 40 | if err != nil { |
| 41 | log.Fatalf("failed to read file: %v", err) |
| 42 | } |
| 43 | var lines []string |
| 44 | err = json.Unmarshal(linesJSON, &lines) |
| 45 | if err != nil { |
| 46 | log.Fatalf("failed to unmarshal file: %v", err) |
| 47 | } |
| 48 | patternMapping := make(map[string]string) |
| 49 | drain := drain.New("test-tenant", drain.DefaultConfig()) |
| 50 | for _, line := range lines { |
| 51 | cluster := drain.Train(line) |
| 52 | if cluster == nil { |
| 53 | patternMapping[line] = "<nil> (possibly too many tokens)" |
| 54 | } else { |
| 55 | patternMapping[line] = cluster.String() |
| 56 | } |
| 57 | } |
| 58 | clusters := drain.Clusters() |
| 59 | testData := TestData{ |
| 60 | OriginalCount: len(lines), |
| 61 | FinalCount: len(clusters), |
| 62 | ReductionPct: 100 * float64(len(lines)-len(clusters)) / float64(len(lines)), |
| 63 | PatternMapping: patternMapping, |
| 64 | } |
| 65 | |
| 66 | fmt.Printf("finished %s\n reduction: %f%%\n", file.Name(), testData.ReductionPct) //nolint:forbidigo |
| 67 | |
| 68 | var buf bytes.Buffer |
| 69 | enc := json.NewEncoder(&buf) |
| 70 | enc.SetEscapeHTML(false) |
| 71 | enc.SetIndent("", " ") |
| 72 | if err := enc.Encode(testData); err != nil { |
| 73 | log.Fatalf("failed to marshal test data: %v", err) |
| 74 | } |
| 75 | err = os.WriteFile(filepath.Join("pkg/drain/testdata", strings.TrimSuffix(file.Name(), ".json")+".drain"), buf.Bytes(), 0o600) |
| 76 | if err != nil { |
| 77 | log.Fatalf("failed to write file: %v", err) |
| 78 | } |
| 79 | } |
| 80 | } |