閱讀redis源代碼的一些體會html
最近在學習redis及閱讀redis等程序的源碼時,有一些收穫,特記錄到下面。java
1.第一步,閱讀源代碼藉助最好能夠跟蹤的工具去讀,如sourceinsight。
我使用的是windows7環境,又由於是c程序,故使用sourceinsight,固然還有其餘的方式,好比閱讀java代碼能夠導入到eclipse。redis
2. 第二步,搭建能夠啓動該源程序的環境,並能啓動成功。算法
根據最簡單的配置,使用源碼編譯的方式,啓動該程序。逐漸參考用戶使用手冊,修改配置文件進行測試,進而熟悉redis的使用。windows
3.第三步,閱讀代碼前先理清楚源碼的數據結構、基本概念、算法。bash
數據結構諸如字典、雙向鏈表的實現;數據結構
概念諸如aof、rdb、主從複製等
4. 第四步,使用debug模式啓動程序,跑完整個流程。
使用redis的debug模式,瞭解運行的流程,理清思路。eclipse
5. 第五步,使用單元測試對數據結構、算法、流程進行單元測試。工具
使用cunit先進行數據結構的單元測試,再進行算法,而後是流程的測試。單元測試
最後考慮根據模塊進行測試。
以上內容出自:http://www.cnblogs.com/davidwang456/p/3552409.html
增長一條:
六、增長打印日誌和畫代碼流程圖。1)猜想你認爲的代碼流程走向,並在你認爲走到的地方增長本身的打印信息,運行程序,看看是否能夠達到預期。整個和調 試BUG時用到的原理同樣,重要的一點是你要先判斷哪一個地方會調用,本身去主導代碼。2)畫代碼流程圖,根據你已經熟悉的流程,把代碼流程圖畫下來,方便本身整理思路,也能預防閱讀到後面忘記了前面的碼。圖形的表現會遠大於文字,大部分人剛開始都不肯意去畫,包括我,後來感受仍是頗有必要的。
學習redis的相關資料:
1. 《Redis入門很簡單》
2. 《Redis設計與實現》
這兩本書都看完了,感受挺好的,昨天有入手了一本《Redis實戰》還沒看,本想入手一本英文的,奈何比中文版貴了好幾倍。
看完這些書並應用起來後,上面的第1、2、三步應該就沒OK啦。
官網推薦的書(http://redis.io/documentation)以下:
The following is a list of books covering Redis that are already published. Books are ordered by release date (newer books first).
The following books have Redis related content but are not specifically about Redis:
4. 第四步,在redis.conf中須要配置一下loglevel爲debug,以下:
# 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) # warning (only very important / critical messages are logged) loglevel debug # Specify the log file name. Also the empty string can be used to force # Redis to log on the standard output. Note that if you use standard # output for logging but daemonize, logs will be sent to /dev/null logfile "./log/redis_log.txt"
5. 下載最新版本的redis(3.2.0)會在main.c裏面看到比以往版本中多加了測試代碼:
#ifdef REDIS_TEST if (argc == 3 && !strcasecmp(argv[1], "test")) { if (!strcasecmp(argv[2], "ziplist")) { return ziplistTest(argc, argv); } else if (!strcasecmp(argv[2], "quicklist")) { quicklistTest(argc, argv); } else if (!strcasecmp(argv[2], "intset")) { return intsetTest(argc, argv); } else if (!strcasecmp(argv[2], "zipmap")) { return zipmapTest(argc, argv); } else if (!strcasecmp(argv[2], "sha1test")) { return sha1Test(argc, argv); } else if (!strcasecmp(argv[2], "util")) { return utilTest(argc, argv); } else if (!strcasecmp(argv[2], "sds")) { return sdsTest(argc, argv); } else if (!strcasecmp(argv[2], "endianconv")) { return endianconvTest(argc, argv); } else if (!strcasecmp(argv[2], "crc64")) { return crc64Test(argc, argv); } return -1; /* test not found */ } #endif
若是須要測試以上的數據結果須要在makefile文件中,添加相關預編譯:
FINAL_CFLAGS+= -DREDIS_TEST
至於如何使用cunit進行白盒測試,還不清楚。