| 6848 | #define LATENCY_SAMPLE_RATE 10 /* milliseconds. */ |
| 6849 | #define LATENCY_HISTORY_DEFAULT_INTERVAL 15000 /* milliseconds. */ |
| 6850 | static void latencyMode(void) { |
| 6851 | redisReply *reply; |
| 6852 | long long start, latency, min = 0, max = 0, tot = 0, count = 0; |
| 6853 | long long history_interval = |
| 6854 | config.interval ? config.interval/1000 : |
| 6855 | LATENCY_HISTORY_DEFAULT_INTERVAL; |
| 6856 | double avg; |
| 6857 | long long history_start = mstime(); |
| 6858 | |
| 6859 | /* Set a default for the interval in case of --latency option |
| 6860 | * with --raw, --csv or when it is redirected to non tty. */ |
| 6861 | if (config.interval == 0) { |
| 6862 | config.interval = 1000; |
| 6863 | } else { |
| 6864 | config.interval /= 1000; /* We need to convert to milliseconds. */ |
| 6865 | } |
| 6866 | |
| 6867 | if (!context) exit(1); |
| 6868 | while(1) { |
| 6869 | start = mstime(); |
| 6870 | reply = reconnectingRedisCommand(context,"PING"); |
| 6871 | if (reply == NULL) { |
| 6872 | fprintf(stderr,"\nI/O error\n"); |
| 6873 | exit(1); |
| 6874 | } |
| 6875 | latency = mstime()-start; |
| 6876 | freeReplyObject(reply); |
| 6877 | count++; |
| 6878 | if (count == 1) { |
| 6879 | min = max = tot = latency; |
| 6880 | avg = (double) latency; |
| 6881 | } else { |
| 6882 | if (latency < min) min = latency; |
| 6883 | if (latency > max) max = latency; |
| 6884 | tot += latency; |
| 6885 | avg = (double) tot/count; |
| 6886 | } |
| 6887 | |
| 6888 | if (config.output == OUTPUT_STANDARD) { |
| 6889 | printf("\x1b[0G\x1b[2K"); /* Clear the line. */ |
| 6890 | latencyModePrint(min,max,avg,count); |
| 6891 | } else { |
| 6892 | if (config.latency_history) { |
| 6893 | latencyModePrint(min,max,avg,count); |
| 6894 | } else if (mstime()-history_start > config.interval) { |
| 6895 | latencyModePrint(min,max,avg,count); |
| 6896 | exit(0); |
| 6897 | } |
| 6898 | } |
| 6899 | |
| 6900 | if (config.latency_history && mstime()-history_start > history_interval) |
| 6901 | { |
| 6902 | printf(" -- %.2f seconds range\n", (float)(mstime()-history_start)/1000); |
| 6903 | history_start = mstime(); |
| 6904 | min = max = tot = count = 0; |
| 6905 | } |
| 6906 | usleep(LATENCY_SAMPLE_RATE * 1000); |
| 6907 | } |
no test coverage detected