-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from taniabogatsch/profiling
Add profiling support
- Loading branch information
Showing
7 changed files
with
183 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package duckdb | ||
|
||
/* | ||
#include <duckdb.h> | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"database/sql" | ||
"unsafe" | ||
) | ||
|
||
// ProfilingInfo is a recursive type containing metrics for each node in DuckDB's query plan. | ||
// There are two types of nodes: the QUERY_ROOT and OPERATOR nodes. | ||
// The QUERY_ROOT refers exclusively to the top-level node; its metrics are measured over the entire query. | ||
// The OPERATOR nodes refer to the individual operators in the query plan. | ||
type ProfilingInfo struct { | ||
// Metrics contains all key-value pairs of the current node. | ||
// The key represents the name and corresponds to the measured value. | ||
Metrics map[string]string | ||
// Children contains all children of the node and their respective metrics. | ||
Children []ProfilingInfo | ||
} | ||
|
||
// GetProfilingInfo obtains all available metrics set by the current connection. | ||
func GetProfilingInfo(c *sql.Conn) (ProfilingInfo, error) { | ||
info := ProfilingInfo{} | ||
err := c.Raw(func(driverConn any) error { | ||
con := driverConn.(*conn) | ||
duckdbInfo := C.duckdb_get_profiling_info(con.duckdbCon) | ||
if duckdbInfo == nil { | ||
return getError(errProfilingInfoEmpty, nil) | ||
} | ||
|
||
// Recursive tree traversal. | ||
info.getMetrics(duckdbInfo) | ||
return nil | ||
}) | ||
return info, err | ||
} | ||
|
||
func (info *ProfilingInfo) getMetrics(duckdbInfo C.duckdb_profiling_info) { | ||
m := C.duckdb_profiling_info_get_metrics(duckdbInfo) | ||
count := C.duckdb_get_map_size(m) | ||
info.Metrics = make(map[string]string, count) | ||
|
||
for i := C.idx_t(0); i < count; i++ { | ||
key := C.duckdb_get_map_key(m, i) | ||
value := C.duckdb_get_map_value(m, i) | ||
|
||
cKey := C.duckdb_get_varchar(key) | ||
cValue := C.duckdb_get_varchar(value) | ||
keyStr := C.GoString(cKey) | ||
valueStr := C.GoString(cValue) | ||
|
||
info.Metrics[keyStr] = valueStr | ||
|
||
C.duckdb_destroy_value(&key) | ||
C.duckdb_destroy_value(&value) | ||
C.duckdb_free(unsafe.Pointer(cKey)) | ||
C.duckdb_free(unsafe.Pointer(cValue)) | ||
} | ||
C.duckdb_destroy_value(&m) | ||
|
||
childCount := C.duckdb_profiling_info_get_child_count(duckdbInfo) | ||
for i := C.idx_t(0); i < childCount; i++ { | ||
duckdbChildInfo := C.duckdb_profiling_info_get_child(duckdbInfo, i) | ||
childInfo := ProfilingInfo{} | ||
childInfo.getMetrics(duckdbChildInfo) | ||
info.Children = append(info.Children, childInfo) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package duckdb | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProfiling(t *testing.T) { | ||
t.Parallel() | ||
|
||
db, err := sql.Open("duckdb", "") | ||
require.NoError(t, err) | ||
con, err := db.Conn(context.Background()) | ||
require.NoError(t, err) | ||
|
||
_, err = con.ExecContext(context.Background(), `PRAGMA enable_profiling = 'no_output'`) | ||
require.NoError(t, err) | ||
_, err = con.ExecContext(context.Background(), `PRAGMA profiling_mode = 'detailed'`) | ||
require.NoError(t, err) | ||
res, err := con.QueryContext(context.Background(), `SELECT range AS i FROM range(100) ORDER BY i`) | ||
require.NoError(t, err) | ||
|
||
info, err := GetProfilingInfo(con) | ||
require.NoError(t, err) | ||
|
||
_, err = con.ExecContext(context.Background(), `PRAGMA disable_profiling`) | ||
require.NoError(t, err) | ||
require.NoError(t, res.Close()) | ||
require.NoError(t, con.Close()) | ||
require.NoError(t, db.Close()) | ||
|
||
// Verify the metrics. | ||
require.NotEmpty(t, info.Metrics, "metrics must not be empty") | ||
require.NotEmpty(t, info.Children, "children must not be empty") | ||
require.NotEmpty(t, info.Children[0].Metrics, "child metrics must not be empty") | ||
} | ||
|
||
func TestErrProfiling(t *testing.T) { | ||
t.Parallel() | ||
db, err := sql.Open("duckdb", "") | ||
require.NoError(t, err) | ||
con, err := db.Conn(context.Background()) | ||
require.NoError(t, err) | ||
|
||
_, err = GetProfilingInfo(con) | ||
testError(t, err, errProfilingInfoEmpty.Error()) | ||
require.NoError(t, con.Close()) | ||
require.NoError(t, db.Close()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters