|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created by PyCharm. |
| 5 | +File Name: LinuxBashShellScriptForOps:redis-performance-tester.py |
| 6 | +Version: 0.0.1 |
| 7 | +Author: dgden |
| 8 | +Author Email: dgdenterprise@gmail.com |
| 9 | +URL: https://github.com/DingGuodong/LinuxBashShellScriptForOps |
| 10 | +Download URL: https://github.com/DingGuodong/LinuxBashShellScriptForOps/tarball/master |
| 11 | +Create Date: 2021/3/25 |
| 12 | +Create Time: 14:28 |
| 13 | +Description: do a performance benchmark for redis-server using python and redis library |
| 14 | +Long Description: |
| 15 | +References: |
| 16 | +Prerequisites: [] |
| 17 | +Development Status: 3 - Alpha, 5 - Production/Stable |
| 18 | +Environment: Console |
| 19 | +Intended Audience: System Administrators, Developers, End Users/Desktop |
| 20 | +License: Freeware, Freely Distributable |
| 21 | +Natural Language: English, Chinese (Simplified) |
| 22 | +Operating System: POSIX :: Linux, Microsoft :: Windows |
| 23 | +Programming Language: Python :: 2.6 |
| 24 | +Programming Language: Python :: 2.7 |
| 25 | +Topic: Utilities |
| 26 | + """ |
| 27 | +import time |
| 28 | +import warnings |
| 29 | + |
| 30 | +from redis import Redis |
| 31 | + |
| 32 | +redis_conn = Redis(host='127.0.0.1', port=6379, password='', |
| 33 | + decode_responses=True, |
| 34 | + charset='UTF-8', encoding='UTF-8') |
| 35 | +redis_info = redis_conn.info(section='server') |
| 36 | +redis_version = redis_info.get("redis_version") |
| 37 | +print("redis_version:{}".format(redis_version)) |
| 38 | + |
| 39 | + |
| 40 | +def _do_redis_benchmark(): |
| 41 | + # redis-benchmark -c 1 -n 10000 incr x 1 |
| 42 | + test_key_name = "x" |
| 43 | + redis_conn.delete(test_key_name) |
| 44 | + start_time = time.time() |
| 45 | + while True: |
| 46 | + redis_conn.incr(test_key_name, 1) |
| 47 | + spent_time = time.time() - start_time |
| 48 | + if spent_time >= 1.0: |
| 49 | + break |
| 50 | + count = redis_conn.get("x") |
| 51 | + precise_count = float(count) / spent_time |
| 52 | + |
| 53 | + return precise_count |
| 54 | + |
| 55 | + |
| 56 | +def do_redis_benchmark(times=4): |
| 57 | + benchmark_result = [_do_redis_benchmark() for _ in range(times)] |
| 58 | + max_count = max(benchmark_result) |
| 59 | + min_count = min(benchmark_result) |
| 60 | + avg_count = sum(benchmark_result) / times |
| 61 | + print("do {times} times bench(using redis `INCR`), max: {max}, min: {min}, avg: {avg} requests per second".format( |
| 62 | + times=times, |
| 63 | + max=max_count, |
| 64 | + min=min_count, |
| 65 | + avg=avg_count)) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + warnings.warn("Tips: you can use `redis-benchmark` to test full performance of Redis server.") |
| 70 | + do_redis_benchmark() |
0 commit comments