2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-26 09:49:07 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2016-03-02 00:47:13 -06:00
|
|
|
#include <cstdlib>
|
2015-04-14 09:50:38 -05:00
|
|
|
#include <cstring>
|
2015-03-26 09:49:07 -05:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
#include <Poco/Condition.h>
|
|
|
|
#include <Poco/Mutex.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/Net/SocketStream.h>
|
|
|
|
#include <Poco/Net/StreamSocket.h>
|
|
|
|
#include <Poco/Net/TCPServer.h>
|
|
|
|
#include <Poco/Net/TCPServerConnection.h>
|
|
|
|
#include <Poco/Net/TCPServerConnectionFactory.h>
|
|
|
|
#include <Poco/Net/WebSocket.h>
|
|
|
|
#include <Poco/Process.h>
|
2015-05-28 05:23:25 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2015-03-26 09:49:07 -05:00
|
|
|
#include <Poco/Thread.h>
|
|
|
|
#include <Poco/Timespan.h>
|
|
|
|
#include <Poco/Timestamp.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Poco/Util/HelpFormatter.h>
|
|
|
|
#include <Poco/Util/Option.h>
|
|
|
|
#include <Poco/Util/OptionSet.h>
|
|
|
|
|
2015-12-27 16:23:43 -06:00
|
|
|
#include "Common.hpp"
|
2015-03-26 09:49:07 -05:00
|
|
|
#include "LoadTest.hpp"
|
|
|
|
#include "LOOLProtocol.hpp"
|
|
|
|
#include "Util.hpp"
|
|
|
|
|
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
|
|
|
using Poco::Condition;
|
|
|
|
using Poco::Mutex;
|
|
|
|
using Poco::Net::HTTPClientSession;
|
|
|
|
using Poco::Net::HTTPRequest;
|
|
|
|
using Poco::Net::HTTPResponse;
|
|
|
|
using Poco::Net::Socket;
|
|
|
|
using Poco::Net::SocketOutputStream;
|
|
|
|
using Poco::Net::StreamSocket;
|
|
|
|
using Poco::Net::TCPServer;
|
|
|
|
using Poco::Net::TCPServerConnection;
|
|
|
|
using Poco::Net::WebSocket;
|
|
|
|
using Poco::Net::WebSocketException;
|
|
|
|
using Poco::Runnable;
|
2015-05-28 05:23:25 -05:00
|
|
|
using Poco::StringTokenizer;
|
2015-03-26 09:49:07 -05:00
|
|
|
using Poco::Thread;
|
|
|
|
using Poco::Timespan;
|
|
|
|
using Poco::Timestamp;
|
|
|
|
using Poco::URI;
|
|
|
|
using Poco::Util::Application;
|
|
|
|
using Poco::Util::HelpFormatter;
|
|
|
|
using Poco::Util::Option;
|
|
|
|
using Poco::Util::OptionSet;
|
|
|
|
|
|
|
|
class Output: public Runnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Output(WebSocket& ws, Condition& cond, Mutex& mutex) :
|
|
|
|
_ws(ws),
|
|
|
|
_cond(cond),
|
|
|
|
_mutex(mutex),
|
|
|
|
_type(LOK_DOCTYPE_OTHER),
|
|
|
|
_width(0),
|
|
|
|
_height(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
int n;
|
2015-06-09 09:13:13 -05:00
|
|
|
int tileCount = 0;
|
2015-03-26 09:49:07 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[100000];
|
|
|
|
n = _ws.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-02-23 12:03:43 -06:00
|
|
|
if (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE)
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
2015-05-13 05:16:10 -05:00
|
|
|
#if 0
|
2015-12-27 14:15:57 -06:00
|
|
|
Log::debug() << "Client got " << n << " bytes: "
|
|
|
|
<< getAbbreviatedMessage(buffer, n) << Log::end;
|
2015-05-13 05:16:10 -05:00
|
|
|
#endif
|
2015-04-14 09:50:38 -05:00
|
|
|
std::string response = getFirstLine(buffer, n);
|
2015-05-28 05:23:25 -05:00
|
|
|
StringTokenizer tokens(response, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
int size;
|
|
|
|
if (tokens.count() == 2 && tokens[0] == "nextmessage:" && getTokenInteger(tokens[1], "size", size) && size > 0)
|
|
|
|
{
|
|
|
|
char largeBuffer[size];
|
|
|
|
|
|
|
|
n = _ws.receiveFrame(largeBuffer, size, flags);
|
2015-05-28 07:29:04 -05:00
|
|
|
|
|
|
|
#if 0
|
2015-12-27 14:15:57 -06:00
|
|
|
Log::debug() << "Client got " << n << " bytes: "
|
|
|
|
<< getAbbreviatedMessage(largeBuffer, n) << Log::end;
|
2015-05-28 07:29:04 -05:00
|
|
|
#endif
|
2016-02-23 12:03:43 -06:00
|
|
|
if (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE)
|
|
|
|
response = getFirstLine(largeBuffer, n);
|
2015-05-28 05:23:25 -05:00
|
|
|
}
|
2016-01-06 11:00:44 -06:00
|
|
|
else if (tokens[0] == "loolclient")
|
|
|
|
{
|
|
|
|
const auto versionTuple = ParseVersion(tokens[1]);
|
|
|
|
if (std::get<0>(versionTuple) != ProtocolMajorVersionNumber ||
|
|
|
|
std::get<1>(versionTuple) != ProtocolMinorVersionNumber)
|
|
|
|
{
|
|
|
|
const std::string error = "error: cmd=loolclient kind=badversion";
|
|
|
|
_ws.sendFrame(error.c_str(), error.size());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto version = "loolserver " + GetProtocolVersion();
|
|
|
|
_ws.sendFrame(version.c_str(), version.size());
|
|
|
|
}
|
2015-03-26 09:49:07 -05:00
|
|
|
if (response.find("status:") == 0)
|
|
|
|
{
|
|
|
|
parseStatus(response, _type, _numParts, _currentPart, _width, _height);
|
|
|
|
_cond.signal();
|
|
|
|
}
|
2015-06-09 09:13:13 -05:00
|
|
|
else if (response.find("tile:") == 0)
|
|
|
|
{
|
|
|
|
tileCount++;
|
|
|
|
}
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
|
|
|
|
}
|
|
|
|
catch (WebSocketException& exc)
|
|
|
|
{
|
2015-06-09 10:22:09 -05:00
|
|
|
Application::instance().logger().error("WebSocketException: " + exc.message());
|
2015-03-26 09:49:07 -05:00
|
|
|
_ws.close();
|
|
|
|
}
|
2015-12-27 14:15:57 -06:00
|
|
|
Log::debug() << "Got " << tileCount << " tiles" << Log::end;
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocket& _ws;
|
|
|
|
Condition& _cond;
|
|
|
|
Mutex& _mutex;
|
|
|
|
LibreOfficeKitDocumentType _type;
|
|
|
|
|
|
|
|
int _numParts;
|
|
|
|
int _currentPart;
|
|
|
|
int _width;
|
|
|
|
int _height;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Client: public Runnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Client(LoadTest& app) :
|
2015-05-28 08:16:02 -05:00
|
|
|
_app(app),
|
|
|
|
_g(_rd())
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
|
|
|
std::vector<std::string> uris(_app.getDocList());
|
2015-05-28 08:16:02 -05:00
|
|
|
std::shuffle(uris.begin(), uris.end(), _g);
|
2015-03-26 09:49:07 -05:00
|
|
|
if (uris.size() > _app.getNumDocsPerClient())
|
|
|
|
uris.resize(_app.getNumDocsPerClient());
|
2015-03-27 11:25:06 -05:00
|
|
|
while (!clientDurationExceeded())
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
2015-05-28 08:16:02 -05:00
|
|
|
std::shuffle(uris.begin(), uris.end(), _g);
|
2015-03-26 09:49:07 -05:00
|
|
|
for (auto i : uris)
|
|
|
|
{
|
2015-03-27 11:25:06 -05:00
|
|
|
if (clientDurationExceeded())
|
2015-03-26 09:49:07 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
testDocument(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-03-27 11:25:06 -05:00
|
|
|
bool clientDurationExceeded()
|
|
|
|
{
|
|
|
|
return _clientStartTimestamp.isElapsed(_app.getDuration() * Timespan::HOURS);
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
void testDocument(const std::string& document)
|
|
|
|
{
|
2015-03-27 11:25:06 -05:00
|
|
|
Timestamp documentStartTimestamp;
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
URI uri(_app.getURL());
|
|
|
|
|
2015-12-27 14:15:57 -06:00
|
|
|
Log::debug() << "Starting client for '" << document << "'" << Log::end;
|
2015-05-13 05:15:00 -05:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
HTTPClientSession cs(uri.getHost(), uri.getPort());
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
|
|
|
|
HTTPResponse response;
|
|
|
|
WebSocket ws(cs, request, response);
|
|
|
|
|
|
|
|
Condition cond;
|
|
|
|
Mutex mutex;
|
|
|
|
|
|
|
|
Thread thread;
|
|
|
|
Output output(ws, cond, mutex);
|
|
|
|
|
|
|
|
thread.start(output);
|
|
|
|
|
2016-01-06 11:00:44 -06:00
|
|
|
sendTextFrame(ws, "loolclient " + GetProtocolVersion());
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
if (document[0] == '/')
|
|
|
|
sendTextFrame(ws, "load " + document);
|
|
|
|
else
|
2015-05-13 06:51:27 -05:00
|
|
|
sendTextFrame(ws, "load url=" + document);
|
2015-03-26 09:49:07 -05:00
|
|
|
|
|
|
|
sendTextFrame(ws, "status");
|
|
|
|
|
2015-03-27 11:25:06 -05:00
|
|
|
// Wait for the "status:" message
|
2015-03-26 09:49:07 -05:00
|
|
|
mutex.lock();
|
|
|
|
cond.wait(mutex);
|
|
|
|
mutex.unlock();
|
|
|
|
|
2015-12-27 14:15:57 -06:00
|
|
|
Log::debug() << "Got status, size=" << output._width << "x" << output._height << Log::end;
|
2015-03-26 09:49:07 -05:00
|
|
|
|
2015-05-13 05:15:00 -05:00
|
|
|
int y = 0;
|
|
|
|
const int DOCTILESIZE = 5000;
|
2015-04-20 09:42:47 -05:00
|
|
|
|
2015-05-28 08:16:02 -05:00
|
|
|
std::uniform_int_distribution<> dis(0, 20);
|
|
|
|
int extra = dis(_g);
|
|
|
|
|
2015-06-09 09:13:13 -05:00
|
|
|
int requestCount = 0;
|
|
|
|
|
2015-03-27 11:25:06 -05:00
|
|
|
// Exercise the server with this document for some minutes
|
2015-05-28 08:16:02 -05:00
|
|
|
while (!documentStartTimestamp.isElapsed((20 + extra) * Timespan::SECONDS) && !clientDurationExceeded())
|
2015-03-27 11:25:06 -05:00
|
|
|
{
|
2015-05-13 05:15:00 -05:00
|
|
|
int x = 0;
|
2015-05-28 08:16:02 -05:00
|
|
|
while (!documentStartTimestamp.isElapsed((20 + extra) * Timespan::SECONDS) && !clientDurationExceeded())
|
2015-05-13 05:15:00 -05:00
|
|
|
{
|
|
|
|
sendTextFrame(ws,
|
2015-05-13 05:15:22 -05:00
|
|
|
"tile part=0 width=256 height=256 "
|
2015-05-13 05:15:00 -05:00
|
|
|
"tileposx=" + std::to_string(x * DOCTILESIZE) + " "
|
|
|
|
"tileposy=" + std::to_string(y * DOCTILESIZE) + " "
|
|
|
|
"tilewidth=" + std::to_string(DOCTILESIZE) + " "
|
|
|
|
"tileheight=" + std::to_string(DOCTILESIZE));
|
2015-06-09 09:13:13 -05:00
|
|
|
requestCount++;
|
2015-05-13 05:15:00 -05:00
|
|
|
x = ((x + 1) % ((output._width-1)/DOCTILESIZE + 1));
|
|
|
|
if (x == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
y = ((y + 1) % ((output._height-1)/DOCTILESIZE + 1));
|
2015-06-05 08:12:06 -05:00
|
|
|
Thread::sleep(50);
|
2015-03-27 11:25:06 -05:00
|
|
|
}
|
2015-06-05 08:12:06 -05:00
|
|
|
sendTextFrame(ws, "canceltiles");
|
2015-03-26 09:49:07 -05:00
|
|
|
|
|
|
|
Thread::sleep(10000);
|
|
|
|
|
2015-12-27 14:15:57 -06:00
|
|
|
Log::debug() << "Sent " << requestCount << " tile requests, shutting down client for '" << document << "'" << Log::end;
|
2015-03-26 09:49:07 -05:00
|
|
|
|
|
|
|
ws.shutdown();
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendTextFrame(WebSocket& ws, const std::string& s)
|
|
|
|
{
|
|
|
|
ws.sendFrame(s.data(), s.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadTest& _app;
|
2015-03-27 11:25:06 -05:00
|
|
|
Timestamp _clientStartTimestamp;
|
2015-05-28 08:16:02 -05:00
|
|
|
|
|
|
|
std::random_device _rd;
|
|
|
|
std::mt19937 _g;
|
2015-03-26 09:49:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
LoadTest::LoadTest() :
|
|
|
|
_numClients(20),
|
|
|
|
_numDocsPerClient(500),
|
|
|
|
_duration(6),
|
2015-12-27 16:23:43 -06:00
|
|
|
_url("http://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER) + "/ws")
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadTest::~LoadTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LoadTest::getNumDocsPerClient() const
|
|
|
|
{
|
|
|
|
return _numDocsPerClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LoadTest::getDuration() const
|
|
|
|
{
|
|
|
|
return _duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LoadTest::getURL() const
|
|
|
|
{
|
|
|
|
return _url;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> LoadTest::getDocList() const
|
|
|
|
{
|
|
|
|
return _docList;
|
|
|
|
}
|
|
|
|
|
2015-10-28 04:55:03 -05:00
|
|
|
void LoadTest::defineOptions(OptionSet& optionSet)
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
2015-10-28 04:55:03 -05:00
|
|
|
Application::defineOptions(optionSet);
|
|
|
|
|
|
|
|
optionSet.addOption(Option("help", "", "Display help information on command line arguments.")
|
|
|
|
.required(false)
|
|
|
|
.repeatable(false));
|
|
|
|
|
|
|
|
optionSet.addOption(Option("doclist", "", "file containing URIs or pathnames of documents to load, - for stdin")
|
|
|
|
.required(true)
|
|
|
|
.repeatable(false)
|
|
|
|
.argument("file"));
|
|
|
|
|
|
|
|
optionSet.addOption(Option("numclients", "", "number of simultaneous clients to simulate")
|
|
|
|
.required(false)
|
|
|
|
.repeatable(false)
|
|
|
|
.argument("number"));
|
|
|
|
|
|
|
|
optionSet.addOption(Option("numdocs", "", "number of sequential documents per client")
|
|
|
|
.required(false)
|
|
|
|
.repeatable(false)
|
|
|
|
.argument("number"));
|
|
|
|
|
|
|
|
optionSet.addOption(Option("duration", "", "duration in hours")
|
|
|
|
.required(false)
|
|
|
|
.repeatable(false)
|
|
|
|
.argument("hours"));
|
|
|
|
|
|
|
|
optionSet.addOption(Option("server", "", "URI of LOOL server")
|
|
|
|
.required(false)
|
|
|
|
.repeatable(false)
|
|
|
|
.argument("uri"));
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
|
|
|
|
2015-10-28 04:55:03 -05:00
|
|
|
void LoadTest::handleOption(const std::string& optionName, const std::string& value)
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
2015-10-28 04:55:03 -05:00
|
|
|
Application::handleOption(optionName, value);
|
2015-03-26 09:49:07 -05:00
|
|
|
|
2015-10-28 04:55:03 -05:00
|
|
|
if (optionName == "help")
|
2015-03-27 11:25:06 -05:00
|
|
|
{
|
|
|
|
displayHelp();
|
2016-03-02 00:47:13 -06:00
|
|
|
std::exit(Application::EXIT_OK);
|
2015-03-27 11:25:06 -05:00
|
|
|
}
|
2015-10-28 04:55:03 -05:00
|
|
|
else if (optionName == "doclist")
|
2015-03-26 09:49:07 -05:00
|
|
|
_docList = readDocList(value);
|
2015-10-28 04:55:03 -05:00
|
|
|
else if (optionName == "numclients")
|
2015-03-26 09:49:07 -05:00
|
|
|
_numClients = std::stoi(value);
|
2015-10-28 04:55:03 -05:00
|
|
|
else if (optionName == "numdocs")
|
2015-03-26 09:49:07 -05:00
|
|
|
_numDocsPerClient = std::stoi(value);
|
2015-10-28 04:55:03 -05:00
|
|
|
else if (optionName == "duration")
|
2015-03-26 09:49:07 -05:00
|
|
|
_duration = std::stoi(value);
|
2015-10-28 04:55:03 -05:00
|
|
|
else if (optionName == "url")
|
2015-03-26 09:49:07 -05:00
|
|
|
_url = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadTest::displayHelp()
|
|
|
|
{
|
|
|
|
HelpFormatter helpFormatter(options());
|
|
|
|
helpFormatter.setCommand(commandName());
|
|
|
|
helpFormatter.setUsage("OPTIONS");
|
|
|
|
helpFormatter.setHeader("LibreOffice On-Line WebSocket server load test.");
|
|
|
|
helpFormatter.format(std::cout);
|
|
|
|
}
|
|
|
|
|
2015-10-16 10:45:03 -05:00
|
|
|
int LoadTest::main(const std::vector<std::string>& /*args*/)
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
|
|
|
Thread *clients[_numClients];
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < _numClients; i++)
|
|
|
|
{
|
|
|
|
clients[i] = new Thread();
|
|
|
|
clients[i]->start(*(new Client(*this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < _numClients; i++)
|
|
|
|
{
|
|
|
|
clients[i]->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Application::EXIT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> LoadTest::readDocList(const std::string& filename)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
std::ifstream infile;
|
|
|
|
std::istream *input;
|
|
|
|
if (filename == "-")
|
|
|
|
input = &std::cin;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
infile.open(filename);
|
|
|
|
input = &infile;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!input->eof())
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
*input >> std::ws;
|
|
|
|
if (input->eof())
|
|
|
|
break;
|
|
|
|
*input >> s;
|
|
|
|
result.push_back(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename == "-")
|
|
|
|
infile.close();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
POCO_APP_MAIN(LoadTest)
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|