1
0

querybuf.tcl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. proc client_idle_sec {name} {
  2. set clients [split [r client list] "\r\n"]
  3. set c [lsearch -inline $clients *name=$name*]
  4. assert {[regexp {idle=([0-9]+)} $c - idle]}
  5. return $idle
  6. }
  7. # Calculate query buffer memory of slave
  8. proc client_query_buffer {name} {
  9. set clients [split [r client list] "\r\n"]
  10. set c [lsearch -inline $clients *name=$name*]
  11. if {[string length $c] > 0} {
  12. assert {[regexp {qbuf=([0-9]+)} $c - qbuf]}
  13. assert {[regexp {qbuf-free=([0-9]+)} $c - qbuf_free]}
  14. return [expr $qbuf + $qbuf_free]
  15. }
  16. return 0
  17. }
  18. start_server {tags {"querybuf slow"}} {
  19. # The test will run at least 2s to check if client query
  20. # buffer will be resized when client idle 2s.
  21. test "query buffer resized correctly" {
  22. set rd [redis_client]
  23. $rd client setname test_client
  24. set orig_test_client_qbuf [client_query_buffer test_client]
  25. # Make sure query buff has less than the peak resize threshold (PROTO_RESIZE_THRESHOLD) 32k
  26. # but at least the basic IO reading buffer size (PROTO_IOBUF_LEN) 16k
  27. assert {$orig_test_client_qbuf >= 16384 && $orig_test_client_qbuf < 32768}
  28. # Check that the initial query buffer is resized after 2 sec
  29. wait_for_condition 1000 10 {
  30. [client_idle_sec test_client] >= 3 && [client_query_buffer test_client] == 0
  31. } else {
  32. fail "query buffer was not resized"
  33. }
  34. $rd close
  35. }
  36. test "query buffer resized correctly when not idle" {
  37. # Memory will increase by more than 32k due to client query buffer.
  38. set rd [redis_client]
  39. $rd client setname test_client
  40. # Create a large query buffer (more than PROTO_RESIZE_THRESHOLD - 32k)
  41. $rd set x [string repeat A 400000]
  42. # Make sure query buff is larger than the peak resize threshold (PROTO_RESIZE_THRESHOLD) 32k
  43. set orig_test_client_qbuf [client_query_buffer test_client]
  44. assert {$orig_test_client_qbuf > 32768}
  45. # Wait for qbuf to shrink due to lower peak
  46. set t [clock milliseconds]
  47. while true {
  48. # Write something smaller, so query buf peak can shrink
  49. $rd set x [string repeat A 100]
  50. set new_test_client_qbuf [client_query_buffer test_client]
  51. if {$new_test_client_qbuf < $orig_test_client_qbuf} { break }
  52. if {[expr [clock milliseconds] - $t] > 1000} { break }
  53. after 10
  54. }
  55. # Validate qbuf shrunk but isn't 0 since we maintain room based on latest peak
  56. assert {[client_query_buffer test_client] > 0 && [client_query_buffer test_client] < $orig_test_client_qbuf}
  57. $rd close
  58. }
  59. }