loolwsd/Common.hpp        |    2 ++
 loolwsd/LOOLWebSocket.hpp |   28 ++++++++++++++++++++++++++++
 loolwsd/README.vars       |    7 +++++++
 3 files changed, 37 insertions(+)

New commits:
commit 3c2337cfc5ea2f4f11ea3b8bc8d93051fdf55705
Author: Pranav Kant <pran...@collabora.co.uk>
Date:   Mon Nov 21 15:36:05 2016 +0530

    loolwsd: Random latency simulator - delay, jitter in websockets
    
    Only enabled in debug build. Default values for base delay is
    10ms and jitter 5ms.
    
    Override using LOOL_WS_DELAY, LOOL_WS_JITTER env. variables
    
    Change-Id: I50502613075d6f73365623b93eb17748fe96fcc3

diff --git a/loolwsd/Common.hpp b/loolwsd/Common.hpp
index 814ab59..0223be8 100644
--- a/loolwsd/Common.hpp
+++ b/loolwsd/Common.hpp
@@ -22,6 +22,8 @@ constexpr long CHILD_TIMEOUT_MS = COMMAND_TIMEOUT_MS;
 constexpr int CHILD_REBALANCE_INTERVAL_MS = CHILD_TIMEOUT_MS / 10;
 constexpr int POLL_TIMEOUT_MS = COMMAND_TIMEOUT_MS / 10;
 constexpr int WS_SEND_TIMEOUT_MS = 1000;
+constexpr int WS_DELAY_MS = 10;
+constexpr int WS_JITTER_MS = 5;
 
 /// Pipe and Socket read buffer size.
 /// Should be large enough for ethernet packets
diff --git a/loolwsd/LOOLWebSocket.hpp b/loolwsd/LOOLWebSocket.hpp
index b4956ce..d78f625 100644
--- a/loolwsd/LOOLWebSocket.hpp
+++ b/loolwsd/LOOLWebSocket.hpp
@@ -10,7 +10,9 @@
 #ifndef INCLUDED_LOOLWEBSOCKET_HPP
 #define INCLUDED_LOOLWEBSOCKET_HPP
 
+#include <cstdlib>
 #include <mutex>
+#include <thread>
 
 #include <Poco/Net/WebSocket.h>
 
@@ -29,6 +31,24 @@ class LOOLWebSocket : public Poco::Net::WebSocket
 private:
     std::mutex _mutex;
 
+#ifdef ENABLE_DEBUG
+    std::chrono::milliseconds getWebSocketDelay()
+    {
+        unsigned long baseDelay = WS_DELAY_MS;
+        unsigned long jitter = WS_JITTER_MS;
+        if (std::getenv("LOOL_WS_DELAY"))
+        {
+            baseDelay = std::stoul(std::getenv("LOOL_WS_DELAY"));
+        }
+        if (std::getenv("LOOL_WS_JITTER"))
+        {
+            jitter = std::stoul(std::getenv("LOOL_WS_JITTER"));
+        }
+
+        return std::chrono::milliseconds(baseDelay + (std::rand() % jitter));
+    }
+#endif
+
 public:
     LOOLWebSocket(const Socket& socket) :
         Poco::Net::WebSocket(socket)
@@ -61,6 +81,10 @@ public:
     /// Should we also factor out the handling of non-final and continuation 
frames into this?
     int receiveFrame(char* buffer, const int length, int& flags)
     {
+#ifdef ENABLE_DEBUG
+        // Delay receiving the frame
+        std::this_thread::sleep_for(getWebSocketDelay());
+#endif
         // Timeout given is in microseconds.
         static const Poco::Timespan waitTime(POLL_TIMEOUT_MS * 1000);
 
@@ -87,6 +111,10 @@ public:
     /// Wrapper for Poco::Net::WebSocket::sendFrame() that handles large 
frames.
     int sendFrame(const char* buffer, const int length, const int flags = 
FRAME_TEXT)
     {
+#ifdef ENABLE_DEBUG
+        // Delay sending the frame
+        std::this_thread::sleep_for(getWebSocketDelay());
+#endif
         std::unique_lock<std::mutex> lock(_mutex);
 
         // Size after which messages will be sent preceded with
diff --git a/loolwsd/README.vars b/loolwsd/README.vars
index 305b9ea..b55672a 100644
--- a/loolwsd/README.vars
+++ b/loolwsd/README.vars
@@ -24,3 +24,10 @@ SLEEPKITFORDEBUGGER     <seconds to sleep>
         sleep <n> seconds in each kit process instance after forking,
         to allow a 'sudo gdb' session to attach and debug that
         process.
+
+LOOL_WS_DELAY          <base delay in milliseconds in websocket communication>
+       sleep atleast <n> seconds when sending and receiving websocket frames
+
+LOOL_WS_JITTER         <jitter in milliseconds for websocket communication>
+       Maximum Jitter in millseconds which is added to delay in
+       websocket communication.
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to