Opcache是php的加速器,5.3以前为APC,5.3以后边位opcache+apcu,5.5之后系统内置opcache(未确认)
之前一直忽略了这个利器,最近在做新项目,用ab测了下发现效果还是蛮强悍的,特别是有高并发需求的网站
本站加速前ab测试:
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 |
This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking hiwbb.com (be patient).....done Server Software: lighttpd/1.4.45 Server Hostname: hiwbb.com Server Port: 80 Document Path: / Document Length: 30812 bytes Concurrency Level: 100 Time taken for tests: 2.341 seconds Complete requests: 100 Failed requests: 49 (Connect: 0, Receive: 0, Length: 49, Exceptions: 0) Write errors: 0 Total transferred: 3104749 bytes HTML transferred: 3081249 bytes Requests per second: 42.72 [#/sec] (mean) Time per request: 2340.965 [ms] (mean) Time per request: 23.410 [ms] (mean, across all concurrent requests) Transfer rate: 1295.18 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 107 49.4 84 181 Processing: 192 1224 590.4 1279 2121 Waiting: 28 1220 595.1 1279 2121 Total: 219 1331 635.1 1362 2300 Percentage of the requests served within a certain time (ms) 50% 1362 66% 1724 75% 1889 80% 1974 90% 2197 95% 2254 98% 2287 99% 2300 100% 2300 (longest request) |
加速后ab:
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 |
This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking hiwbb.com (be patient).....done Server Software: lighttpd/1.4.45 Server Hostname: hiwbb.com Server Port: 80 Document Path: / Document Length: 30812 bytes Concurrency Level: 100 Time taken for tests: 0.376 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 3104700 bytes HTML transferred: 3081200 bytes Requests per second: 266.23 [#/sec] (mean) Time per request: 375.609 [ms] (mean) Time per request: 3.756 [ms] (mean, across all concurrent requests) Transfer rate: 8072.05 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 6 0.9 6 7 Processing: 18 297 50.0 295 357 Waiting: 7 296 50.8 295 357 Total: 19 303 49.7 301 362 Percentage of the requests served within a certain time (ms) 50% 301 66% 334 75% 340 80% 357 90% 360 95% 361 98% 362 99% 362 100% 362 (longest request) |
每秒可承受请求次数从42上升到266
安装方式
yum install php-opcache
本站适用remi-php源安的php5.6 所以是 yum install php-opcache --enablerepo=remi-php56
装完重启php/lighttpd即生效。
运行状态
查看opcache状态的单文件: https://github.com/rlerdorf/opcache-status, 3年没更新了,但功能强悍,看看内存占用什么的还是足够用的,方便调整数值。
直接下载: https://raw.githubusercontent.com/rlerdorf/opcache-status/master/opcache.php
没法上github?
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
<?php define('THOUSAND_SEPARATOR',true); if (!extension_loaded('Zend OPcache')) { echo '<div style="background-color: #F2DEDE; color: #B94A48; padding: 1em;">You do not have the Zend OPcache extension loaded, sample data is being shown instead.</div>'; require 'data-sample.php'; } class OpCacheDataModel { private $_configuration; private $_status; private $_d3Scripts = array(); public function __construct() { $this->_configuration = opcache_get_configuration(); $this->_status = opcache_get_status(); } public function getPageTitle() { return 'PHP ' . phpversion() . " with OpCache {$this->_configuration['version']['version']}"; } public function getStatusDataRows() { $rows = array(); foreach ($this->_status as $key => $value) { if ($key === 'scripts') { continue; } if (is_array($value)) { foreach ($value as $k => $v) { if ($v === false) { $value = 'false'; } if ($v === true) { $value = 'true'; } if ($k === 'used_memory' || $k === 'free_memory' || $k === 'wasted_memory') { $v = $this->_size_for_humans( $v ); } if ($k === 'current_wasted_percentage' || $k === 'opcache_hit_rate') { $v = number_format( $v, 2 ) . '%'; } if ($k === 'blacklist_miss_ratio') { $v = number_format($v, 2) . '%'; } if ($k === 'start_time' || $k === 'last_restart_time') { $v = ($v ? date(DATE_RFC822, $v) : 'never'); } if (THOUSAND_SEPARATOR === true && is_int($v)) { $v = number_format($v); } $rows[] = "<tr><th>$k</th><td>$v</td></tr>\n"; } continue; } if ($value === false) { $value = 'false'; } if ($value === true) { $value = 'true'; } $rows[] = "<tr><th>$key</th><td>$value</td></tr>\n"; } return implode("\n", $rows); } public function getConfigDataRows() { $rows = array(); foreach ($this->_configuration['directives'] as $key => $value) { if ($value === false) { $value = 'false'; } if ($value === true) { $value = 'true'; } if ($key == 'opcache.memory_consumption') { $value = $this->_size_for_humans($value); } $rows[] = "<tr><th>$key</th><td>$value</td></tr>\n"; } return implode("\n", $rows); } public function getScriptStatusRows() { foreach ($this->_status['scripts'] as $key => $data) { $dirs[dirname($key)][basename($key)] = $data; $this->_arrayPset($this->_d3Scripts, $key, array( 'name' => basename($key), 'size' => $data['memory_consumption'], )); } asort($dirs); $basename = ''; while (true) { if (count($this->_d3Scripts) !=1) break; $basename .= DIRECTORY_SEPARATOR . key($this->_d3Scripts); $this->_d3Scripts = reset($this->_d3Scripts); } $this->_d3Scripts = $this->_processPartition($this->_d3Scripts, $basename); $id = 1; $rows = array(); foreach ($dirs as $dir => $files) { $count = count($files); $file_plural = $count > 1 ? 's' : null; $m = 0; foreach ($files as $file => $data) { $m += $data["memory_consumption"]; } $m = $this->_size_for_humans($m); if ($count > 1) { $rows[] = '<tr>'; $rows[] = "<th class=\"clickable\" id=\"head-{$id}\" colspan=\"3\" onclick=\"toggleVisible('#head-{$id}', '#row-{$id}')\">{$dir} ({$count} file{$file_plural}, {$m})</th>"; $rows[] = '</tr>'; } foreach ($files as $file => $data) { $rows[] = "<tr id=\"row-{$id}\">"; $rows[] = "<td>" . $this->_format_value($data["hits"]) . "</td>"; $rows[] = "<td>" . $this->_size_for_humans($data["memory_consumption"]) . "</td>"; $rows[] = $count > 1 ? "<td>{$file}</td>" : "<td>{$dir}/{$file}</td>"; $rows[] = '</tr>'; } ++$id; } return implode("\n", $rows); } public function getScriptStatusCount() { return count($this->_status["scripts"]); } public function getGraphDataSetJson() { $dataset = array(); $dataset['memory'] = array( $this->_status['memory_usage']['used_memory'], $this->_status['memory_usage']['free_memory'], $this->_status['memory_usage']['wasted_memory'], ); $dataset['keys'] = array( $this->_status['opcache_statistics']['num_cached_keys'], $this->_status['opcache_statistics']['max_cached_keys'] - $this->_status['opcache_statistics']['num_cached_keys'], 0 ); $dataset['hits'] = array( $this->_status['opcache_statistics']['misses'], $this->_status['opcache_statistics']['hits'], 0, ); $dataset['restarts'] = array( $this->_status['opcache_statistics']['oom_restarts'], $this->_status['opcache_statistics']['manual_restarts'], $this->_status['opcache_statistics']['hash_restarts'], ); if (THOUSAND_SEPARATOR === true) { $dataset['TSEP'] = 1; } else { $dataset['TSEP'] = 0; } return json_encode($dataset); } public function getHumanUsedMemory() { return $this->_size_for_humans($this->getUsedMemory()); } public function getHumanFreeMemory() { return $this->_size_for_humans($this->getFreeMemory()); } public function getHumanWastedMemory() { return $this->_size_for_humans($this->getWastedMemory()); } public function getUsedMemory() { return $this->_status['memory_usage']['used_memory']; } public function getFreeMemory() { return $this->_status['memory_usage']['free_memory']; } public function getWastedMemory() { return $this->_status['memory_usage']['wasted_memory']; } public function getWastedMemoryPercentage() { return number_format($this->_status['memory_usage']['current_wasted_percentage'], 2); } public function getD3Scripts() { return $this->_d3Scripts; } private function _processPartition($value, $name = null) { if (array_key_exists('size', $value)) { return $value; } $array = array('name' => $name,'children' => array()); foreach ($value as $k => $v) { $array['children'][] = $this->_processPartition($v, $k); } return $array; } private function _format_value($value) { if (THOUSAND_SEPARATOR === true) { return number_format($value); } else { return $value; } } private function _size_for_humans($bytes) { if ($bytes > 1048576) { return sprintf('%.2f MB', $bytes / 1048576); } else { if ($bytes > 1024) { return sprintf('%.2f kB', $bytes / 1024); } else { return sprintf('%d bytes', $bytes); } } } // Borrowed from Laravel private function _arrayPset(&$array, $key, $value) { if (is_null($key)) return $array = $value; $keys = explode(DIRECTORY_SEPARATOR, ltrim($key, DIRECTORY_SEPARATOR)); while (count($keys) > 1) { $key = array_shift($keys); if ( ! isset($array[$key]) || ! is_array($array[$key])) { $array[$key] = array(); } $array =& $array[$key]; } $array[array_shift($keys)] = $value; return $array; } } $dataModel = new OpCacheDataModel(); ?> <!DOCTYPE html> <meta charset="utf-8"> <html> <head> <style> body { font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; margin: 0; padding: 0; } #container { width: 1024px; margin: auto; position: relative; } h1 { padding: 10px 0; } table { border-collapse: collapse; } tbody tr:nth-child(even) { background-color: #eee; } p.capitalize { text-transform: capitalize; } .tabs { position: relative; float: left; width: 60%; } .tab { float: left; } .tab label { background: #eee; padding: 10px 12px; border: 1px solid #ccc; margin-left: -1px; position: relative; left: 1px; } .tab [type=radio] { display: none; } .tab th, .tab td { padding: 8px 12px; } .content { position: absolute; top: 28px; left: 0; background: white; border: 1px solid #ccc; height: 450px; width: 100%; overflow: auto; } .content table { width: 100%; } .content th, .tab:nth-child(3) td { text-align: left; } .content td { text-align: right; } .clickable { cursor: pointer; } [type=radio]:checked ~ label { background: white; border-bottom: 1px solid white; z-index: 2; } [type=radio]:checked ~ label ~ .content { z-index: 1; } #graph { float: right; width: 40%; position: relative; } #graph > form { position: absolute; right: 60px; top: -20px; } #graph > svg { position: absolute; top: 0; right: 0; } #stats { position: absolute; right: 125px; top: 145px; } #stats th, #stats td { padding: 6px 10px; font-size: 0.8em; } #partition { position: absolute; width: 100%; height: 100%; z-index: 10; top: 0; left: 0; background: #ddd; display: none; } #close-partition { display: none; position: absolute; z-index: 20; right: 15px; top: 15px; background: #f9373d; color: #fff; padding: 12px 15px; } #close-partition:hover { background: #D32F33; cursor: pointer; } #partition rect { stroke: #fff; fill: #aaa; fill-opacity: 1; } #partition rect.parent { cursor: pointer; fill: steelblue; } #partition text { pointer-events: none; } label { cursor: pointer; } </style> <script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.0.1/d3.v3.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> var hidden = {}; function toggleVisible(head, row) { if (!hidden[row]) { d3.selectAll(row).transition().style('display', 'none'); hidden[row] = true; d3.select(head).transition().style('color', '#ccc'); } else { d3.selectAll(row).transition().style('display'); hidden[row] = false; d3.select(head).transition().style('color', '#000'); } } </script> <title><?php echo $dataModel->getPageTitle(); ?></title> </head> <body> <div id="container"> <h1><?php echo $dataModel->getPageTitle(); ?></h1> <div class="tabs"> <div class="tab"> <input type="radio" id="tab-status" name="tab-group-1" checked> <label for="tab-status">Status</label> <div class="content"> <table> <?php echo $dataModel->getStatusDataRows(); ?> </table> </div> </div> <div class="tab"> <input type="radio" id="tab-config" name="tab-group-1"> <label for="tab-config">Configuration</label> <div class="content"> <table> <?php echo $dataModel->getConfigDataRows(); ?> </table> </div> </div> <div class="tab"> <input type="radio" id="tab-scripts" name="tab-group-1"> <label for="tab-scripts">Scripts (<?php echo $dataModel->getScriptStatusCount(); ?>)</label> <div class="content"> <table style="font-size:0.8em;"> <tr> <th width="10%">Hits</th> <th width="20%">Memory</th> <th width="70%">Path</th> </tr> <?php echo $dataModel->getScriptStatusRows(); ?> </table> </div> </div> <div class="tab"> <input type="radio" id="tab-visualise" name="tab-group-1"> <label for="tab-visualise">Visualise Partition</label> <div class="content"></div> </div> </div> <div id="graph"> <form> <label><input type="radio" name="dataset" value="memory" checked> Memory</label> <label><input type="radio" name="dataset" value="keys"> Keys</label> <label><input type="radio" name="dataset" value="hits"> Hits</label> <label><input type="radio" name="dataset" value="restarts"> Restarts</label> </form> <div id="stats"></div> </div> </div> <div id="close-partition">✖ Close Visualisation</div> <div id="partition"></div> <script> var dataset = <?php echo $dataModel->getGraphDataSetJson(); ?>; var width = 400, height = 400, radius = Math.min(width, height) / 2, colours = ['#B41F1F', '#1FB437', '#ff7f0e']; d3.scale.customColours = function() { return d3.scale.ordinal().range(colours); }; var colour = d3.scale.customColours(); var pie = d3.layout.pie().sort(null); var arc = d3.svg.arc().innerRadius(radius - 20).outerRadius(radius - 50); var svg = d3.select("#graph").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var path = svg.selectAll("path") .data(pie(dataset.memory)) .enter().append("path") .attr("fill", function(d, i) { return colour(i); }) .attr("d", arc) .each(function(d) { this._current = d; }); // store the initial values d3.selectAll("input").on("change", change); set_text("memory"); function set_text(t) { if (t === "memory") { d3.select("#stats").html( "<table><tr><th style='background:#B41F1F;'>Used</th><td><?php echo $dataModel->getHumanUsedMemory()?></td></tr>"+ "<tr><th style='background:#1FB437;'>Free</th><td><?php echo $dataModel->getHumanFreeMemory()?></td></tr>"+ "<tr><th style='background:#ff7f0e;' rowspan=\"2\">Wasted</th><td><?php echo $dataModel->getHumanWastedMemory()?></td></tr>"+ "<tr><td><?php echo $dataModel->getWastedMemoryPercentage()?>%</td></tr></table>" ); } else if (t === "keys") { d3.select("#stats").html( "<table><tr><th style='background:#B41F1F;'>Cached keys</th><td>"+format_value(dataset[t][0])+"</td></tr>"+ "<tr><th style='background:#1FB437;'>Free Keys</th><td>"+format_value(dataset[t][1])+"</td></tr></table>" ); } else if (t === "hits") { d3.select("#stats").html( "<table><tr><th style='background:#B41F1F;'>Misses</th><td>"+format_value(dataset[t][0])+"</td></tr>"+ "<tr><th style='background:#1FB437;'>Cache Hits</th><td>"+format_value(dataset[t][1])+"</td></tr></table>" ); } else if (t === "restarts") { d3.select("#stats").html( "<table><tr><th style='background:#B41F1F;'>Memory</th><td>"+dataset[t][0]+"</td></tr>"+ "<tr><th style='background:#1FB437;'>Manual</th><td>"+dataset[t][1]+"</td></tr>"+ "<tr><th style='background:#ff7f0e;'>Keys</th><td>"+dataset[t][2]+"</td></tr></table>" ); } } function change() { // Filter out any zero values to see if there is anything left var remove_zero_values = dataset[this.value].filter(function(value) { return value > 0; }); // Skip if the value is undefined for some reason if (typeof dataset[this.value] !== 'undefined' && remove_zero_values.length > 0) { $('#graph').find('> svg').show(); path = path.data(pie(dataset[this.value])); // update the data path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs // Hide the graph if we can't draw it correctly, not ideal but this works } else { $('#graph').find('> svg').hide(); } set_text(this.value); } function arcTween(a) { var i = d3.interpolate(this._current, a); this._current = i(0); return function(t) { return arc(i(t)); }; } function size_for_humans(bytes) { if (bytes > 1048576) { return (bytes/1048576).toFixed(2) + ' MB'; } else if (bytes > 1024) { return (bytes/1024).toFixed(2) + ' KB'; } else return bytes + ' bytes'; } function format_value(value) { if (dataset["TSEP"] == 1) { return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } else { return value; } } var w = window.innerWidth, h = window.innerHeight, x = d3.scale.linear().range([0, w]), y = d3.scale.linear().range([0, h]); var vis = d3.select("#partition") .style("width", w + "px") .style("height", h + "px") .append("svg:svg") .attr("width", w) .attr("height", h); var partition = d3.layout.partition() .value(function(d) { return d.size; }); root = JSON.parse('<?php echo json_encode($dataModel->getD3Scripts()); ?>'); var g = vis.selectAll("g") .data(partition.nodes(root)) .enter().append("svg:g") .attr("transform", function(d) { return "translate(" + x(d.y) + "," + y(d.x) + ")"; }) .on("click", click); var kx = w / root.dx, ky = h / 1; g.append("svg:rect") .attr("width", root.dy * kx) .attr("height", function(d) { return d.dx * ky; }) .attr("class", function(d) { return d.children ? "parent" : "child"; }); g.append("svg:text") .attr("transform", transform) .attr("dy", ".35em") .style("opacity", function(d) { return d.dx * ky > 12 ? 1 : 0; }) .text(function(d) { return d.name; }) d3.select(window) .on("click", function() { click(root); }) function click(d) { if (!d.children) return; kx = (d.y ? w - 40 : w) / (1 - d.y); ky = h / d.dx; x.domain([d.y, 1]).range([d.y ? 40 : 0, w]); y.domain([d.x, d.x + d.dx]); var t = g.transition() .duration(d3.event.altKey ? 7500 : 750) .attr("transform", function(d) { return "translate(" + x(d.y) + "," + y(d.x) + ")"; }); t.select("rect") .attr("width", d.dy * kx) .attr("height", function(d) { return d.dx * ky; }); t.select("text") .attr("transform", transform) .style("opacity", function(d) { return d.dx * ky > 12 ? 1 : 0; }); d3.event.stopPropagation(); } function transform(d) { return "translate(8," + d.dx * ky / 2 + ")"; } $(document).ready(function() { function handleVisualisationToggle(close) { $('#partition, #close-partition').fadeToggle(); // Is the visualisation being closed? If so show the status tab again if (close) { $('#tab-visualise').removeAttr('checked'); $('#tab-status').trigger('click'); } } $('label[for="tab-visualise"], #close-partition').on('click', function() { handleVisualisationToggle(($(this).attr('id') === 'close-partition')); }); $(document).keyup(function(e) { if (e.keyCode == 27) handleVisualisationToggle(true); }); }); </script> </body> </html> |