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.

281 lines
8.7KB

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