MCPcopy Create free account
hub / github.com/PostHog/duckgres / TestProfilingOutputToFile

Function TestProfilingOutputToFile

server/profiling_test.go:16–79  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestProfilingOutputToFile(t *testing.T) {
17 db, err := sql.Open("duckdb", ":memory:")
18 if err != nil {
19 t.Fatalf("open duckdb: %v", err)
20 }
21 defer func() { _ = db.Close() }()
22
23 tmpFile := t.TempDir() + "/profiling.json"
24
25 for _, stmt := range []string{
26 "SET enable_profiling = 'json'",
27 "SET profiling_mode = 'detailed'",
28 "SET profiling_output = '" + tmpFile + "'",
29 } {
30 if _, err := db.Exec(stmt); err != nil {
31 t.Fatalf("%s: %v", stmt, err)
32 }
33 }
34
35 // Run a query that produces profiling output
36 if _, err := db.Exec("CREATE TABLE test_prof (id INT, name VARCHAR)"); err != nil {
37 t.Fatalf("create table: %v", err)
38 }
39 if _, err := db.Exec("INSERT INTO test_prof VALUES (1, 'alice'), (2, 'bob')"); err != nil {
40 t.Fatalf("insert: %v", err)
41 }
42 rows, err := db.Query("SELECT * FROM test_prof WHERE id > 0")
43 if err != nil {
44 t.Fatalf("select: %v", err)
45 }
46 _ = rows.Close()
47
48 // Read profiling output from file
49 data, err := os.ReadFile(tmpFile)
50 if err != nil {
51 t.Fatalf("read profiling file: %v", err)
52 }
53 if len(data) == 0 {
54 t.Fatal("profiling output file is empty")
55 }
56
57 // Parse and verify key fields
58 m, ok := observe.ParseProfilingOutput(string(data))
59 if !ok {
60 t.Fatalf("failed to parse profiling output: %s", data[:min(200, len(data))])
61 }
62 if m.Latency <= 0 {
63 t.Errorf("expected positive latency, got %f", m.Latency)
64 }
65 if m.RowsReturned != 2 {
66 t.Errorf("expected 2 rows_returned, got %d", m.RowsReturned)
67 }
68
69 // Verify it's valid JSON with expected fields
70 var raw map[string]interface{}
71 if err := json.Unmarshal(data, &raw); err != nil {
72 t.Fatalf("profiling output is not valid JSON: %v", err)
73 }

Callers

nothing calls this directly

Calls 5

ParseProfilingOutputFunction · 0.92
CloseMethod · 0.65
ExecMethod · 0.65
QueryMethod · 0.65
OpenMethod · 0.45

Tested by

no test coverage detected