Bug 1520241 - Avoid slow sleeps on Windows in TestSPSCQueue.exe r=padenot
authorDavid Major <dmajor@mozilla.com>
Wed, 16 Jan 2019 12:04:13 +0000
changeset 511205 5876723df7ba626af3257d55fb79957ebfaec7c9
parent 511204 d50d152c74c71a2581ecfb373f9efb8255aef4a2
child 511206 a41a7e9cdf4e84446a85f3d0fa5c8c06a4e1a92a
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1520241
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1520241 - Avoid slow sleeps on Windows in TestSPSCQueue.exe r=padenot Differential Revision: https://phabricator.services.mozilla.com/D16583
mfbt/tests/TestSPSCQueue.cpp
--- a/mfbt/tests/TestSPSCQueue.cpp
+++ b/mfbt/tests/TestSPSCQueue.cpp
@@ -8,16 +8,20 @@
 #include "mozilla/PodOperations.h"
 #include <vector>
 #include <iostream>
 #include <thread>
 #include <chrono>
 #include <memory>
 #include <string>
 
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
 using namespace mozilla;
 
 /* Generate a monotonically increasing sequence of numbers. */
 template <typename T>
 class SequenceGenerator {
  public:
   SequenceGenerator() {}
   void Get(T* aElements, size_t aCount) {
@@ -70,42 +74,53 @@ void TestRing(int capacity) {
     MOZ_RELEASE_ASSERT(rv == BLOCK_SIZE);
     PodZero(seq.get(), BLOCK_SIZE);
     rv = buf.Dequeue(seq.get(), BLOCK_SIZE);
     MOZ_RELEASE_ASSERT(rv == BLOCK_SIZE);
     checker.Check(seq.get(), BLOCK_SIZE);
   }
 }
 
+void Delay() {
+  // On Windows, the timer resolution is so bad that, even if we used
+  // `timeBeginPeriod(1)`, any nonzero sleep from the test's inner loops
+  // would make this program take far too long.
+#ifdef _WIN32
+  Sleep(0);
+#else
+  std::this_thread::sleep_for(std::chrono::microseconds(10));
+#endif
+}
+
 template <typename T>
 void TestRingMultiThread(int capacity) {
   SPSCQueue<T> buf(capacity);
   SequenceVerifier<T> checker;
   std::unique_ptr<T[]> outBuffer(new T[capacity]);
 
   std::thread t([&buf, capacity] {
     int iterations = 1002;
     std::unique_ptr<T[]> inBuffer(new T[capacity]);
     SequenceGenerator<T> gen;
 
     while (iterations--) {
-      std::this_thread::sleep_for(std::chrono::microseconds(10));
+      Delay();
       gen.Get(inBuffer.get(), BLOCK_SIZE);
       int rv = buf.Enqueue(inBuffer.get(), BLOCK_SIZE);
       MOZ_RELEASE_ASSERT(rv <= BLOCK_SIZE);
       if (rv != BLOCK_SIZE) {
         gen.Rewind(BLOCK_SIZE - rv);
       }
     }
   });
 
   int remaining = 1002;
 
   while (remaining--) {
-    std::this_thread::sleep_for(std::chrono::microseconds(10));
+    Delay();
     int rv = buf.Dequeue(outBuffer.get(), BLOCK_SIZE);
     MOZ_RELEASE_ASSERT(rv <= BLOCK_SIZE);
     checker.Check(outBuffer.get(), rv);
   }
 
   t.join();
 }