The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. ThreadedAnalyticsDestination::ThreadedAnalyticsDestination (const String& threadName)
  16. : dispatcher (threadName, *this)
  17. {}
  18. ThreadedAnalyticsDestination::~ThreadedAnalyticsDestination()
  19. {
  20. // If you hit this assertion then the analytics thread has not been shut down
  21. // before this class is destroyed. Call stopAnalyticsThread() in your destructor!
  22. jassert (! dispatcher.isThreadRunning());
  23. }
  24. void ThreadedAnalyticsDestination::setBatchPeriod (int newBatchPeriodMilliseconds)
  25. {
  26. dispatcher.batchPeriodMilliseconds = newBatchPeriodMilliseconds;
  27. }
  28. void ThreadedAnalyticsDestination::logEvent (const AnalyticsEvent& event)
  29. {
  30. dispatcher.addToQueue (event);
  31. }
  32. void ThreadedAnalyticsDestination::startAnalyticsThread (int initialBatchPeriodMilliseconds)
  33. {
  34. setBatchPeriod (initialBatchPeriodMilliseconds);
  35. dispatcher.startThread();
  36. }
  37. void ThreadedAnalyticsDestination::stopAnalyticsThread (int timeout)
  38. {
  39. dispatcher.signalThreadShouldExit();
  40. stopLoggingEvents();
  41. dispatcher.stopThread (timeout);
  42. if (dispatcher.eventQueue.size() > 0)
  43. saveUnloggedEvents (dispatcher.eventQueue);
  44. }
  45. ThreadedAnalyticsDestination::EventDispatcher::EventDispatcher (const String& dispatcherThreadName,
  46. ThreadedAnalyticsDestination& destination)
  47. : Thread (dispatcherThreadName),
  48. parent (destination)
  49. {}
  50. void ThreadedAnalyticsDestination::EventDispatcher::run()
  51. {
  52. // We may have inserted some events into the queue (on the message thread)
  53. // before this thread has started, so make sure the old events are at the
  54. // front of the queue.
  55. {
  56. std::deque<AnalyticsEvent> restoredEventQueue;
  57. parent.restoreUnloggedEvents (restoredEventQueue);
  58. const ScopedLock lock (queueAccess);
  59. for (auto rit = restoredEventQueue.rbegin(); rit != restoredEventQueue.rend(); ++rit)
  60. eventQueue.push_front (*rit);
  61. }
  62. const int maxBatchSize = parent.getMaximumBatchSize();
  63. while (! threadShouldExit())
  64. {
  65. {
  66. const auto numEventsInBatch = eventsToSend.size();
  67. const auto freeBatchCapacity = maxBatchSize - numEventsInBatch;
  68. if (freeBatchCapacity > 0)
  69. {
  70. const auto numNewEvents = (int) eventQueue.size() - numEventsInBatch;
  71. if (numNewEvents > 0)
  72. {
  73. const ScopedLock lock (queueAccess);
  74. const auto numEventsToAdd = jmin (numNewEvents, freeBatchCapacity);
  75. const auto newBatchSize = numEventsInBatch + numEventsToAdd;
  76. for (auto i = numEventsInBatch; i < newBatchSize; ++i)
  77. eventsToSend.add (eventQueue[(size_t) i]);
  78. }
  79. }
  80. }
  81. const auto submissionTime = Time::getMillisecondCounter();
  82. if (! eventsToSend.isEmpty())
  83. {
  84. if (parent.logBatchedEvents (eventsToSend))
  85. {
  86. const ScopedLock lock (queueAccess);
  87. for (auto i = 0; i < eventsToSend.size(); ++i)
  88. eventQueue.pop_front();
  89. eventsToSend.clearQuick();
  90. }
  91. }
  92. while (Time::getMillisecondCounter() - submissionTime < (uint32) batchPeriodMilliseconds.get())
  93. {
  94. if (threadShouldExit())
  95. return;
  96. Thread::sleep (100);
  97. }
  98. }
  99. }
  100. void ThreadedAnalyticsDestination::EventDispatcher::addToQueue (const AnalyticsEvent& event)
  101. {
  102. const ScopedLock lock (queueAccess);
  103. eventQueue.push_back (event);
  104. }
  105. //==============================================================================
  106. //==============================================================================
  107. #if JUCE_UNIT_TESTS
  108. namespace DestinationTestHelpers
  109. {
  110. //==============================================================================
  111. struct BasicDestination : public ThreadedAnalyticsDestination
  112. {
  113. BasicDestination (std::deque<AnalyticsEvent>& loggedEvents,
  114. std::deque<AnalyticsEvent>& unloggedEvents)
  115. : ThreadedAnalyticsDestination ("ThreadedAnalyticsDestinationTest"),
  116. loggedEventQueue (loggedEvents),
  117. unloggedEventStore (unloggedEvents)
  118. {
  119. startAnalyticsThread (20);
  120. }
  121. ~BasicDestination() override
  122. {
  123. stopAnalyticsThread (1000);
  124. }
  125. int getMaximumBatchSize() override
  126. {
  127. return 5;
  128. }
  129. void saveUnloggedEvents (const std::deque<AnalyticsEvent>& eventsToSave) override
  130. {
  131. unloggedEventStore = eventsToSave;
  132. }
  133. void restoreUnloggedEvents (std::deque<AnalyticsEvent>& restoredEventQueue) override
  134. {
  135. restoredEventQueue = unloggedEventStore;
  136. }
  137. bool logBatchedEvents (const Array<AnalyticsEvent>& events) override
  138. {
  139. jassert (events.size() <= getMaximumBatchSize());
  140. if (loggingIsEnabled)
  141. {
  142. const ScopedLock lock (eventQueueChanging);
  143. for (auto& event : events)
  144. loggedEventQueue.push_back (event);
  145. return true;
  146. }
  147. return false;
  148. }
  149. void stopLoggingEvents() override {}
  150. void setLoggingEnabled (bool shouldLogEvents)
  151. {
  152. loggingIsEnabled = shouldLogEvents;
  153. }
  154. std::deque<AnalyticsEvent>& loggedEventQueue;
  155. std::deque<AnalyticsEvent>& unloggedEventStore;
  156. bool loggingIsEnabled = true;
  157. CriticalSection eventQueueChanging;
  158. };
  159. }
  160. //==============================================================================
  161. struct ThreadedAnalyticsDestinationTests : public UnitTest
  162. {
  163. ThreadedAnalyticsDestinationTests()
  164. : UnitTest ("ThreadedAnalyticsDestination", UnitTestCategories::analytics)
  165. {}
  166. void compareEventQueues (const std::deque<AnalyticsDestination::AnalyticsEvent>& a,
  167. const std::deque<AnalyticsDestination::AnalyticsEvent>& b)
  168. {
  169. const auto numEntries = a.size();
  170. expectEquals ((int) b.size(), (int) numEntries);
  171. for (size_t i = 0; i < numEntries; ++i)
  172. {
  173. expectEquals (a[i].name, b[i].name);
  174. expect (a[i].timestamp == b[i].timestamp);
  175. }
  176. }
  177. void runTest() override
  178. {
  179. std::deque<AnalyticsDestination::AnalyticsEvent> testEvents;
  180. for (int i = 0; i < 7; ++i)
  181. testEvents.push_back ({ String (i), 0, Time::getMillisecondCounter(), {}, "TestUser", {} });
  182. std::deque<AnalyticsDestination::AnalyticsEvent> loggedEvents, unloggedEvents;
  183. beginTest ("New events");
  184. {
  185. DestinationTestHelpers::BasicDestination destination (loggedEvents, unloggedEvents);
  186. for (auto& event : testEvents)
  187. destination.logEvent (event);
  188. size_t waitTime = 0, numLoggedEvents = 0;
  189. while (numLoggedEvents < testEvents.size())
  190. {
  191. if (waitTime > 4000)
  192. {
  193. expect (waitTime < 4000);
  194. break;
  195. }
  196. Thread::sleep (40);
  197. waitTime += 40;
  198. const ScopedLock lock (destination.eventQueueChanging);
  199. numLoggedEvents = loggedEvents.size();
  200. }
  201. }
  202. compareEventQueues (loggedEvents, testEvents);
  203. expect (unloggedEvents.size() == 0);
  204. loggedEvents.clear();
  205. beginTest ("Unlogged events");
  206. {
  207. DestinationTestHelpers::BasicDestination destination (loggedEvents, unloggedEvents);
  208. destination.setLoggingEnabled (false);
  209. for (auto& event : testEvents)
  210. destination.logEvent (event);
  211. }
  212. compareEventQueues (unloggedEvents, testEvents);
  213. expect (loggedEvents.size() == 0);
  214. }
  215. };
  216. static ThreadedAnalyticsDestinationTests threadedAnalyticsDestinationTests;
  217. #endif
  218. } // namespace juce