Skip to content

Commit

Permalink
fix recorder and added cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-jentzsch committed Jul 11, 2020
1 parent 31870d0 commit 71c37bb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
9 changes: 8 additions & 1 deletion c/src/cmd/in3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ int main(int argc, char* argv[]) {
#ifndef USE_CURL
c->flags |= FLAGS_HTTP;
#endif
// handle clear cache opt before initializing cache
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "-fi") == 0) {
recorder_update_cmd(argv[i + 1], &argc, &argv);
break;
}

// handle clear cache opt before initializing cache
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "-ccache") == 0)
Expand Down Expand Up @@ -768,7 +775,7 @@ int main(int argc, char* argv[]) {
else if (strcmp(argv[i], "-thr") == 0)
run_test_request = 2;
else if (strcmp(argv[i], "-fo") == 0)
recorder_write_start(c, argv[++i]);
recorder_write_start(c, argv[++i], argc, argv);
else if (strcmp(argv[i], "-fi") == 0)
recorder_read_start(c, argv[++i]);
else if (strcmp(argv[i], "-nl") == 0)
Expand Down
30 changes: 23 additions & 7 deletions c/src/cmd/in3/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ static int rand_in(void* s) {
static in3_ret_t recorder_transport_in(in3_request_t* req) {

if (req->action == REQ_ACTION_SEND) {
entry_free(next_entry("requqest", NULL));
entry_free(next_entry("request", NULL));
req->cptr = &rec;
}
if (req->action != REQ_ACTION_CLEANUP) {
recorder_entry_t* entry = next_entry("response", NULL);
in3_response_t* r = req->ctx->raw_response + atoi(entry->args[0]);
recorder_entry_t* entry = next_entry("response", d_get_stringk(req->ctx->requests[0], K_METHOD));
in3_response_t* r = req->ctx->raw_response + atoi(entry->args[1]);
sb_add_chars(&r->data, entry->content.data);
r->state = atoi(entry->args[3]);
r->time = atoi(entry->args[4]);
Expand All @@ -141,7 +141,7 @@ static in3_ret_t recorder_transport_out(in3_request_t* req) {
for (int i = 0; m; i++, m = m->next) {
in3_response_t* r = req->ctx->raw_response + i;
if (r->time) {
fprintf(rec.f, ":: response %i %s %s %i %i\n", i, d_get_stringk(req->ctx->requests[0], K_METHOD), ctx_get_node(chain, m)->url, r->state, r->time);
fprintf(rec.f, ":: response %s %i %s %i %i\n", d_get_stringk(req->ctx->requests[0], K_METHOD), i, ctx_get_node(chain, m)->url, r->state, r->time);
char* data = format_json(r->data.data ? r->data.data : "");
fprintf(rec.f, "%s\n\n", data);
fflush(rec.f);
Expand All @@ -155,9 +155,8 @@ static in3_ret_t recorder_transport_out(in3_request_t* req) {
bytes_t* rec_get_item_in(void* cptr, const char* key) {
UNUSED_VAR(cptr);
UNUSED_VAR(key);
sb_t sb = {0};
recorder_entry_t* entry = next_entry("cache", key);
bytes_t* found = atoi(entry->args[1]) ? hex_to_new_bytes(sb.data, sb.len) : NULL;
bytes_t* found = atoi(entry->args[1]) ? hex_to_new_bytes(entry->content.data, entry->content.len) : NULL;
entry_free(entry);

return found;
Expand Down Expand Up @@ -199,13 +198,16 @@ uint64_t static_time(void* t) {
return rec.time;
}

void recorder_write_start(in3_t* c, char* file) {
void recorder_write_start(in3_t* c, char* file, int argc, char* argv[]) {
rec.file = file;
rec.transport = c->transport;
c->transport = recorder_transport_out;
rec.f = fopen(file, "w");
rec.cache = c->cache;
in3_set_func_rand(rand_out);
fprintf(rec.f, ":: cmd");
for (int i = 0; i < argc; i++) fprintf(rec.f, " %s", strcmp(argv[i], "-fo") ? argv[i] : "-fi");
fprintf(rec.f, "\n\n");
in3_set_storage_handler(c, rec_get_item_out, rec_set_item_out, rec_clear_out, &rec);
fprintf(rec.f, ":: time %u\n\n", (uint32_t) in3_time(NULL));
}
Expand All @@ -222,3 +224,17 @@ void recorder_read_start(in3_t* c, char* file) {
entry_free(entry);
in3_set_func_time(static_time);
}

void recorder_update_cmd(char* file, int* argc, char** argv[]) {
rec.file = file;
rec.f = fopen(file, "r");
recorder_entry_t* entry = next_entry("cmd", NULL);
*argc = entry->argl;
*argv = entry->args;
for (int i = 0; i < entry->argl; i++) {
if (strcmp(entry->args[i], "-fi") == 0) entry->args[i + 1] = file;
}
fclose(rec.f);
rec.f = NULL;
rec.queue = NULL;
}
3 changes: 2 additions & 1 deletion c/src/cmd/in3/recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#include <stdlib.h>
#include <string.h>

void recorder_write_start(in3_t* c, char* file);
void recorder_write_start(in3_t* c, char* file, int argc, char* argv[]);
void recorder_read_start(in3_t* c, char* file);
void recorder_update_cmd(char* file, int* argc, char** argv[]);

0 comments on commit 71c37bb

Please sign in to comment.