You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Execution Engine is a TypeScript library that enables tracingand visualization of code execution workflows in your
16
-
project. Gain insights into the dynamic sequence of code execution by
17
-
capturing detailed traces in JSON format, easily parseable into graphs.
15
+
Execution Engine is a TypeScript library that enables tracing, visualization, and optimization of code execution
16
+
workflows. It provides tools to trace execution flow, manage caching, and optimize repeated computations, offering
17
+
insights through structured execution traces in JSON format.
18
18
19
19
## Features ✨
20
20
21
+
Features are divided into two main parts:
22
+
23
+
### 1. Execution:
24
+
25
+
-**Trace:** Capture detailed execution flow for debugging and analysis.
26
+
-**Cache:** Prevent redundant function executions by storing results temporarily.
27
+
-**Memoize:** Optimize repeated computations within the same execution call.
28
+
29
+
### 2. Engine:
30
+
21
31
-**Tracing:** Trace the execution flow of code within your project.
22
32
-**Timing:** Capture the timing of each executed function.
23
33
-**Visualization:** Generate traces in JSON format for clear and insightful visualization.
@@ -39,7 +49,73 @@ yarn add execution-engine
39
49
40
50
## Usage 📚
41
51
42
-
### Example 1: Basic Usage
52
+
---
53
+
### 1. Execution:
54
+
55
+
#### Example 1: Memoization with `@memoize`
56
+
57
+
```typescript
58
+
import { memoize } from"execution-engine";
59
+
60
+
classCalculator {
61
+
@memoize() // Store the result of Fibonacci calculations
62
+
fibonacci(n:number):number {
63
+
if (n<=1) returnn;
64
+
returnthis.fibonacci(n-1) +this.fibonacci(n-2);
65
+
}
66
+
}
67
+
68
+
const calc =newCalculator();
69
+
console.log(calc.fibonacci(10)); // Calculates and stores result
70
+
console.log(calc.fibonacci(10)); // Reuses pending result, no recalculation
71
+
```
72
+
73
+
In this example, the `fibonacci` method is decorated with `@memoize`, meaning repeated calls with the same `n` will reuse the stored result instead of recalculating it.
74
+
75
+
76
+
#### Example 2: Caching Results with `@cache`
77
+
78
+
```typescript
79
+
import { cache } from"execution-engine";
80
+
81
+
classExampleService {
82
+
@cache({ ttl: 5000 }) // Store result for 5 seconds
83
+
async fetchData(id:number):Promise<string> {
84
+
console.log('Fetching data...');
85
+
return`Data for ${id}`;
86
+
}
87
+
}
88
+
89
+
const service =newExampleService();
90
+
console.log(awaitservice.fetchData(1)); // Fetches data and stores it
91
+
console.log(awaitservice.fetchData(1)); // Reuses stored result (within ttl)
92
+
```
93
+
94
+
The `fetchData` method is decorated with `@cache`, storing the result for 5 seconds. Subsequent calls within that time reuse the stored result.
95
+
96
+
#### Example 3: Tracing with `@trace`
97
+
98
+
```typescript
99
+
import { trace } from"execution-engine";
100
+
101
+
classMathOperations {
102
+
@trace(console.log) // Trace the execution and log using console.log
103
+
add(a:number, b:number):number {
104
+
returna+b;
105
+
}
106
+
}
107
+
108
+
const mathOps =newMathOperations();
109
+
console.log(mathOps.add(2, 3)); // Traces the 'add' method execution and logs it
110
+
```
111
+
112
+
In this example, `@trace`logs the method execution, including input parameters, output, timing, duration, start time, end time, and elapsed time.
- view the **complete code** in [examples/usage.ts](examples/usage.ts)
63
139
- inspect the **trace output** in [examples/usage.json](examples/usage.json).
64
-
- visualize the **trace graph** using the json-to-graph online tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage.json)
140
+
- visualize the **trace graph** using the json-to-graph online
141
+
tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage.json)
65
142
66
-
### Example 2: Usage with Decorators
143
+
####Example 2: Usage with Decorators
67
144
68
145
```typescript
69
146
import { engine, run } from"execution-engine";
@@ -94,9 +171,10 @@ You can:
94
171
95
172
- view the **complete code** in [examples/usage2.ts](examples/usage2.ts)
96
173
- inspect the **trace output** in [examples/usage2.json](examples/usage2.json)
97
-
- visualize the **trace graph** using the json-to-graph online tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage2.json)
174
+
- visualize the **trace graph** using the json-to-graph online
175
+
tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage2.json)
98
176
99
-
### Understanding the Trace 🧭
177
+
####Understanding the Trace 🧭
100
178
101
179
The `trace` object is an array containing **nodes** and **edges**. It has the following structure:
102
180
@@ -130,7 +208,7 @@ trace = [
130
208
];
131
209
```
132
210
133
-
## Examples 📘
211
+
####Examples 📘
134
212
135
213
For additional usage examples, please explore the __[/examples](examples)__ directory in this repository.
0 commit comments