fork download
  1. <?php
  2.  
  3. // =================================================================
  4. // CONFIGURACIÓN DEL BENCHMARK
  5. // =================================================================
  6. define('NUM_ELEMENTOS', 199);
  7. define('NUM_BUSQUEDAS', 500000); // Iteraciones para probar la velocidad de acceso
  8.  
  9. echo "<h1>Benchmark Final y Completo para Archmage</h1>";
  10. echo "<p>Simulando la estructura de datos real, incluyendo la lista completa de habilidades.</p>";
  11.  
  12. // --- Función para generar los datos de prueba ---
  13. function generarDatos() {
  14. $datos = [];
  15.  
  16. // --- Listas de datos para generar variedad ---
  17. $razas = ['elemental', 'humano', 'demonio', 'no-muerto', 'bestia', 'angel'];
  18. $tipos_ataque_normal = ['magic', 'melee', 'poison', 'cold'];
  19. $tipos_ataque_extra = ['ranged lighting', 'fire breath', 'holy strike', 'psychic blast'];
  20.  
  21. // ✨ LISTA COMPLETA DE HABILIDADES EXTRAÍDA DEL HTML
  22. $lista_habilidades_posibles = [
  23. 'FLYING', 'ADDITIONAL STRIKE', 'SWIFT', 'LARGE SHIELD', 'PIKE', 'REGENERATION',
  24. 'ENDURANCE', 'SIEGE', 'BEAUTY', 'STEAL LIFE', 'MARKSMANSHIP', 'SCALES', 'FEAR',
  25. 'HEALING', 'CHARM', 'BURSTING FLAME', 'CLUMSINESS', 'ACCURACY', 'RECRUIT SPEED',
  26. 'WEAKNESS TO', 'INITIATIVE', 'ATTACK', 'COUNTERATTACK', 'HITPOINT', 'ATTACKTYPE',
  27. 'PREVENT DAMAGE', 'DESTROY', 'RESURRECT', 'SPELL IMMUNITY', 'SLEEP', 'ATTDEF AGAINST',
  28. 'ATTACK RESISTANCE', 'SPELL RESISTANCE', 'DAMAGE SELF'
  29. ];
  30.  
  31. $tipos_resistencia_ataque = [
  32. 'Missile', 'Magic', 'Cold', 'Fire', 'Melee', 'Paralyze',
  33. 'Poison', 'Ranged', 'Psychic', 'Breath', 'Lightning', 'Holy'
  34. ];
  35.  
  36.  
  37. for ($i = 1; $i <= NUM_ELEMENTOS; $i++) {
  38. $id = 1000 + $i;
  39.  
  40. // --- Asignación de habilidades (de 1 a 3 por unidad) ---
  41. $habilidades_asignadas = [];
  42. $num_habilidades_a_asignar = rand(1, 3);
  43. $keys_habilidades_aleatorias = array_rand($lista_habilidades_posibles, $num_habilidades_a_asignar);
  44. if (is_array($keys_habilidades_aleatorias)) {
  45. foreach ($keys_habilidades_aleatorias as $key) {
  46. $habilidades_asignadas[] = $lista_habilidades_posibles[$key];
  47. }
  48. } else {
  49. $habilidades_asignadas[] = $lista_habilidades_posibles[$keys_habilidades_aleatorias];
  50. }
  51.  
  52. // --- Creación de resistencias a ataques ---
  53. $resistencias_ataque = [];
  54. foreach ($tipos_resistencia_ataque as $tipo) {
  55. $resistencias_ataque[$tipo] = rand(0, 100);
  56. }
  57.  
  58. $datos[$id] = [
  59. 'id' => $id,
  60. 'nombre' => "Unidad de prueba #$i",
  61. 'raza' => $razas[array_rand($razas)],
  62. 'poder' => rand(20000, 50000),
  63. 'hp' => rand(50000, 80000),
  64. 'imagen_url' => "https://w...content-available-to-author-only...d.cl/i/units/" . rand(1, 200) . ".jpg",
  65.  
  66. 'poder_ataque' => rand(80000, 120000),
  67. 'tipo_ataque' => $tipos_ataque_normal[array_rand($tipos_ataque_normal)],
  68. 'poder_ataque_extra' => rand(250000, 350000),
  69. 'tipo_ataque_extra' => $tipos_ataque_extra[array_rand($tipos_ataque_extra)],
  70.  
  71. 'iniciativa' => rand(1, 5),
  72. 'iniciativa_extra' => rand(1, 5),
  73. 'contra_ataque' => rand(6000, 8000),
  74.  
  75. 'mant_oro' => rand(0, 50),
  76. 'mant_gente' => rand(0, 10),
  77. 'mant_mana' => rand(80, 150),
  78.  
  79. 'resistencia_ataques' => $resistencias_ataque,
  80.  
  81. 'res_hechizo1' => rand(0, 100),
  82. 'res_hechizo2' => rand(0, 100),
  83. 'res_hechizo3' => rand(0, 100),
  84. 'res_hechizo4' => rand(0, 100),
  85. 'res_hechizo5' => rand(0, 100),
  86.  
  87. // ✨ CAMPO DE HABILIDADES ACTUALIZADO
  88. 'habilidades' => $habilidades_asignadas, // Ahora es un array con 1 a 3 habilidades
  89.  
  90. 'desc_es' => "Descripción en español para la unidad $i.",
  91. 'desc_en' => "English description for unit $i."
  92. ];
  93. }
  94. return $datos;
  95. }
  96.  
  97. // --- Funciones auxiliares ---
  98. function format_mem($bytes) { return number_format($bytes / 1024, 2) . ' KB'; }
  99. function format_time($start, $end) { return number_format(($end - $start) * 1000, 2) . ' ms'; }
  100.  
  101. $datos_originales = generarDatos();
  102.  
  103. // =================================================================
  104. // PARTE 1: PRUEBAS EN MEMORIA (ARRAYS vs OBJETOS)
  105. // =================================================================
  106. echo "<hr><h2>Pruebas de Rendimiento en Memoria</h2>";
  107.  
  108. // Test 1.1: Array de Arrays
  109. $start_mem = memory_get_usage();
  110. $start_time = microtime(true);
  111. $array_de_arrays = $datos_originales;
  112. $end_time = microtime(true);
  113. $end_mem = memory_get_usage();
  114. echo "<h3>1. Array de Arrays (Asociativos)</h3>";
  115. echo "<strong>Creación:</strong> " . format_time($start_time, $end_time) . "<br>";
  116. echo "<strong>Uso de Memoria:</strong> " . format_mem($end_mem - $start_mem) . "<br>";
  117. $start_time = microtime(true);
  118. for ($i=0; $i < NUM_BUSQUEDAS; $i++) { $elemento = $array_de_arrays[1000 + rand(1, NUM_ELEMENTOS)]; }
  119. $end_time = microtime(true);
  120. echo "<strong>Acceso aleatorio (" . number_format(NUM_BUSQUEDAS) . " veces):</strong> " . format_time($start_time, $end_time) . "<br>";
  121.  
  122. // Test 1.2: Array de Objetos
  123. $start_mem = memory_get_usage();
  124. $start_time = microtime(true);
  125. $array_de_objetos = [];
  126. foreach ($datos_originales as $id => $dato) { $array_de_objetos[$id] = (object)$dato; }
  127. $end_time = microtime(true);
  128. $end_mem = memory_get_usage();
  129. echo "<h3>2. Array de Objetos (stdClass)</h3>";
  130. echo "<strong>Creación:</strong> " . format_time($start_time, $end_time) . "<br>";
  131. echo "<strong>Uso de Memoria:</strong> " . format_mem($end_mem - $start_mem) . "<br>";
  132. $start_time = microtime(true);
  133. for ($i=0; $i < NUM_BUSQUEDAS; $i++) { $elemento = $array_de_objetos[1000 + rand(1, NUM_ELEMENTOS)]; }
  134. $end_time = microtime(true);
  135. echo "<strong>Acceso aleatorio (" . number_format(NUM_BUSQUEDAS) . " veces):</strong> " . format_time($start_time, $end_time) . "<br>";
  136.  
  137. // =================================================================
  138. // PARTE 2: PRUEBAS DE GUARDADO Y LECTURA (JSON vs SERIALIZE)
  139. // =================================================================
  140. echo "<hr><h2>Pruebas de Guardado y Lectura en Disco</h2>";
  141.  
  142. // Test 2.1: JSON
  143. $start_time = microtime(true);
  144. $json_data = json_encode($array_de_arrays);
  145. file_put_contents('datos.json', $json_data);
  146. $end_time = microtime(true);
  147. echo "<h3>1. JSON (json_encode / json_decode)</h3>";
  148. echo "<strong>Tiempo de guardado:</strong> " . format_time($start_time, $end_time) . "<br>";
  149. echo "<strong>Tamaño del archivo:</strong> " . format_mem(filesize('datos.json')) . "<br>";
  150. $start_time = microtime(true);
  151. $json_leido = file_get_contents('datos.json');
  152. $datos_desde_json = json_decode($json_leido, true);
  153. $end_time = microtime(true);
  154. echo "<strong>Tiempo de lectura:</strong> " . format_time($start_time, $end_time) . "<br>";
  155. unlink('datos.json');
  156.  
  157. // Test 2.2: PHP Serialize
  158. $start_time = microtime(true);
  159. $serialized_data = serialize($array_de_arrays);
  160. file_put_contents('datos.serialize', $serialized_data);
  161. $end_time = microtime(true);
  162. echo "<h3>2. PHP Serialize (serialize / unserialize)</h3>";
  163. echo "<strong>Tiempo de guardado:</strong> " . format_time($start_time, $end_time) . "<br>";
  164. echo "<strong>Tamaño del archivo:</strong> " . format_mem(filesize('datos.serialize')) . "<br>";
  165. $start_time = microtime(true);
  166. $serial_leido = file_get_contents('datos.serialize');
  167. $datos_desde_serial = unserialize($serial_leido);
  168. $end_time = microtime(true);
  169. echo "<strong>Tiempo de lectura:</strong> " . format_time($start_time, $end_time) . "<br>";
  170. unlink('datos.serialize');
  171.  
  172. ?>
Success #stdin #stdout #stderr 0.14s 25884KB
stdin
Standard input is empty
stdout
<h1>Benchmark Final y Completo para Archmage</h1><p>Simulando la estructura de datos real, incluyendo la lista completa de habilidades.</p><hr><h2>Pruebas de Rendimiento en Memoria</h2><h3>1. Array de Arrays (Asociativos)</h3><strong>Creación:</strong> 0.00 ms<br><strong>Uso de Memoria:</strong> 0.00 KB<br><strong>Acceso aleatorio (500,000 veces):</strong> 53.46 ms<br><h3>2. Array de Objetos (stdClass)</h3><strong>Creación:</strong> 0.05 ms<br><strong>Uso de Memoria:</strong> 19.83 KB<br><strong>Acceso aleatorio (500,000 veces):</strong> 47.15 ms<br><hr><h2>Pruebas de Guardado y Lectura en Disco</h2><h3>1. JSON (json_encode / json_decode)</h3><strong>Tiempo de guardado:</strong> 0.54 ms<br><strong>Tamaño del archivo:</strong> 0.00 KB<br><strong>Tiempo de lectura:</strong> 0.03 ms<br><h3>2. PHP Serialize (serialize / unserialize)</h3><strong>Tiempo de guardado:</strong> 0.35 ms<br><strong>Tamaño del archivo:</strong> 0.00 KB<br><strong>Tiempo de lectura:</strong> 0.02 ms<br>
stderr
PHP Warning:  file_put_contents(datos.json): failed to open stream: Permission denied in /home/BNMJai/prog.php on line 145
PHP Warning:  filesize(): stat failed for datos.json in /home/BNMJai/prog.php on line 149
PHP Warning:  file_get_contents(datos.json): failed to open stream: No such file or directory in /home/BNMJai/prog.php on line 151
PHP Warning:  unlink(datos.json): No such file or directory in /home/BNMJai/prog.php on line 155
PHP Warning:  file_put_contents(datos.serialize): failed to open stream: Permission denied in /home/BNMJai/prog.php on line 160
PHP Warning:  filesize(): stat failed for datos.serialize in /home/BNMJai/prog.php on line 164
PHP Warning:  file_get_contents(datos.serialize): failed to open stream: No such file or directory in /home/BNMJai/prog.php on line 166
PHP Warning:  unlink(datos.serialize): No such file or directory in /home/BNMJai/prog.php on line 170