<commit_before>/*
	Copyright (c) 2013, University of Lugano
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
    	* Redistributions of source code must retain the above copyright
		  notice, this list of conditions and the following disclaimer.
		* Redistributions in binary form must reproduce the above copyright
		  notice, this list of conditions and the following disclaimer in the
		  documentation and/or other materials provided with the distribution.
		* Neither the name of the copyright holders nor the
		  names of its contributors may be used to endorse or promote products
		  derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.	
*/


#include <paxos.h>
#include <evpaxos.h>
#include <config.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <event2/event.h>
#include <sys/time.h>
#include <stdio.h>

#define MAX_VALUE_SIZE 64*1024


struct client_value
{
  struct timeval t;
  size_t size;
  char value[MAX_VALUE_SIZE];
};

struct stats
{
  int avg_latency;
  int delivered;
};

struct client
{
	int value_size;
	int outstanding;
        int socket;
	struct stats stats;
	struct event_base* base;
	struct event* stats_ev;
	struct event* connect_ev;
	struct timeval stats_interval;
	struct timeval start_time;
	struct event* sig;
	struct evlearner* learner;
        struct sockaddr_in proposer;
  FILE* output;
};

static void
handle_sigint(int sig, short ev, void* arg)
{
	struct event_base* base = arg;
	event_base_loopexit(base, NULL);
}

static void
random_string(char *s, const int len)
{
	int i;
	static const char alphanum[] =
		"0123456789abcdefghijklmnopqrstuvwxyz";
	for (i = 0; i < len-1; ++i)
		s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
	s[len-1] = 0;
}

static void
client_submit_value(struct client* c)
{
  struct client_value v;
  gettimeofday(&v.t, NULL);
  v.size = c->value_size;
  random_string(v.value, v.size);

  int vsize = sizeof(size_t) + sizeof(struct timeval) + v.size;

  evlearner_submit_value(c->learner, (struct sockaddr*)&c->proposer, (char*) &v, vsize);
}


// Returns t2 - t1 in microseconds.
long timeval_diff(struct timeval* t1, struct timeval* t2) {
  long us;
  us = (t2->tv_sec - t1->tv_sec) * 1e6;
  if (us < 0) return 0;
  us += (t2->tv_usec - t1->tv_usec);
  return us;
}

static void
on_deliver(unsigned iid, char* value, size_t size, void* arg)
{
	struct client* c = arg;
	struct client_value* v = (struct client_value*)value;
	struct timeval tv;
	gettimeofday(&tv, NULL);
	long latency = timeval_diff(&v->t, &tv);
        long dt = tv.tv_sec - c->start_time.tv_sec;
        if (dt > 300) exit(0);
	fprintf(c->output, "%d,%ld,%ld,%ld\n", c->outstanding, v->size, dt, latency);
	c->stats.delivered++;
	c->stats.avg_latency = c->stats.avg_latency + ((latency - c->stats.avg_latency) / c->stats.delivered);
	
	client_submit_value(c);
}

static void
on_stats(evutil_socket_t fd, short event, void *arg)
{
	struct client* c = arg;
	double mbps = (double)((c->stats.delivered*c->value_size*8)+(sizeof(struct timeval)+sizeof(size_t)*8)) / (1024*1024);
        printf("%d value/sec, %.2f Mbps, %d avg latency us\n", c->stats.delivered, mbps, c->stats.avg_latency);
	c->stats.delivered = 0;
	c->stats.avg_latency = 0;
	event_add(c->stats_ev, &c->stats_interval);
}

static void<commit_msg>records timestamp in microseconds<commit_after>/*
	Copyright (c) 2013, University of Lugano
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
    	* Redistributions of source code must retain the above copyright
		  notice, this list of conditions and the following disclaimer.
		* Redistributions in binary form must reproduce the above copyright
		  notice, this list of conditions and the following disclaimer in the
		  documentation and/or other materials provided with the distribution.
		* Neither the name of the copyright holders nor the
		  names of its contributors may be used to endorse or promote products
		  derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.	
*/


#include <paxos.h>
#include <evpaxos.h>
#include <config.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <event2/event.h>
#include <sys/time.h>
#include <stdio.h>

#define MAX_VALUE_SIZE 64*1024


struct client_value
{
  struct timeval t;
  size_t size;
  char value[MAX_VALUE_SIZE];
};

struct stats
{
  int avg_latency;
  int delivered;
};

struct client
{
	int value_size;
	int outstanding;
        int socket;
	struct stats stats;
	struct event_base* base;
	struct event* stats_ev;
	struct event* connect_ev;
	struct timeval stats_interval;
	struct timeval start_time;
	struct event* sig;
	struct evlearner* learner;
        struct sockaddr_in proposer;
  FILE* output;
};

static void
handle_sigint(int sig, short ev, void* arg)
{
	struct event_base* base = arg;
	event_base_loopexit(base, NULL);
}

static void
random_string(char *s, const int len)
{
	int i;
	static const char alphanum[] =
		"0123456789abcdefghijklmnopqrstuvwxyz";
	for (i = 0; i < len-1; ++i)
		s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
	s[len-1] = 0;
}

static void
client_submit_value(struct client* c)
{
  struct client_value v;
  gettimeofday(&v.t, NULL);
  v.size = c->value_size;
  random_string(v.value, v.size);

  int vsize = sizeof(size_t) + sizeof(struct timeval) + v.size;

  evlearner_submit_value(c->learner, (struct sockaddr*)&c->proposer, (char*) &v, vsize);
}


// Returns t2 - t1 in microseconds.
long timeval_diff(struct timeval* t1, struct timeval* t2) {
  long us;
  us = (t2->tv_sec - t1->tv_sec) * 1e6;
  if (us < 0) return 0;
  us += (t2->tv_usec - t1->tv_usec);
  return us;
}

static void
on_deliver(unsigned iid, char* value, size_t size, void* arg)
{
	struct client* c = arg;
	struct client_value* v = (struct client_value*)value;
	struct timeval tv;
	gettimeofday(&tv, NULL);
	long latency = timeval_diff(&v->t, &tv);
        long dt = tv.tv_sec - c->start_time.tv_sec;
        long ms = timeval_diff(&c->start_time, &tv);
        if (dt > 60) exit(0);
	fprintf(c->output, "%d,%ld,%ld,%ld\n", c->outstanding, v->size, ms, latency);
	c->stats.delivered++;
	c->stats.avg_latency = c->stats.avg_latency + ((latency - c->stats.avg_latency) / c->stats.delivered);
	
	client_submit_value(c);
}

static void
on_stats(evutil_socket_t fd, short event, void *arg)
{
	struct client* c = arg;
	double mbps = (double)((c->stats.delivered*c->value_size*8)+(sizeof(struct timeval)+sizeof(size_t)*8)) / (1024*1024);
        printf("%d value/sec, %.2f Mbps, %d avg latency us\n", c->stats.delivered, mbps, c->stats.avg_latency);
	c->stats.delivered = 0;
	c->stats.avg_latency = 0;
	event_add(c->stats_ev, &c->stats_interval);
}

static void
{'fraction_non_alphanumeric': 0.10807937632884479, 'fraction_numerical': 0.012875029529884242, 'mean_word_length': 4.298498122653316, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '604570', 'n_tokens_mistral': 3051, 'n_tokens_neox': 2803, 'n_words': 960}