-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(profiling): Move profiling types to types and cache to utils (#…
- Loading branch information
1 parent
50ead24
commit 9d8464b
Showing
8 changed files
with
162 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,4 @@ | ||
import type { Event } from '@sentry/types'; | ||
import { makeFifoCache } from '@sentry/utils'; | ||
|
||
/** | ||
* Creates a cache that evicts keys in fifo order | ||
* @param size {Number} | ||
*/ | ||
export function makeProfilingCache<Key extends string, Value extends Event>( | ||
size: number, | ||
): { | ||
get: (key: Key) => Value | undefined; | ||
add: (key: Key, value: Value) => void; | ||
delete: (key: Key) => boolean; | ||
clear: () => void; | ||
size: () => number; | ||
} { | ||
// Maintain a fifo queue of keys, we cannot rely on Object.keys as the browser may not support it. | ||
let evictionOrder: Key[] = []; | ||
let cache: Record<string, Value> = {}; | ||
|
||
return { | ||
add(key: Key, value: Value) { | ||
while (evictionOrder.length >= size) { | ||
// shift is O(n) but this is small size and only happens if we are | ||
// exceeding the cache size so it should be fine. | ||
const evictCandidate = evictionOrder.shift(); | ||
|
||
if (evictCandidate !== undefined) { | ||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete cache[evictCandidate]; | ||
} | ||
} | ||
|
||
// in case we have a collision, delete the old key. | ||
if (cache[key]) { | ||
this.delete(key); | ||
} | ||
|
||
evictionOrder.push(key); | ||
cache[key] = value; | ||
}, | ||
clear() { | ||
cache = {}; | ||
evictionOrder = []; | ||
}, | ||
get(key: Key): Value | undefined { | ||
return cache[key]; | ||
}, | ||
size() { | ||
return evictionOrder.length; | ||
}, | ||
// Delete cache key and return true if it existed, false otherwise. | ||
delete(key: Key): boolean { | ||
if (!cache[key]) { | ||
return false; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete cache[key]; | ||
|
||
for (let i = 0; i < evictionOrder.length; i++) { | ||
if (evictionOrder[i] === key) { | ||
evictionOrder.splice(i, 1); | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
}; | ||
} | ||
|
||
export const PROFILING_EVENT_CACHE = makeProfilingCache<string, Event>(20); | ||
export const PROFILING_EVENT_CACHE = makeFifoCache<string, Event>(20); |
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,76 @@ | ||
export type ThreadId = string; | ||
export type FrameId = number; | ||
export type StackId = number; | ||
|
||
export interface ThreadCpuSample { | ||
stack_id: StackId; | ||
thread_id: ThreadId; | ||
elapsed_since_start_ns: string; | ||
} | ||
|
||
export type ThreadCpuStack = FrameId[]; | ||
|
||
export type ThreadCpuFrame = { | ||
function: string; | ||
file?: string; | ||
line?: number; | ||
column?: number; | ||
}; | ||
|
||
export interface ThreadCpuProfile { | ||
samples: ThreadCpuSample[]; | ||
stacks: ThreadCpuStack[]; | ||
frames: ThreadCpuFrame[]; | ||
thread_metadata: Record<ThreadId, { name?: string; priority?: number }>; | ||
queue_metadata?: Record<string, { label: string }>; | ||
} | ||
|
||
export interface Profile { | ||
event_id: string; | ||
version: string; | ||
os: { | ||
name: string; | ||
version: string; | ||
build_number?: string; | ||
}; | ||
runtime: { | ||
name: string; | ||
version: string; | ||
}; | ||
device: { | ||
architecture: string; | ||
is_emulator: boolean; | ||
locale: string; | ||
manufacturer: string; | ||
model: string; | ||
}; | ||
timestamp: string; | ||
release: string; | ||
environment: string; | ||
platform: string; | ||
profile: ThreadCpuProfile; | ||
debug_meta?: { | ||
images: { | ||
debug_id: string; | ||
image_addr: string; | ||
code_file: string; | ||
type: string; | ||
image_size: number; | ||
image_vmaddr: string; | ||
}[]; | ||
}; | ||
transaction?: { | ||
name: string; | ||
id: string; | ||
trace_id: string; | ||
active_thread_id: string; | ||
}; | ||
transactions?: { | ||
name: string; | ||
id: string; | ||
trace_id: string; | ||
active_thread_id: string; | ||
relative_start_ns: string; | ||
relative_end_ns: string; | ||
}[]; | ||
} |
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,68 @@ | ||
/** | ||
* Creates a cache that evicts keys in fifo order | ||
* @param size {Number} | ||
*/ | ||
export function makeFifoCache<Key extends string, Value>( | ||
size: number, | ||
): { | ||
get: (key: Key) => Value | undefined; | ||
add: (key: Key, value: Value) => void; | ||
delete: (key: Key) => boolean; | ||
clear: () => void; | ||
size: () => number; | ||
} { | ||
// Maintain a fifo queue of keys, we cannot rely on Object.keys as the browser may not support it. | ||
let evictionOrder: Key[] = []; | ||
let cache: Record<string, Value> = {}; | ||
|
||
return { | ||
add(key: Key, value: Value) { | ||
while (evictionOrder.length >= size) { | ||
// shift is O(n) but this is small size and only happens if we are | ||
// exceeding the cache size so it should be fine. | ||
const evictCandidate = evictionOrder.shift(); | ||
|
||
if (evictCandidate !== undefined) { | ||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete cache[evictCandidate]; | ||
} | ||
} | ||
|
||
// in case we have a collision, delete the old key. | ||
if (cache[key]) { | ||
this.delete(key); | ||
} | ||
|
||
evictionOrder.push(key); | ||
cache[key] = value; | ||
}, | ||
clear() { | ||
cache = {}; | ||
evictionOrder = []; | ||
}, | ||
get(key: Key): Value | undefined { | ||
return cache[key]; | ||
}, | ||
size() { | ||
return evictionOrder.length; | ||
}, | ||
// Delete cache key and return true if it existed, false otherwise. | ||
delete(key: Key): boolean { | ||
if (!cache[key]) { | ||
return false; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete cache[key]; | ||
|
||
for (let i = 0; i < evictionOrder.length; i++) { | ||
if (evictionOrder[i] === key) { | ||
evictionOrder.splice(i, 1); | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
}; | ||
} |
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