《快学BigData》--Redis 总结(G)(32)

2023-03-06 21:17:19 浏览数 (1)

Redis 发布订阅

Redis 的消息订阅/发布(pub/sub)是一种消息的模型,Redis客户端可以订阅任意数量级的频道,一旦某频道接收到消息时,订阅他的客户端就会收到信息,接下来演示一下实例:

1-1)、订阅窗口

[root@hadoop3 src]# ./redis-cli

127.0.0.1:6379> SUBSCRIBE message

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "message"

3) (integer) 1

1-2)、发布窗口

[root@hadoop3 src]# ./redis-cli

127.0.0.1:6379> PUBLISH message "new message"

(integer) 1

127.0.0.1:6379> PUBLISH message "new message1"

(integer) 1

1-3)、查看订阅窗口

127.0.0.1:6379> SUBSCRIBE message

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "message"

3) (integer) 1

1) "message"

2) "message"

3) "new message"

1) "message"

2) "message"

3) "new message1"

Redis 的性能测试

我们使用Redis自带的redis-benchmark进行测试

1-1)、查看帮助信息

[root@hadoop3 src]# ./redis-benchmark -h

Invalid option "-h" or option argument missing

Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]

-h <hostname> Server hostname (default 127.0.0.1)

-p <port> Server port (default 6379)

-s <socket> Server socket (overrides host and port)

-a <password> Password for Redis Auth

-c <clients> Number of parallel connections (default 50)

-n <requests> Total number of requests (default 100000)

-d <size> Data size of SET/GET value in bytes (default 2)

-dbnum <db> SELECT the specified db number (default 0)

-k <boolean> 1=keep alive 0=reconnect (default 1)

-r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD

Using this option the benchmark will expand the string __rand_int__

inside an argument with a 12 digits number in the specified range

from 0 to keyspacelen-1. The substitution changes every time a command

is executed. Default tests use this to hit random keys in the

specified range.

-P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).

-q Quiet. Just show query/sec values

--csv Output in CSV format

-l Loop. Run the tests forever

-t <tests> Only run the comma separated list of tests. The test

names are the same as the ones produced as output.

-I Idle mode. Just open N idle connections and wait.

Examples:

Run the benchmark with the default configuration against 127.0.0.1:6379:

$ redis-benchmark

Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:

$ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20

Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:

$ redis-benchmark -t set -n 1000000 -r 100000000

Benchmark 127.0.0.1:6379 for a few commands producing CSV output:

$ redis-benchmark -t ping,set,get -n 100000 --csv

Benchmark a specific command line:

$ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0

Fill a list with 10000 random elements:

$ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__

On user specified command lines __rand_int__ is replaced with a random integer

with a range of values selected by the -r option.

1-2)、实例

A)、测试并发

100个并发连接,100000个请求,检测host为localhost 端口为6379的redis服务器性能

[root@hadoop3 src]# ./redis-benchmark -h localhost -p 6379 -c 100 -n 1000

====== PING_INLINE ======

1000 requests completed in 0.04 seconds

100 parallel clients

3 bytes payload

keep alive: 1

****************

详细的参数请参考Blog

http://blog.csdn.net/xfg0218/article/details/52825874

B)、测试数据包的问题

测试存取大小为100字节的数据包的性能

[root@hadoop3 src]# ./redis-benchmark -h localhost -p 6379 -q -d 100

PING_INLINE: 31172.07 requests per second

PING_BULK: 33886.82 requests per second

SET: 30998.14 requests per second

GET: 30693.68 requests per second

INCR: 33079.72 requests per second

LPUSH: 29403.12 requests per second

LPOP: 32530.91 requests per second

SADD: 31377.47 requests per second

SPOP: 33134.53 requests per second

LPUSH (needed to benchmark LRANGE): 32393.91 requests per second

LRANGE_100 (first 100 elements): 12055.46 requests per second

LRANGE_300 (first 300 elements): 4528.58 requests per second

LRANGE_500 (first 450 elements): 2971.68 requests per second

LRANGE_600 (first 600 elements): 2184.22 requests per second

MSET (10 keys): 19361.08 requests per second

C)、测试set,lpush的性能

[root@hadoop3 src]# ./redis-benchmark -t set,lpush -n 1000 -q

SET: 33333.34 requests per second

LPUSH: 33333.34 requests per second

-q 是只显示测试的结果

D)、只测试某些数值存取的性能

[root@hadoop3 src]# ./redis-benchmark -n 1000 -q script load "redis.call('set','username','xiaozhang')"

script load redis.call('set','username','xiaozhang'): 27777.78 requests per second

Redis-trib.rb详解

[root@hadoop1 src]# ./redis-trib.rb help

Usage: redis-trib <command> <options> <arguments ...>

set-timeout host:port milliseconds

info host:port

check host:port

call host:port command arg arg .. arg

fix host:port

--timeout <arg>

help (show this help)

add-node new_host:new_port existing_host:existing_port

--slave

--master-id <arg>

rebalance host:port

--timeout <arg>

--use-empty-masters

--threshold <arg>

--auto-weights

--pipeline <arg>

--weight <arg>

--simulate

import host:port

--from <arg>

--replace

--copy

del-node host:port node_id

create host1:port1 ... hostN:portN

--replicas <arg>

reshard host:port

--from <arg>

--timeout <arg>

--yes

--slots <arg>

--to <arg>

--pipeline <arg>

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

可以看到redis-trib.rb具有以下功能:

1、create:创建集群

2、check:检查集群

3、info:查看集群信息

4、fix:修复集群

5、reshard:在线迁移slot

6、rebalance:平衡集群节点slot数量

7、add-node:将新节点加入集群

8、del-node:从集群中删除节点

9、set-timeout:设置集群节点间心跳连接的超时时间

10、call:在集群全部节点上执行命令

11、import:将外部redis数据导入集群

具体的每一个功能请查看:

http://blog.csdn.net/xfg0218/article/details/56505216

或者

http://blog.csdn.net/xfg0218/article/details/56678783

0 人点赞