Redis与MySQL基准测试

我们比较看一下这两个流行的数据库选项,即再次使NoSQL和SQL数据库相互竞争。

在本文中,通过优锐课核心java学习笔记中,我们将讨论Redis和MySQL的性能基准测试。 我们将从在Ubuntu上引入和安装Redis开始。 然后,我们将朝着这两者之间的基准测试迈进。码了很多专业的相关知识, 分享给大家参考学习

1、Redis简介

根据官方网站的说法,Redis是一种开源(BSD许可)的内存中数据结构存储,用作数据库,缓存和消息代理。 实际上,Redis是高级键值存储。 它具有超高的吞吐率,因此超快,因为它每秒可以执行约110000个SET,每秒执行约81000个GET。 它还支持一组非常丰富的数据类型来存储。 实际上,Redis每次都将数据保留在内存中,但也保留在磁盘上。 因此,这需要权衡:惊人的速度和数据集的大小限制(根据内存)。 在本文中,为了有一些与MySQL相比的基准,我们将仅使用Redis作为缓存引擎。

2、先决条件

·在计算机上安装/配置了PHP,如果没有,请转到此处:如何在Ubuntu 16.04上安装PHP

·在你的计算机上安装/配置了MySQL,如果没有,请转到此处:如何在Ubuntu 16.04上安装MySQL

3、在Ubuntu上安装Redis

首先,运行以下命令:

 sudo apt update

sudo apt install redis-server

这些命令将更新apt软件包并在Ubuntu计算机上安装Redis。

现在,事实上,要启用Redis用作服务,你需要通过更新Redis配置文件中存在的受监管指令来对其进行配置。你可以在这里轻松找到配置文件:

 sudo vi /etc/redis/redis.conf复制代码

默认情况下,受监管的指令设置为“否”。你需要将其设置为:systemd。 更新后,配置文件的这一部分将类似于以下内容:

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.

# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.

daemonize yes

# If you run Redis from upstart or systemd, Redis can interact with your

# supervision tree. Options:

# supervised no - no supervision interaction

# supervised upstart - signal upstart by putting Redis into SIGSTOP mode

# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET

# supervised auto - detect upstart or systemd method based on

# UPSTART_JOB or NOTIFY_SOCKET environment variables

# Note: these supervision methods only signal "process is ready."

# They do not enable continuous liveness pings back to your supervisor.

supervised systemd

# If a pid file is specified, Redis writes it where specified at startup

# and removes it at exit.

#

# When the server runs non daemonized, no pid file is created if none is

# specified in the configuration. When the server is daemonized, the pid file

# is used even if not specified, defaulting to "/var/run/redis.pid".

#

# Creating a pid file is best effort: if Redis is not able to create it

# nothing bad happens, the server will start and run normally.

pidfile /var/run/redis/redis-server.pid

# Specify the server verbosity level.

# This can be one of:

# debug (a lot of information, useful for development/testing)

# verbose (many rarely useful info, but not a mess like the debug level)

# notice (moderately verbose, what you want in production probably)

4、启用密码验证:

使用密码身份验证配置Redis不是强制性的,但是它非常重要(并且也很容易),因为它启用了Redis的安全因素。 使用密码配置我们的Redis服务器非常简单,可以通过上述相同的配置文件来完成。 因此,打开配置文件并查找``requirepass''指令。你将默认情况下注释该行,只需取消注释并在此处输入密码即可。配置文件将类似于以下内容:

# Require clients to issue AUTH <PASSWORD> before processing any other   # commands. This might be useful in environments in which you do not trust   # others with access to the host running redis-server.   #   # This should stay commented out for backward compatibility and because most   # people do not need auth (e.g. they run their own servers).   #   # Warning: since Redis is pretty fast an outside user can try up to   # 150k passwords per second against a good box. This means that you should   # use a very strong password otherwise it will be very easy to break.   requirepass yourpasswordhere   # Command renaming.   #   # It is possible to change the name of dangerous commands in a shared   # environment. For instance the CONFIG command may be renamed into something   # hard to guess so that it will still be available for internal-use tools   # but not available for general clients.   #   # Example:   #   # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52复制代码

立即保存此文件,并使更改反映在Redis上,请使用以下命令重新启动Redis服务:

sudo systemctl restart redis.service复制代码

5、在Ubuntu上安装PHPRedis:

现在,要使你的PHP代码能够将Redis用作服务:

·运行以下命令以安装PHPRedis扩展名:

sudo apt-get install php-redis复制代码

·将以下行添加到你的php.ini文件中:

extension=redis.so复制代码

6、工作流程

·仅使用MySQL:

  1. 在[1,10000]之间随机生成一个密钥,并在MySQL数据库中搜索该密钥
  2. o请记下这样做所花费的时间
  3. 应使用时间样本来处理n个此类请求,同时将n逐渐增加为1、10、100、1000、10000、100000、1000000、1000000

·使用MySQL和Redis:

  1. 随机生成一个介于[1,10000]之间的密钥
  2. 将检查该密钥是否已经存在/存储在我们的Redis中
  3. §如果Redis上有它,我们不会打MySQL
  4. §如果Redis中不存在该密钥,我们将在MySQL数据库中搜索该密钥,并将该密钥存储到Redis
  5. o请记下这样做所花费的时间应使用时间样本来处理

  6. n个此类请求,同时将n逐渐增加为1、10、100、1000、10000、100000、1000000、1000000

7、源代码

仅MySQL源代码

(当我们尝试仅从MySQL获取密钥时):

<?php   $con = mysqli_connect("localhost","root","admin","blog_db");   for($i = 1; $i <= 10000000; $i = $i *10) {             $startTime = microtime(true);        for($j = 1; $j <= $i; $j++) {             $rand = rand(1, 100000);             $sql = "SELECT VALUE from data WHERE `key` = $rand";                       if (!mysqli_query($con, $sql)) {               echo "Error: " . $sql . "" . mysqli_error($con);             }        }   $endTime = microtime(true);   file_put_contents('/home/ayush/Desktop/temp/blog/mysqlonly.log', $i . ',' . ($endTime - $startTime) . "\n" , FILE_APPEND);   }复制代码

8、MySQL和Redis源代码

(当我们尝试先从Redis获取一个密钥,然后在无法在Redis上找到该密钥的情况下,从MySQL获取密钥时):

<?php   $con = mysqli_connect("localhost","root","admin","blog_db");   $client = new Redis();   $client->connect('localhost');   for($i = 1; $i <= 10000000; $i = $i *10) {        $startTime = microtime(true);        for($j = 1; $j <= $i; $j++) {             $rand = rand(1, 100000);             if(!$client->exists($rand)) {                  $client->set($rand, $rand);                  $sql = "SELECT VALUE from data WHERE `key` = $rand";                            if (!mysqli_query($con, $sql)) {                    echo "Error: " . $sql . "" . mysqli_error($con);                  }             }         }        $endTime = microtime(true);        file_put_contents('/home/ayush/Desktop/temp/blog/redis.log', $i . ',' . ($endTime - $startTime) . "\n" , FILE_APPEND);        $client->flushAll();   }复制代码

基准测试

表格数据

结论

从上面给出的图形表示可以很容易地看出,随着请求数量的显着增加,

Redis的性能开始显着提高。 因此,如果处理的请求数量很大,则将Redis这样的缓存引擎与数据库一起使用是一个好主意。

抽丝剥茧。细说架构那些事 优锐课


猜你喜欢

转载自juejin.im/post/5df72f05518825126a6c63ec
今日推荐