Skip to content

Commit

Permalink
get heap statistics from deno
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Aug 6, 2024
1 parent 836ef3d commit 1a774eb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
53 changes: 52 additions & 1 deletion router-bridge/js-src/plan_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ declare let logger: {
error: typeof logFunction;
};

export interface MemoryUsage {
/** The number of bytes of the current Deno's process resident set size,
* which is the amount of memory occupied in main memory (RAM). */
rss: number;
/** The total size of the heap for V8, in bytes. */
heapTotal: number;
/** The amount of the heap used for V8, in bytes. */
heapUsed: number;
/** Memory, in bytes, associated with JavaScript objects outside of the
* JavaScript isolate. */
external: number;
}

enum PlannerEventKind {
UpdateSchema = "UpdateSchema",
Plan = "Plan",
Expand All @@ -32,6 +45,7 @@ enum PlannerEventKind {
Introspect = "Introspect",
Signature = "Signature",
Subgraphs = "Subgraphs",
GetHeapStatistics = "GetHeapStatistics",
}

interface UpdateSchemaEvent {
Expand Down Expand Up @@ -75,13 +89,19 @@ interface Exit {
kind: PlannerEventKind.Exit;
schemaId: number;
}

interface GetHeapStatisticsEvent {
kind: PlannerEventKind.GetHeapStatistics;
}

type PlannerEvent =
| UpdateSchemaEvent
| PlanEvent
| ApiSchemaEvent
| IntrospectEvent
| SignatureEvent
| SubgraphsEvent
| GetHeapStatisticsEvent
| Exit;
type PlannerEventWithId = {
id: string;
Expand All @@ -92,19 +112,27 @@ type WorkerResultWithId = {
id?: string;
payload: WorkerResult;
};

type WorkerResult =
| PlanResult
| ApiSchemaResult
| ExecutionResult
| Map<string, string>
| String;
| String
| MemoryUsageResult;
// Plan result
type PlanResult =
| ExecutionResultWithUsageReporting<QueryPlanResult>
| FatalError;
type ApiSchemaResult = {
schema: string;
};
type MemoryUsageResult = {
rss: number;
heapTotal: number;
heapUsed: number;
external: number;
};

type FatalError = {
errors: (JsError | WorkerGraphQLError)[];
Expand Down Expand Up @@ -290,6 +318,29 @@ async function run() {

await send({ id, payload: subgraphs });
break;
case PlannerEventKind.GetHeapStatistics:
logger.info(`received event: ${JSON.stringify(event)}`);

const mem = Deno.memoryUsage();
//const mem = memoryUsage();

logger.info(`got memoryUsage`);
const result: MemoryUsageResult = {
rss: mem.rss,
heapTotal: mem.heapTotal,
heapUsed: mem.heapUsed,
external: mem.external,
};
/*const result: MemoryUsageResult = {
rss: 1,
heapTotal: 2,
heapUsed: 3,
external: 4,
};*/
logger.info(`memoryUsage: ${JSON.stringify(result)}`);

await send({ id, payload: result });
break;
case PlannerEventKind.Exit:
planners.delete(event.schemaId);
if (planners.size == 0) {
Expand Down
31 changes: 31 additions & 0 deletions router-bridge/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ where
}
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct HeapStatistics {
pub rss: u64,
pub heap_total: u64,
pub heap_used: u64,
pub external: u64,
}

/// A Deno worker backed query Planner.
pub struct Planner<T>
Expand Down Expand Up @@ -586,6 +595,11 @@ where
})
.await
}

/// Get deno's heap statistics
pub async fn get_heap_statistics(&self) -> Result<HeapStatistics, crate::error::Error> {
self.worker.request(PlanCmd::GetHeapStatistics).await
}
}

impl<T> Drop for Planner<T>
Expand Down Expand Up @@ -647,7 +661,10 @@ enum PlanCmd {
Subgraphs { schema_id: u64 },
#[serde(rename_all = "camelCase")]
Exit { schema_id: u64 },
#[serde(rename_all = "camelCase")]
GetHeapStatistics,
}

#[derive(Serialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
/// Query planner configuration
Expand Down Expand Up @@ -1871,6 +1888,20 @@ ofType {
insta::assert_snapshot!(schema);
}
}

#[tokio::test]
async fn heap_statistics() {
let planner =
Planner::<serde_json::Value>::new(SCHEMA.to_string(), QueryPlannerConfig::default())
.await
.unwrap();

let _subgraphs = planner.subgraphs().await.unwrap();
let statistics = planner.get_heap_statistics().await.unwrap();

println!("statistics: {statistics:?}");
panic!()
}
}

#[cfg(test)]
Expand Down

0 comments on commit 1a774eb

Please sign in to comment.