@hazeljs/agent 0.2.0-beta.1 → 0.2.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 HazelJS Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Performance Benchmarks
3
+ * Measure agent runtime performance under various conditions
4
+ */
5
+
6
+ import { AgentRuntime } from '../src/runtime/agent.runtime';
7
+ import { Agent } from '../src/decorators/agent.decorator';
8
+ import { Tool } from '../src/decorators/tool.decorator';
9
+
10
+ interface BenchmarkResult {
11
+ name: string;
12
+ iterations: number;
13
+ totalTime: number;
14
+ averageTime: number;
15
+ minTime: number;
16
+ maxTime: number;
17
+ throughput: number;
18
+ }
19
+
20
+ class PerformanceBenchmark {
21
+ /**
22
+ * Run a benchmark
23
+ */
24
+ async runBenchmark(
25
+ name: string,
26
+ fn: () => Promise<void>,
27
+ iterations: number = 100
28
+ ): Promise<BenchmarkResult> {
29
+ const times: number[] = [];
30
+ const startTotal = Date.now();
31
+
32
+ for (let i = 0; i < iterations; i++) {
33
+ const start = Date.now();
34
+ await fn();
35
+ const duration = Date.now() - start;
36
+ times.push(duration);
37
+ }
38
+
39
+ const totalTime = Date.now() - startTotal;
40
+ const averageTime = times.reduce((a, b) => a + b, 0) / times.length;
41
+ const minTime = Math.min(...times);
42
+ const maxTime = Math.max(...times);
43
+ const throughput = (iterations / totalTime) * 1000;
44
+
45
+ return {
46
+ name,
47
+ iterations,
48
+ totalTime,
49
+ averageTime,
50
+ minTime,
51
+ maxTime,
52
+ throughput,
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Format benchmark results
58
+ */
59
+ formatResults(results: BenchmarkResult[]): string {
60
+ const lines: string[] = [
61
+ 'Performance Benchmark Results',
62
+ '============================',
63
+ '',
64
+ ];
65
+
66
+ for (const result of results) {
67
+ lines.push(`${result.name}:`);
68
+ lines.push(` Iterations: ${result.iterations}`);
69
+ lines.push(` Total Time: ${result.totalTime.toFixed(2)}ms`);
70
+ lines.push(` Average: ${result.averageTime.toFixed(2)}ms`);
71
+ lines.push(` Min: ${result.minTime.toFixed(2)}ms`);
72
+ lines.push(` Max: ${result.maxTime.toFixed(2)}ms`);
73
+ lines.push(` Throughput: ${result.throughput.toFixed(2)} ops/sec`);
74
+ lines.push('');
75
+ }
76
+
77
+ return lines.join('\n');
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Example benchmarks
83
+ */
84
+ async function runBenchmarks() {
85
+ const benchmark = new PerformanceBenchmark();
86
+ const results: BenchmarkResult[] = [];
87
+
88
+ // Benchmark 1: Agent Registration
89
+ @Agent({ name: 'bench-agent', description: 'Benchmark agent' })
90
+ class BenchAgent {
91
+ @Tool({ description: 'Benchmark tool', parameters: [] })
92
+ async benchTool() {
93
+ return { result: 'ok' };
94
+ }
95
+ }
96
+
97
+ const runtime = new AgentRuntime({
98
+ enableMetrics: true,
99
+ rateLimitPerMinute: 10000,
100
+ });
101
+
102
+ results.push(
103
+ await benchmark.runBenchmark(
104
+ 'Agent Registration',
105
+ async () => {
106
+ const agent = new BenchAgent();
107
+ runtime.registerAgentInstance('bench-agent', agent);
108
+ },
109
+ 1000
110
+ )
111
+ );
112
+
113
+ // Benchmark 2: Tool Registry Lookup
114
+ const agent = new BenchAgent();
115
+ runtime.registerAgentInstance('bench-agent', agent);
116
+
117
+ results.push(
118
+ await benchmark.runBenchmark(
119
+ 'Tool Registry Lookup',
120
+ async () => {
121
+ runtime.getAgentTools('bench-agent');
122
+ },
123
+ 10000
124
+ )
125
+ );
126
+
127
+ // Benchmark 3: Metrics Collection
128
+ results.push(
129
+ await benchmark.runBenchmark(
130
+ 'Metrics Collection',
131
+ async () => {
132
+ runtime.getMetrics();
133
+ },
134
+ 10000
135
+ )
136
+ );
137
+
138
+ // Benchmark 4: Health Check
139
+ results.push(
140
+ await benchmark.runBenchmark(
141
+ 'Health Check',
142
+ async () => {
143
+ await runtime.healthCheck();
144
+ },
145
+ 100
146
+ )
147
+ );
148
+
149
+ console.log(benchmark.formatResults(results));
150
+ }
151
+
152
+ // Run benchmarks if executed directly
153
+ if (require.main === module) {
154
+ runBenchmarks().catch(console.error);
155
+ }
156
+
157
+ export { PerformanceBenchmark, runBenchmarks };