logcat_benchmark.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2013-2014 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <benchmark/benchmark.h>
  20. static const char begin[] = "--------- beginning of ";
  21. static void BM_logcat_sorted_order(benchmark::State& state) {
  22. FILE* fp;
  23. if (!state.KeepRunning()) return;
  24. fp = popen(
  25. "logcat -v time -b radio -b events -b system -b main -d 2>/dev/null",
  26. "r");
  27. if (!fp) return;
  28. class timestamp {
  29. private:
  30. int month;
  31. int day;
  32. int hour;
  33. int minute;
  34. int second;
  35. int millisecond;
  36. bool ok;
  37. public:
  38. void init(const char* buffer) {
  39. ok = false;
  40. if (buffer != NULL) {
  41. ok = sscanf(buffer, "%d-%d %d:%d:%d.%d ", &month, &day, &hour,
  42. &minute, &second, &millisecond) == 6;
  43. }
  44. }
  45. explicit timestamp(const char* buffer) {
  46. init(buffer);
  47. }
  48. bool operator<(timestamp& T) {
  49. return !ok || !T.ok || (month < T.month) ||
  50. ((month == T.month) &&
  51. ((day < T.day) ||
  52. ((day == T.day) &&
  53. ((hour < T.hour) ||
  54. ((hour == T.hour) &&
  55. ((minute < T.minute) ||
  56. ((minute == T.minute) &&
  57. ((second < T.second) ||
  58. ((second == T.second) &&
  59. (millisecond < T.millisecond))))))))));
  60. }
  61. bool valid(void) {
  62. return ok;
  63. }
  64. } last(NULL);
  65. char* last_buffer = NULL;
  66. char buffer[5120];
  67. int count = 0;
  68. int next_lt_last = 0;
  69. while (fgets(buffer, sizeof(buffer), fp)) {
  70. if (!strncmp(begin, buffer, sizeof(begin) - 1)) {
  71. continue;
  72. }
  73. if (!last.valid()) {
  74. free(last_buffer);
  75. last_buffer = strdup(buffer);
  76. last.init(buffer);
  77. }
  78. timestamp next(buffer);
  79. if (next < last) {
  80. if (last_buffer) {
  81. fprintf(stderr, "<%s", last_buffer);
  82. }
  83. fprintf(stderr, ">%s", buffer);
  84. ++next_lt_last;
  85. }
  86. if (next.valid()) {
  87. free(last_buffer);
  88. last_buffer = strdup(buffer);
  89. last.init(buffer);
  90. }
  91. ++count;
  92. }
  93. free(last_buffer);
  94. pclose(fp);
  95. static const int max_ok = 2;
  96. // Allow few fails, happens with readers active
  97. fprintf(stderr, "%s: %d/%d out of order entries\n",
  98. (next_lt_last) ? ((next_lt_last <= max_ok) ? "WARNING" : "ERROR")
  99. : "INFO",
  100. next_lt_last, count);
  101. if (next_lt_last > max_ok) {
  102. fprintf(stderr, "EXPECT_GE(max_ok=%d, next_lt_last=%d)\n", max_ok,
  103. next_lt_last);
  104. }
  105. // sample statistically too small
  106. if (count < 100) {
  107. fprintf(stderr, "EXPECT_LT(100, count=%d)\n", count);
  108. }
  109. state.KeepRunning();
  110. }
  111. BENCHMARK(BM_logcat_sorted_order);
  112. BENCHMARK_MAIN();