forked from cweiske/jsonmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_analysis.php
More file actions
172 lines (142 loc) · 5.94 KB
/
memory_analysis.php
File metadata and controls
172 lines (142 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Memory usage analysis for JsonMapper ancestors
*
* This script demonstrates the memory impact of ancestor tracking
* and shows how optimization can help.
*/
require_once 'vendor/autoload.php';
echo "=== JsonMapper Ancestor Memory Analysis ===\n\n";
// Test 1: Memory usage without ancestors (baseline)
echo "1. Baseline memory usage (no ancestors):\n";
$jm = new JsonMapper();
$json = createLargeNestedJson(5, 50); // 5 levels deep, 50 chars per field
$target = new stdClass();
$memBefore = memory_get_usage(true);
$result = $jm->map($json, $target);
$memAfter = memory_get_usage(true);
echo " Memory used: " . formatBytes($memAfter - $memBefore) . "\n\n";
// Test 2: Memory usage with ancestor tracking
echo "2. Memory usage with ancestor tracking:\n";
$jm = new JsonMapper();
$memoryUsages = [];
$jm->classMap['DeepClass'] = function($class, $jvalue, $ancestors) use (&$memoryUsages) {
$memoryUsages[] = memory_get_usage(true);
return 'stdClass';
};
// Create target with typed property to trigger classMap
$target = new class {
/** @var DeepClass */
public $level1;
};
$json = createLargeNestedJsonWithClasses(5, 100);
$memBefore = memory_get_usage(true);
$result = $jm->map($json, $target);
$memAfter = memory_get_usage(true);
echo " Memory used: " . formatBytes($memAfter - $memBefore) . "\n";
echo " Peak memory during classMap calls: " . formatBytes(max($memoryUsages) - $memBefore) . "\n\n";
// Test 3: Memory optimization strategies
echo "3. Memory optimization strategies:\n\n";
echo " Strategy A: Depth Limiting\n";
$ancestors = createMockAncestors(10, 200); // 10 deep, 200 chars each
$memBefore = memory_get_usage(true);
$limited = limitAncestorDepth($ancestors, 3);
$memAfter = memory_get_usage(true);
echo " Original: " . count($ancestors) . " ancestors, " . formatBytes(calculateAncestorMemory($ancestors)) . "\n";
echo " Limited: " . count($limited) . " ancestors, " . formatBytes(calculateAncestorMemory($limited)) . "\n";
echo " Memory saved: " . formatBytes(calculateAncestorMemory($ancestors) - calculateAncestorMemory($limited)) . "\n\n";
echo " Strategy B: Field Filtering\n";
$ancestors = createMockAncestors(5, 500); // 5 levels, 500 chars of data each
$filtered = filterAncestorFields($ancestors, ['id', 'version', 'type']);
echo " Original: " . formatBytes(calculateAncestorMemory($ancestors)) . "\n";
echo " Filtered: " . formatBytes(calculateAncestorMemory($filtered)) . "\n";
echo " Memory saved: " . formatBytes(calculateAncestorMemory($ancestors) - calculateAncestorMemory($filtered)) . "\n\n";
echo " Strategy C: Combined Optimization\n";
$ancestors = createMockAncestors(15, 1000); // 15 deep, 1KB each
$optimized = filterAncestorFields(limitAncestorDepth($ancestors, 5), ['id', 'version']);
echo " Original: " . count($ancestors) . " ancestors, " . formatBytes(calculateAncestorMemory($ancestors)) . "\n";
echo " Optimized: " . count($optimized) . " ancestors, " . formatBytes(calculateAncestorMemory($optimized)) . "\n";
echo " Memory saved: " . formatBytes(calculateAncestorMemory($ancestors) - calculateAncestorMemory($optimized)) . "\n";
echo " Reduction: " . round((1 - calculateAncestorMemory($optimized) / calculateAncestorMemory($ancestors)) * 100, 1) . "%\n\n";
// Summary
echo "=== Summary ===\n";
echo "For deeply nested JSON structures, ancestor tracking can use significant memory.\n";
echo "Optimization strategies can reduce memory usage by 80-90% in extreme cases.\n";
echo "Most real-world JSON has < 10 levels, making the impact manageable.\n";
echo "Use MemoryOptimizedJsonMapper for memory-constrained environments.\n";
function createLargeNestedJson($depth, $stringSize) {
$data = str_repeat('x', $stringSize);
$json = new stdClass();
$json->data = $data;
$json->extra1 = $data;
$json->extra2 = $data;
$current = $json;
for ($i = 1; $i < $depth; $i++) {
$current->nested = new stdClass();
$current->nested->data = $data;
$current->nested->extra1 = $data;
$current->nested->extra2 = $data;
$current = $current->nested;
}
return $json;
}
function createLargeNestedJsonWithClasses($depth, $stringSize) {
$data = str_repeat('x', $stringSize);
$json = new stdClass();
$json->data = $data;
$json->extra1 = $data;
$json->extra2 = $data;
$current = $json;
for ($i = 1; $i < $depth; $i++) {
$current->level1 = new stdClass();
$current->level1->data = $data;
$current->level1->extra1 = $data;
$current->level1->extra2 = $data;
$current = $current->level1;
}
return $json;
}
function createMockAncestors($count, $dataSize) {
$ancestors = [];
$data = str_repeat('x', $dataSize);
for ($i = 0; $i < $count; $i++) {
$ancestor = new stdClass();
$ancestor->id = "id_$i";
$ancestor->version = "v$i";
$ancestor->type = "type_$i";
$ancestor->large_data = $data;
$ancestor->extra_field_1 = $data;
$ancestor->extra_field_2 = $data;
$ancestors[] = $ancestor;
}
return $ancestors;
}
function limitAncestorDepth($ancestors, $maxDepth) {
if (count($ancestors) <= $maxDepth) {
return $ancestors;
}
return array_slice($ancestors, -$maxDepth);
}
function filterAncestorFields($ancestors, $allowedFields) {
return array_map(function($ancestor) use ($allowedFields) {
$filtered = new stdClass();
foreach ($allowedFields as $field) {
if (isset($ancestor->$field)) {
$filtered->$field = $ancestor->$field;
}
}
return $filtered;
}, $ancestors);
}
function calculateAncestorMemory($ancestors) {
return strlen(serialize($ancestors));
}
function formatBytes($size, $precision = 2) {
$units = ['B', 'KB', 'MB', 'GB'];
$unit = 0;
while ($size >= 1024 && $unit < count($units) - 1) {
$size /= 1024;
$unit++;
}
return round($size, $precision) . ' ' . $units[$unit];
}