@buoy-gg/benchmark 2.1.3 → 2.1.4-beta.1

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.
Files changed (34) hide show
  1. package/lib/commonjs/benchmarking/BenchmarkComparator.js +221 -1
  2. package/lib/commonjs/benchmarking/BenchmarkRecorder.js +497 -1
  3. package/lib/commonjs/benchmarking/BenchmarkStorage.js +235 -1
  4. package/lib/commonjs/benchmarking/index.js +83 -1
  5. package/lib/commonjs/benchmarking/types.js +13 -1
  6. package/lib/commonjs/components/BenchmarkCompareView.js +475 -1
  7. package/lib/commonjs/components/BenchmarkDetailView.js +346 -1
  8. package/lib/commonjs/components/BenchmarkModal.js +505 -1
  9. package/lib/commonjs/components/BenchmarkSessionCard.js +193 -1
  10. package/lib/commonjs/index.js +62 -1
  11. package/lib/commonjs/preset.js +86 -1
  12. package/lib/module/benchmarking/BenchmarkComparator.js +216 -1
  13. package/lib/module/benchmarking/BenchmarkRecorder.js +493 -1
  14. package/lib/module/benchmarking/BenchmarkStorage.js +227 -1
  15. package/lib/module/benchmarking/index.js +48 -1
  16. package/lib/module/benchmarking/types.js +13 -1
  17. package/lib/module/components/BenchmarkCompareView.js +469 -1
  18. package/lib/module/components/BenchmarkDetailView.js +340 -1
  19. package/lib/module/components/BenchmarkModal.js +499 -1
  20. package/lib/module/components/BenchmarkSessionCard.js +187 -1
  21. package/lib/module/index.js +39 -1
  22. package/lib/module/preset.js +81 -1
  23. package/lib/typescript/benchmarking/BenchmarkComparator.d.ts.map +1 -0
  24. package/lib/typescript/benchmarking/BenchmarkRecorder.d.ts.map +1 -0
  25. package/lib/typescript/benchmarking/BenchmarkStorage.d.ts.map +1 -0
  26. package/lib/typescript/benchmarking/index.d.ts.map +1 -0
  27. package/lib/typescript/benchmarking/types.d.ts.map +1 -0
  28. package/lib/typescript/components/BenchmarkCompareView.d.ts.map +1 -0
  29. package/lib/typescript/components/BenchmarkDetailView.d.ts.map +1 -0
  30. package/lib/typescript/components/BenchmarkModal.d.ts.map +1 -0
  31. package/lib/typescript/components/BenchmarkSessionCard.d.ts.map +1 -0
  32. package/lib/typescript/index.d.ts.map +1 -0
  33. package/lib/typescript/preset.d.ts.map +1 -0
  34. package/package.json +2 -2
@@ -1 +1,227 @@
1
- "use strict";const STORAGE_PREFIX="@buoy-gg/benchmark",INDEX_KEY=`${STORAGE_PREFIX}/index`,REPORT_KEY_PREFIX=`${STORAGE_PREFIX}/report/`;function getReportKey(t){return`${REPORT_KEY_PREFIX}${t}`}function extractMetadata(t){return{id:t.id,name:t.name,description:t.description,createdAt:t.createdAt,duration:t.duration,batchCount:t.stats.batchCount}}export class BenchmarkStorage{constructor(t){this.storage=t}async saveReport(t){const e=await this.loadIndex(),a=e.findIndex(e=>e.id===t.id);return a>=0?e[a]=extractMetadata(t):e.push(extractMetadata(t)),await Promise.all([this.storage.setItem(getReportKey(t.id),JSON.stringify(t)),this.storage.setItem(INDEX_KEY,JSON.stringify(e))]),extractMetadata(t)}async loadReport(t){try{const e=await this.storage.getItem(getReportKey(t));return e?JSON.parse(e):null}catch(e){return console.error(`[BenchmarkStorage] Error loading report ${t}:`,e),null}}async listReports(){return(await this.loadIndex()).sort((t,e)=>e.createdAt-t.createdAt)}async deleteReport(t){const e=await this.loadIndex(),a=e.findIndex(e=>e.id===t);return!(a<0||(e.splice(a,1),await Promise.all([this.storage.removeItem(getReportKey(t)),this.storage.setItem(INDEX_KEY,JSON.stringify(e))]),0))}async clearAll(){const t=await this.loadIndex();await Promise.all([...t.map(t=>this.storage.removeItem(getReportKey(t.id))),this.storage.removeItem(INDEX_KEY)])}async updateReport(t,e){const a=await this.loadReport(t);return!!a&&(void 0!==e.name&&(a.name=e.name),void 0!==e.description&&(a.description=e.description),await this.saveReport(a),!0)}async getMostRecent(){const t=await this.listReports();return 0===t.length?null:this.loadReport(t[0].id)}async getReportsByName(t){return(await this.loadIndex()).filter(e=>e.name===t).sort((t,e)=>e.createdAt-t.createdAt)}async loadIndex(){try{const t=await this.storage.getItem(INDEX_KEY);return t?JSON.parse(t):[]}catch(t){return console.error("[BenchmarkStorage] Error loading index:",t),[]}}}export function createAsyncStorageAdapter(){try{const t=require("@react-native-async-storage/async-storage").default;return{getItem:e=>t.getItem(e),setItem:(e,a)=>t.setItem(e,a),removeItem:e=>t.removeItem(e)}}catch{return console.warn("[BenchmarkStorage] @react-native-async-storage/async-storage not available. Provide a custom storage adapter."),null}}export function createMemoryStorageAdapter(){const t=new Map;return{getItem:async e=>t.get(e)??null,setItem:async(e,a)=>{t.set(e,a)},removeItem:async e=>{t.delete(e)}}}export default BenchmarkStorage;
1
+ /**
2
+ * BenchmarkStorage
3
+ *
4
+ * Handles persistence of benchmark reports to AsyncStorage.
5
+ * Provides methods to save, load, list, and delete benchmark reports.
6
+ *
7
+ * Storage keys:
8
+ * - @benchmark/index: Array of report metadata (id, name, createdAt)
9
+ * - @benchmark/report/{id}: Full report data
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+
14
+ "use strict";
15
+
16
+ /**
17
+ * Storage adapter interface - allows for different storage backends
18
+ */
19
+
20
+ /**
21
+ * Metadata for a stored benchmark (stored in index)
22
+ */
23
+
24
+ // Storage key prefix
25
+ const STORAGE_PREFIX = "@buoy-gg/benchmark";
26
+ const INDEX_KEY = `${STORAGE_PREFIX}/index`;
27
+ const REPORT_KEY_PREFIX = `${STORAGE_PREFIX}/report/`;
28
+
29
+ /**
30
+ * Get the storage key for a report
31
+ */
32
+ function getReportKey(id) {
33
+ return `${REPORT_KEY_PREFIX}${id}`;
34
+ }
35
+
36
+ /**
37
+ * Extract metadata from a full report
38
+ */
39
+ function extractMetadata(report) {
40
+ return {
41
+ id: report.id,
42
+ name: report.name,
43
+ description: report.description,
44
+ createdAt: report.createdAt,
45
+ duration: report.duration,
46
+ batchCount: report.stats.batchCount
47
+ };
48
+ }
49
+
50
+ /**
51
+ * BenchmarkStorage - Manages persistence of benchmark reports
52
+ */
53
+ export class BenchmarkStorage {
54
+ /**
55
+ * Create a new BenchmarkStorage instance
56
+ * @param storage - Storage adapter (e.g., AsyncStorage)
57
+ */
58
+ constructor(storage) {
59
+ this.storage = storage;
60
+ }
61
+
62
+ /**
63
+ * Save a benchmark report
64
+ * @returns The saved report's metadata
65
+ */
66
+ async saveReport(report) {
67
+ // Load current index
68
+ const index = await this.loadIndex();
69
+
70
+ // Check if report already exists
71
+ const existingIndex = index.findIndex(m => m.id === report.id);
72
+ if (existingIndex >= 0) {
73
+ // Update existing entry
74
+ index[existingIndex] = extractMetadata(report);
75
+ } else {
76
+ // Add new entry
77
+ index.push(extractMetadata(report));
78
+ }
79
+
80
+ // Save report and update index
81
+ await Promise.all([this.storage.setItem(getReportKey(report.id), JSON.stringify(report)), this.storage.setItem(INDEX_KEY, JSON.stringify(index))]);
82
+ return extractMetadata(report);
83
+ }
84
+
85
+ /**
86
+ * Load a benchmark report by ID
87
+ * @returns The report, or null if not found
88
+ */
89
+ async loadReport(id) {
90
+ try {
91
+ const data = await this.storage.getItem(getReportKey(id));
92
+ if (!data) return null;
93
+ const report = JSON.parse(data);
94
+ return report;
95
+ } catch (error) {
96
+ console.error(`[BenchmarkStorage] Error loading report ${id}:`, error);
97
+ return null;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Load all benchmark metadata (for listing)
103
+ */
104
+ async listReports() {
105
+ const index = await this.loadIndex();
106
+ // Sort by createdAt descending (newest first)
107
+ return index.sort((a, b) => b.createdAt - a.createdAt);
108
+ }
109
+
110
+ /**
111
+ * Delete a benchmark report
112
+ * @returns true if deleted, false if not found
113
+ */
114
+ async deleteReport(id) {
115
+ // Load current index
116
+ const index = await this.loadIndex();
117
+
118
+ // Find and remove from index
119
+ const existingIndex = index.findIndex(m => m.id === id);
120
+ if (existingIndex < 0) {
121
+ return false;
122
+ }
123
+ index.splice(existingIndex, 1);
124
+
125
+ // Remove report and update index
126
+ await Promise.all([this.storage.removeItem(getReportKey(id)), this.storage.setItem(INDEX_KEY, JSON.stringify(index))]);
127
+ return true;
128
+ }
129
+
130
+ /**
131
+ * Delete all benchmark reports
132
+ */
133
+ async clearAll() {
134
+ const index = await this.loadIndex();
135
+
136
+ // Delete all reports
137
+ await Promise.all([...index.map(m => this.storage.removeItem(getReportKey(m.id))), this.storage.removeItem(INDEX_KEY)]);
138
+ }
139
+
140
+ /**
141
+ * Update a benchmark report's name and/or description
142
+ * @returns true if updated, false if not found
143
+ */
144
+ async updateReport(id, updates) {
145
+ // Load the report
146
+ const report = await this.loadReport(id);
147
+ if (!report) return false;
148
+
149
+ // Apply updates
150
+ if (updates.name !== undefined) {
151
+ report.name = updates.name;
152
+ }
153
+ if (updates.description !== undefined) {
154
+ report.description = updates.description;
155
+ }
156
+
157
+ // Save updated report (this also updates the index)
158
+ await this.saveReport(report);
159
+ return true;
160
+ }
161
+
162
+ /**
163
+ * Get the most recent report
164
+ */
165
+ async getMostRecent() {
166
+ const reports = await this.listReports();
167
+ if (reports.length === 0) return null;
168
+ return this.loadReport(reports[0].id);
169
+ }
170
+
171
+ /**
172
+ * Get reports by name (for comparing multiple runs of same benchmark)
173
+ */
174
+ async getReportsByName(name) {
175
+ const index = await this.loadIndex();
176
+ return index.filter(m => m.name === name).sort((a, b) => b.createdAt - a.createdAt);
177
+ }
178
+
179
+ /**
180
+ * Load the index of all benchmarks
181
+ */
182
+ async loadIndex() {
183
+ try {
184
+ const data = await this.storage.getItem(INDEX_KEY);
185
+ if (!data) return [];
186
+ return JSON.parse(data);
187
+ } catch (error) {
188
+ console.error("[BenchmarkStorage] Error loading index:", error);
189
+ return [];
190
+ }
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Create a storage instance using AsyncStorage
196
+ */
197
+ export function createAsyncStorageAdapter() {
198
+ try {
199
+ // Dynamic import to avoid hard dependency on AsyncStorage
200
+ const AsyncStorage = require("@react-native-async-storage/async-storage").default;
201
+ return {
202
+ getItem: key => AsyncStorage.getItem(key),
203
+ setItem: (key, value) => AsyncStorage.setItem(key, value),
204
+ removeItem: key => AsyncStorage.removeItem(key)
205
+ };
206
+ } catch {
207
+ console.warn("[BenchmarkStorage] @react-native-async-storage/async-storage not available. " + "Provide a custom storage adapter.");
208
+ return null;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * In-memory storage adapter (for testing or when AsyncStorage is not available)
214
+ */
215
+ export function createMemoryStorageAdapter() {
216
+ const store = new Map();
217
+ return {
218
+ getItem: async key => store.get(key) ?? null,
219
+ setItem: async (key, value) => {
220
+ store.set(key, value);
221
+ },
222
+ removeItem: async key => {
223
+ store.delete(key);
224
+ }
225
+ };
226
+ }
227
+ export default BenchmarkStorage;
@@ -1 +1,48 @@
1
- "use strict";export{BenchmarkRecorder,benchmarkRecorder}from"./BenchmarkRecorder";export{BenchmarkStorage,createAsyncStorageAdapter,createMemoryStorageAdapter}from"./BenchmarkStorage";export{BenchmarkComparator}from"./BenchmarkComparator";
1
+ /**
2
+ * @buoy-gg/benchmark/benchmarking
3
+ *
4
+ * Performance benchmarking system for measuring and comparing
5
+ * highlight updates performance. Uses the W3C Performance API
6
+ * for accurate timing measurements.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import {
11
+ * benchmarkRecorder,
12
+ * BenchmarkStorage,
13
+ * BenchmarkComparator,
14
+ * createAsyncStorageAdapter,
15
+ * } from '@buoy-gg/benchmark';
16
+ *
17
+ * // Start recording
18
+ * benchmarkRecorder.startSession({ name: 'MyBenchmark' });
19
+ *
20
+ * // ... perform operations ...
21
+ *
22
+ * // Stop and get report
23
+ * const report = benchmarkRecorder.stopSession();
24
+ *
25
+ * // Save report
26
+ * const storage = new BenchmarkStorage(createAsyncStorageAdapter()!);
27
+ * await storage.saveReport(report);
28
+ *
29
+ * // Compare with previous
30
+ * const previous = await storage.getMostRecent();
31
+ * if (previous) {
32
+ * BenchmarkComparator.quickCompare(previous, report);
33
+ * }
34
+ * ```
35
+ *
36
+ * @packageDocumentation
37
+ */
38
+
39
+ "use strict";
40
+
41
+ // Types
42
+ // BenchmarkRecorder
43
+ export { BenchmarkRecorder, benchmarkRecorder } from "./BenchmarkRecorder";
44
+
45
+ // BenchmarkStorage
46
+ export { BenchmarkStorage, createAsyncStorageAdapter, createMemoryStorageAdapter } from "./BenchmarkStorage";
47
+ // BenchmarkComparator
48
+ export { BenchmarkComparator } from "./BenchmarkComparator";
@@ -1 +1,13 @@
1
- "use strict";
1
+ /**
2
+ * Benchmarking Types
3
+ *
4
+ * Core type definitions for the performance benchmarking system.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+
9
+ "use strict";
10
+
11
+ /**
12
+ * High-resolution timestamp in milliseconds
13
+ */