DDFileLoggerPerformanceTests.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. @import XCTest;
  16. #import <CocoaLumberjack/DDFileLogger+Buffering.h>
  17. #import <CocoaLumberjack/DDLogMacros.h>
  18. #import "DDSampleFileManager.h"
  19. // Debug levels: off, error, warn, info, verbose
  20. static const DDLogLevel ddLogLevel = DDLogLevelWarning; // CONST
  21. @interface DDFileLoggerPerformanceTests : XCTestCase
  22. @property (nonatomic) DDFileLogger *logger;
  23. @end
  24. @implementation DDFileLoggerPerformanceTests
  25. - (void)setUp {
  26. _logger = [[DDFileLogger alloc] initWithLogFileManager:[DDSampleFileManager new]];
  27. [DDLog addLogger:_logger];
  28. }
  29. - (void)tearDown {
  30. [DDLog removeAllLoggers];
  31. }
  32. - (void)testPerformanceNotPrinted {
  33. [self measureBlock:^{
  34. for (NSUInteger i = 0; i < 1000; i++) {
  35. // Log statements that will not be executed due to log level
  36. DDLogVerbose(@"testPerformanceNotPrinted - %lu", (unsigned long)i);
  37. }
  38. }];
  39. }
  40. - (void)testPerformanceAsyncPrint {
  41. [self measureBlock:^{
  42. for (NSUInteger i = 0; i < 1000; i++) {
  43. // Log statements that will be executed asynchronously
  44. DDLogWarn(@"testPerformanceAsyncPrint - %lu", (unsigned long)i);
  45. }
  46. }];
  47. }
  48. - (void)testPerformanceAsyncPrintBuffered {
  49. [DDLog removeAllLoggers];
  50. [DDLog addLogger:[_logger wrapWithBuffer]];
  51. [self measureBlock:^{
  52. for (NSUInteger i = 0; i < 1000; i++) {
  53. // Log statements that will be executed asynchronously
  54. DDLogWarn(@"testPerformanceAsyncPrintBuffered - %lu", (unsigned long)i);
  55. }
  56. }];
  57. }
  58. - (void)testPerformanceSyncPrint {
  59. [self measureBlock:^{
  60. for (NSUInteger i = 0; i < 1000; i++) {
  61. // Log statements that will be executed synchronously
  62. DDLogError(@"testPerformanceSyncPrint - %lu", (unsigned long)i);
  63. }
  64. }];
  65. }
  66. - (void)testPerformanceSyncPrintBuffered {
  67. [DDLog removeAllLoggers];
  68. [DDLog addLogger:[_logger wrapWithBuffer]];
  69. [self measureBlock:^{
  70. for (NSUInteger i = 0; i < 1000; i++) {
  71. // Log statements that will be executed asynchronously
  72. DDLogError(@"testPerformanceSyncPrintBuffered - %lu", (unsigned long)i);
  73. }
  74. }];
  75. }
  76. - (void)testPerformancePrintEvenSpread {
  77. [self measureBlock:^{
  78. // Even Spread:
  79. //
  80. // 25% - Not executed due to log level
  81. // 50% - Executed asynchronously
  82. // 25% - Executed synchronously
  83. NSString *fmt = @"testPerformancePrintEvenSpread - %lu";
  84. for (NSUInteger i = 0; i < 250; i++) {
  85. DDLogError(fmt, (unsigned long)i);
  86. }
  87. for (NSUInteger i = 0; i < 250; i++) {
  88. DDLogWarn(fmt, (unsigned long)i);
  89. }
  90. for (NSUInteger i = 0; i < 250; i++) {
  91. DDLogInfo(fmt, (unsigned long)i);
  92. }
  93. for (NSUInteger i = 0; i < 250; i++) {
  94. DDLogVerbose(fmt, (unsigned long)i);
  95. }
  96. }];
  97. }
  98. - (void)testPerformancePrintEvenSpreadBuffered {
  99. [DDLog removeAllLoggers];
  100. [DDLog addLogger:[_logger wrapWithBuffer]];
  101. [self measureBlock:^{
  102. // Even Spread:
  103. //
  104. // 25% - Not executed due to log level
  105. // 50% - Executed asynchronously
  106. // 25% - Executed synchronously
  107. NSString *fmt = @"testPerformancePrintEvenSpreadBuffered - %lu";
  108. for (NSUInteger i = 0; i < 250; i++) {
  109. DDLogError(fmt, (unsigned long)i);
  110. }
  111. for (NSUInteger i = 0; i < 250; i++) {
  112. DDLogWarn(fmt, (unsigned long)i);
  113. }
  114. for (NSUInteger i = 0; i < 250; i++) {
  115. DDLogInfo(fmt, (unsigned long)i);
  116. }
  117. for (NSUInteger i = 0; i < 250; i++) {
  118. DDLogVerbose(fmt, (unsigned long)i);
  119. }
  120. }];
  121. }
  122. - (void)testPerformanceCustomSpread {
  123. [self measureBlock:^{
  124. // Custom Spread
  125. NSString *fmt = @"testPerformanceCustomSpread - %lu";
  126. for (NSUInteger i = 0; i < 900; i++) {
  127. DDLogError(fmt, (unsigned long)i);
  128. }
  129. for (NSUInteger i = 0; i < 100; i++) {
  130. DDLogWarn(fmt, (unsigned long)i);
  131. }
  132. for (NSUInteger i = 0; i < 100; i++) {
  133. DDLogInfo(fmt, (unsigned long)i);
  134. }
  135. for (NSUInteger i = 0; i < 100; i++) {
  136. DDLogVerbose(fmt, (unsigned long)i);
  137. }
  138. }];
  139. }
  140. - (void)testPerformanceCustomSpreadBuffered {
  141. [DDLog removeAllLoggers];
  142. [DDLog addLogger:[_logger wrapWithBuffer]];
  143. [self measureBlock:^{
  144. // Custom Spread
  145. NSString *fmt = @"testPerformanceCustomSpreadBuffered - %lu";
  146. for (NSUInteger i = 0; i < 900; i++) {
  147. DDLogError(fmt, (unsigned long)i);
  148. }
  149. for (NSUInteger i = 0; i < 100; i++) {
  150. DDLogWarn(fmt, (unsigned long)i);
  151. }
  152. for (NSUInteger i = 0; i < 100; i++) {
  153. DDLogInfo(fmt, (unsigned long)i);
  154. }
  155. for (NSUInteger i = 0; i < 100; i++) {
  156. DDLogVerbose(fmt, (unsigned long)i);
  157. }
  158. }];
  159. }
  160. @end