-
Notifications
You must be signed in to change notification settings - Fork 8
/
PlatisSolutionsBenchmarker.cpp
71 lines (61 loc) · 2.05 KB
/
PlatisSolutionsBenchmarker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "PlatisSolutionsBenchmarker.hpp"
#include <chrono>
#include <curl/curl.h> // You probably want to remove this after refactoring
#include <string>
namespace
{
const auto kUrl
= "https://platis.solutions/assets/images/smartcar_featured.jpg";
const auto kFilePath = "/dev/null";
} // namespace
namespace before
{
std::optional<std::chrono::milliseconds>
PlatisSolutionsBenchmarker::getResponseTime() const
{
const auto start = std::chrono::system_clock::now();
auto downloadSuccessful = false;
if (auto curl = curl_easy_init())
{
auto fp = fopen(kFilePath, "wb");
curl_easy_setopt(curl, CURLOPT_URL, kUrl);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
downloadSuccessful = curl_easy_perform(curl) == CURLE_OK;
fclose(fp);
curl_easy_cleanup(curl);
}
const auto end = std::chrono::system_clock::now();
if (downloadSuccessful)
{
const auto elapsedTime
= std::chrono::duration_cast<std::chrono::milliseconds>(end
- start);
return std::make_optional(elapsedTime);
}
return std::nullopt;
}
} // namespace before
namespace after
{
PlatisSolutionsBenchmarker::PlatisSolutionsBenchmarker(Downloader& downloader)
: mDownloader{downloader}
{
}
std::optional<std::chrono::milliseconds>
PlatisSolutionsBenchmarker::getResponseTime() const
{
const auto start = std::chrono::system_clock::now();
const auto downloadSuccessful = mDownloader.download(kUrl, kFilePath);
const auto end = std::chrono::system_clock::now();
if (downloadSuccessful)
{
const auto elapsedTime
= std::chrono::duration_cast<std::chrono::milliseconds>(end
- start);
return std::make_optional(elapsedTime);
}
return std::nullopt;
}
} // namespace after