-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmultipart.cpp
164 lines (141 loc) · 4.07 KB
/
multipart.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "MultipartParser.h"
#include "MultipartReader.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
//#define TEST_PARSER
#define INPUT_FILE "input3.txt"
//#define BOUNDARY "abcd"
#define BOUNDARY "-----------------------------168072824752491622650073"
#define TIMES 10
#define SLURP
#define QUIET
using namespace std;
#ifdef TEST_PARSER
static void
onPartBegin(const char *buffer, size_t start, size_t end, void *userData) {
printf("onPartBegin\n");
}
static void
onHeaderField(const char *buffer, size_t start, size_t end, void *userData) {
printf("onHeaderField: (%s)\n", string(buffer + start, end - start).c_str());
}
static void
onHeaderValue(const char *buffer, size_t start, size_t end, void *userData) {
printf("onHeaderValue: (%s)\n", string(buffer + start, end - start).c_str());
}
static void
onPartData(const char *buffer, size_t start, size_t end, void *userData) {
printf("onPartData: (%s)\n", string(buffer + start, end - start).c_str());
}
static void
onPartEnd(const char *buffer, size_t start, size_t end, void *userData) {
printf("onPartEnd\n");
}
static void
onEnd(const char *buffer, size_t start, size_t end, void *userData) {
printf("onEnd\n");
}
#else
void onPartBegin(const MultipartHeaders &headers, void *userData) {
printf("onPartBegin:\n");
MultipartHeaders::const_iterator it;
MultipartHeaders::const_iterator end = headers.end();
for (it = headers.begin(); it != headers.end(); it++) {
printf(" %s = %s\n", it->first.c_str(), it->second.c_str());
}
printf(" aaa: %s\n", headers["aaa"].c_str());
}
void onPartData(const char *buffer, size_t size, void *userData) {
//printf("onPartData: (%s)\n", string(buffer, size).c_str());
}
void onPartEnd(void *userData) {
printf("onPartEnd\n");
}
void onEnd(void *userData) {
printf("onEnd\n");
}
#endif
int
main() {
#ifdef TEST_PARSER
MultipartParser parser;
#ifndef QUIET
parser.onPartBegin = onPartBegin;
parser.onHeaderField = onHeaderField;
parser.onHeaderValue = onHeaderValue;
parser.onPartData = onPartData;
parser.onPartEnd = onPartEnd;
parser.onEnd = onEnd;
#endif
#else
MultipartReader parser;
#ifndef QUIET
parser.onPartBegin = onPartBegin;
parser.onPartData = onPartData;
parser.onPartEnd = onPartEnd;
parser.onEnd = onEnd;
#endif
#endif
struct timeval stime, etime;
struct stat sbuf;
stat(INPUT_FILE, &sbuf);
#ifdef SLURP
size_t bufsize = sbuf.st_size;
char *buf = (char *) malloc(bufsize);
FILE *f = fopen(INPUT_FILE, "rb");
fread(buf, 1, bufsize, f);
fclose(f);
gettimeofday(&stime, NULL);
for (int i = 0; i < TIMES; i++) {
#ifndef QUIET
printf("------------\n");
#endif
parser.setBoundary(BOUNDARY);
size_t fed = 0;
do {
size_t ret = parser.feed(buf + fed, bufsize - fed);
fed += ret;
} while (fed < bufsize && !parser.stopped());
#ifndef QUIET
printf("%s\n", parser.getErrorMessage());
#endif
}
gettimeofday(&etime, NULL);
#else
size_t bufsize = 1024 * 32;
char *buf = (char *) malloc(bufsize);
gettimeofday(&stime, NULL);
for (int i = 0; i < TIMES; i++) {
#ifndef QUIET
printf("------------\n");
#endif
parser.setBoundary(BOUNDARY);
FILE *f = fopen(INPUT_FILE, "rb");
while (!parser.stopped() && !feof(f)) {
size_t len = fread(buf, 1, bufsize, f);
size_t fed = 0;
do {
size_t ret = parser.feed(buf + fed, len - fed);
fed += ret;
} while (fed < len && !parser.stopped());
}
#ifndef QUIET
printf("%s\n", parser.getErrorMessage());
#endif
fclose(f);
}
gettimeofday(&etime, NULL);
#endif
unsigned long long a = (unsigned long long) stime.tv_sec * 1000000 + stime.tv_usec;
unsigned long long b = (unsigned long long) etime.tv_sec * 1000000 + etime.tv_usec;
printf("(C++) Total: %.2fs Per run: %.2fs Throughput: %.2f MB/sec\n",
(b - a) / 1000000.0,
(b - a) / TIMES / 1000000.0,
((unsigned long long) sbuf.st_size * TIMES) / ((b - a) / 1000000.0) / 1024.0 / 1024.0);
return 0;
}