Test the reading and writing efficiency of PHP to redis and shared memory (SHM) for 10000 cycles under CentOS

Time:2021-10-18

Redis, Memcache and shared memory all read data from memory. In order to test who is more efficient, I conducted a fair test under my CentOS virtual machine.

Test parameters

Environment: CentOS (configuration ignored), language: PHP, webserver: nginx, test times: 10000, character type and length: string (1024)

Prepare for test

<?php
//Test data
$arr = array( 1 => 
  array  At the same time, it's going to be taken away from the ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddor or a 5.45, and it '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '''s 5.45. It ''s the sort of the way to make the way for a way to the sort of the sort that' '' '' '' '' '' '' '' '' ''s a collection of the stuff that' '''s going for a way way way way way is going going for a home home home home home home to ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddssss','mingzhong ' => '51','hit_class' => 'SSSS','anti_dizziness' => '0','anti_dizzinesssdfdsfdsfdsfdsfds_class' => 'F','anti_critical' => '20','anti_critical_class' => 'F','tenacity' => '0','tenacity_class' => 'F',
  ),
  2 => 
  array  At the same time as an away away away away from the 414ddffffff7, at the back of an away away away away away away away away away away away away away away away away away away away away from the 5.45, and at the same time. It ''s a great deal, and the away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away away from the 5.45, and it' ''45.45, and the sort class of the away away away away away away away away is a class that will take a home home home home home home for the divine level, and it' '' ''s a well well for the rest of the world, where where it' '' '' '' ''s just just just just a way way way way way way way way, and it' '' '' '' '' ''home home home home home home home home home home home home home home home home home home home home home home home home home home home class gzhong' = > '51','Hi ' t_class' => 'SSSS','anti_dizziness' => '0','anti_dizziness_clsdfdsfdsfdsfdsfdsass' => 'F','anti_critical' => '20','anti_critical_class' => 'F','tenacity' => '0','tenacity_class' => 'F',
  ));

$str = json_encode($arr);
echo strlen($str);          // Output 1024

Perform 10000 write tests on redis

<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->auth('310c8cabcdefghf2d8abcdefd44496ac80');
$start = microtime(true) ;

for($i = 0; $i<10000; $i++) {
    $redis->set('key', $str);
}
echo microtime(true) - $start;    // Five times, the average result is 1.7016470432281 seconds

10000 writes to shared memory (SHM)

<?php
$key = 0x4337b123;  
$size = 1024;  
$shmid = @shmop_open($key, 'c', 0644, $size);  
$start = microtime(true);
for($i = 0; $i<10000; $i++) {
    shmop_write($shmid, $str, 0); 
}
@shmop_close($shmid);
echo microtime(true) - $start;  // Execute 5 times, and the average result is about 0.0025370121002197 seconds

Perform 10000 read tests on redis

<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->auth('310c8cabcdefghf2d8abcdefd44496ac80');
$start = microtime(true) ;

for($i = 0; $i<10000; $i++) {
    $redis->get('key');
}
echo microtime(true) - $start;     // Five times, the average result is 2.1236310005188 seconds

10000 read tests on shared memory (SHM)

<?php
$key = 0x4337b123;  
$size = 1024;  
$shmid = @shmop_open($key, 'c', 0644, $size);  
$start = microtime(true);
for($i = 0; $i<10000; $i++) {
    shmop_read($shmid, 0,1024);
}
@shmop_close($shmid);
echo microtime(true) - $start;     // Five times, the average result is about 0.0021078586578369 seconds

List of results

Redis read: 2.1 seconds write: 1.7 seconds

SHM read: 0.0021 seconds write: 0.0025 seconds

It can be seen that the effect is very different.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]