This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbenchmark.c
135 lines (109 loc) · 2.92 KB
/
benchmark.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* apt-spy (c) Steven Holmes, 2003.
*
* This software is licensed as detailed in the COPYRIGHT file
*/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include "include/parse.h"
#include "include/benchmark.h"
#include "include/protocols.h"
#include "include/global.h"
/*
* It is safer to keep track of the total amount of data read ourselves.
* This fixes a bug appearing when libcurl would not reset its own byte counter
*/
/* static size_t total_bytes = 0; */
static int total_bytes = 0;
/**
*
*
* @param current
* @param proxy
* @param timeout
* @param file
*
* @return
*/
int benchmark(server_t *current, char *proxy, int timeout, char *file)
{
CURL *curl;
printf("\nSERVER:\t%s\n", current->hostname);
curl_global_init (CURL_GLOBAL_ALL);
/* We use libcurl - here we setup some global options */
curl = curl_easy_init();
if (curl == NULL)
return 1;
/* Set proxy server to use */
if (proxy != NULL)
if (curl_easy_setopt(curl, CURLOPT_PROXY, proxy) != 0)
return 1;
/* Connection timeout */
if (curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, (long) timeout) != 0)
return 1;
/* Total timeout */
if (curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long) timeout) != 0)
return 1;
/* Select callback function to deal with data. */
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, null_write_function) != 0)
return 1;
/* Turn off libcurl's progress indicator */
if (curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1) != 0)
return 1;
/* Fail on error */
if (curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1) != 0)
return 1;
/* Test for an FTP entry */
if (strlen(current->path[FTP]) != 0) {
printf("Benchmarking FTP...\n");
get_file(current, curl, file, FTP, &total_bytes);
}
/* Test for an HTTP entry */
else if (strlen(current->path[HTTP]) != 0) {
printf("Benchmarking HTTP...\n");
get_file(current, curl, file, HTTP, &total_bytes);
} else {
printf("UNKNOWN TYPE\n");
}
curl_easy_cleanup(curl);
return 0;
}
/**
*
*
* @param current
* @param best
*/
void decide_best(server_t *current, server_t *best)
{
int i, j;
/* Inefficient sorting algorithm, but small number of entries so
it doesn't matter. */
/* move 'i' to the correct place in the array to place the new entry */
for (i = 0; i < bestnumber; ++i)
if (current->stats.speed > best[i].stats.speed)
break;
/* shove everything along one */
for (j = bestnumber - 2; j >= i; --j)
memcpy(&best[j + 1], &best[j], sizeof(server_t));
/* copy the new entry into the correct place */
memcpy(&best[i], current, sizeof(server_t));
}
/**
* @brief Callback from libcurl to allow us to save data. We discard it and
* return success.
*
* @param bytes
* @param size
* @param nmemb
* @param stream
*
* @return
*/
size_t null_write_function(double *bytes, size_t size, size_t nmemb, void *stream)
{
/* We update total_bytes */
total_bytes += size * nmemb;
return size * nmemb;
}