4040#include < limits.h>
4141#endif
4242
43- #ifdef HAVE_ASSERT_H
44- #include < assert.h>
45- #endif
4643
44+ #include < assert.h>
45+ #include < limits.h>
4746#include < math.h>
4847#include < algorithm>
4948#include < arpa/inet.h>
@@ -59,7 +58,14 @@ bool client::setup_client(benchmark_config *config, abstract_protocol *protocol,
5958 unsigned long long total_num_of_clients = config->clients *config->threads ;
6059
6160 // create main connection
62- shard_connection* conn = new shard_connection (m_connections.size (), this , m_config, m_event_base, protocol);
61+ unsigned int thread_id = 0 ; // TODO: set actual thread id if available
62+ unsigned int client_index = m_connections.size ();
63+ unsigned int num_clients_per_thread = config->clients ;
64+ unsigned int conn_id = thread_id * num_clients_per_thread + client_index;
65+ shard_connection* conn = new shard_connection (
66+ client_index, this , m_config, m_event_base, protocol,
67+ conn_id
68+ );
6369 m_connections.push_back (conn);
6470
6571 m_obj_gen = objgen->clone ();
@@ -99,7 +105,7 @@ bool client::setup_client(benchmark_config *config, abstract_protocol *protocol,
99105 return true ;
100106}
101107
102- client::client (client_group* group) :
108+ client::client (client_group* group, unsigned int conn_id ) :
103109 m_event_base(NULL ), m_initialized(false ), m_end_set(false ), m_config(NULL ),
104110 m_obj_gen(NULL ), m_stats(group->get_config ()), m_reqs_processed(0 ), m_reqs_generated(0 ),
105111 m_set_ratio_count(0 ), m_get_ratio_count(0 ),
@@ -108,16 +114,21 @@ client::client(client_group* group) :
108114{
109115 m_event_base = group->get_event_base ();
110116
117+ // Initialize conn_id string and value with prefix
118+ m_conn_id_str = " user" + std::to_string (conn_id);
119+ m_conn_id_value = m_conn_id_str.c_str ();
120+ m_conn_id_value_len = m_conn_id_str.length ();
121+
111122 if (!setup_client (group->get_config (), group->get_protocol (), group->get_obj_gen ())) {
112123 return ;
113124 }
114125
115- benchmark_debug_log (" new client %p successfully set up.\n " , this );
126+ benchmark_debug_log (" new client %p successfully set up with conn_id: %s .\n " , this , m_conn_id_value );
116127 m_initialized = true ;
117128}
118129
119130client::client (struct event_base *event_base, benchmark_config *config,
120- abstract_protocol *protocol, object_generator *obj_gen) :
131+ abstract_protocol *protocol, object_generator *obj_gen, unsigned int conn_id ) :
121132 m_event_base(NULL ), m_initialized(false ), m_end_set(false ), m_config(NULL ),
122133 m_obj_gen(NULL ), m_stats(config), m_reqs_processed(0 ), m_reqs_generated(0 ),
123134 m_set_ratio_count(0 ), m_get_ratio_count(0 ),
@@ -126,11 +137,16 @@ client::client(struct event_base *event_base, benchmark_config *config,
126137{
127138 m_event_base = event_base;
128139
140+ // Initialize conn_id string and value
141+ m_conn_id_str = std::to_string (conn_id);
142+ m_conn_id_value = m_conn_id_str.c_str ();
143+ m_conn_id_value_len = m_conn_id_str.length ();
144+
129145 if (!setup_client (config, protocol, obj_gen)) {
130146 return ;
131147 }
132148
133- benchmark_debug_log (" new client %p successfully set up.\n " , this );
149+ benchmark_debug_log (" new client %p successfully set up with conn_id: %s .\n " , this , m_conn_id_value );
134150 m_initialized = true ;
135151}
136152
@@ -273,7 +289,11 @@ bool client::create_arbitrary_request(unsigned int command_index, struct timeval
273289
274290 const arbitrary_command& cmd = get_arbitrary_command (command_index);
275291
276- benchmark_debug_log (" %s: %s:\n " , m_connections[conn_id]->get_readable_id (), cmd.command .c_str ());
292+ benchmark_debug_log (" %s: %s" , m_connections[conn_id]->get_readable_id (), cmd.command .c_str ());
293+
294+ // Build final command string for debug output
295+ std::string final_command = cmd.command ;
296+ bool has_substitutions = false ;
277297
278298 for (unsigned int i = 0 ; i < cmd.command_args .size (); i++) {
279299 const command_arg* arg = &cmd.command_args [i];
@@ -293,9 +313,32 @@ bool client::create_arbitrary_request(unsigned int command_index, struct timeval
293313 assert (value_len > 0 );
294314
295315 cmd_size += m_connections[conn_id]->send_arbitrary_command (arg, value, value_len);
316+ } else if (arg->type == conn_id_type) {
317+ // Replace __conn_id__ placeholder with actual connection ID
318+ std::string substituted_arg = arg->data ;
319+ size_t pos = substituted_arg.find (CONN_PLACEHOLDER);
320+ if (pos != std::string::npos) {
321+ substituted_arg.replace (pos, strlen (CONN_PLACEHOLDER), m_conn_id_value);
322+ has_substitutions = true ;
323+ }
324+
325+ cmd_size += m_connections[conn_id]->send_arbitrary_command (arg, substituted_arg.c_str (), substituted_arg.length ());
326+
327+ // Replace placeholder in final command string for debug output
328+ pos = final_command.find (CONN_PLACEHOLDER);
329+ if (pos != std::string::npos) {
330+ final_command.replace (pos, strlen (CONN_PLACEHOLDER), m_conn_id_value);
331+ }
296332 }
297333 }
298334
335+ // Show final command if substitutions were made
336+ if (has_substitutions) {
337+ benchmark_debug_log (" -> %s\n " , final_command.c_str ());
338+ } else {
339+ benchmark_debug_log (" \n " );
340+ }
341+
299342 m_connections[conn_id]->send_arbitrary_command_end (command_index, ×tamp, cmd_size);
300343 return true ;
301344}
@@ -581,8 +624,8 @@ bool verify_client::finished(void)
581624
582625// /////////////////////////////////////////////////////////////////////////
583626
584- client_group::client_group (benchmark_config* config, abstract_protocol *protocol, object_generator* obj_gen) :
585- m_base(NULL ), m_config(config), m_protocol(protocol), m_obj_gen(obj_gen)
627+ client_group::client_group (benchmark_config* config, abstract_protocol *protocol, object_generator* obj_gen, unsigned int thread_id ) :
628+ m_base(NULL ), m_config(config), m_protocol(protocol), m_obj_gen(obj_gen), m_thread_id(thread_id)
586629{
587630 m_base = event_base_new ();
588631 assert (m_base != NULL );
@@ -608,11 +651,12 @@ int client_group::create_clients(int num)
608651{
609652 for (int i = 0 ; i < num; i++) {
610653 client* c;
654+ unsigned int conn_id = m_thread_id * num + i + 1 ;
611655
612656 if (m_config->cluster_mode )
613- c = new cluster_client (this );
657+ c = new cluster_client (this , conn_id );
614658 else
615- c = new client (this );
659+ c = new client (this , conn_id );
616660
617661 assert (c != NULL );
618662
0 commit comments