@@ -295,21 +295,6 @@ mod tests {
295295 let repo = Repository :: new ( ) ;
296296 let now = CurrentClock :: now ( ) ;
297297
298- // Set up initial connections handled
299- let ipv4_labels = LabelSet :: from ( [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "connect" ) ] ) ;
300- let ipv6_labels = LabelSet :: from ( [ ( "server_binding_address_ip_family" , "inet6" ) , ( "request_kind" , "connect" ) ] ) ;
301-
302- // Simulate 2 IPv4 and 1 IPv6 connections
303- repo. increase_counter ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) , & ipv4_labels, now)
304- . await
305- . unwrap ( ) ;
306- repo. increase_counter ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) , & ipv4_labels, now)
307- . await
308- . unwrap ( ) ;
309- repo. increase_counter ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) , & ipv6_labels, now)
310- . await
311- . unwrap ( ) ;
312-
313298 // Set initial average to 1000ns
314299 let connect_labels = LabelSet :: from ( [ ( "request_kind" , "connect" ) ] ) ;
315300 repo. set_gauge (
@@ -322,14 +307,16 @@ mod tests {
322307 . unwrap ( ) ;
323308
324309 // Calculate new average with processing time of 2000ns
310+ // This will increment the processed requests counter from 0 to 1
325311 let processing_time = Duration :: from_nanos ( 2000 ) ;
326312 let new_avg = repo
327313 . recalculate_udp_avg_processing_time_ns ( processing_time, & connect_labels, now)
328314 . await ;
329315
330- // Moving average: previous_avg + (new_value - previous_avg) / total_connections
331- // 1000 + (2000 - 1000) / 3 = 1000 + 333.33 = 1333.33
332- let expected_avg = 1000.0 + ( 2000.0 - 1000.0 ) / 3.0 ;
316+ // Moving average: previous_avg + (new_value - previous_avg) / processed_requests_total
317+ // With processed_requests_total = 1 (incremented during the call):
318+ // 1000 + (2000 - 1000) / 1 = 1000 + 1000 = 2000
319+ let expected_avg = 1000.0 + ( 2000.0 - 1000.0 ) / 1.0 ;
333320 assert ! (
334321 ( new_avg - expected_avg) . abs( ) < 0.01 ,
335322 "Expected {expected_avg}, got {new_avg}"
@@ -341,22 +328,6 @@ mod tests {
341328 let repo = Repository :: new ( ) ;
342329 let now = CurrentClock :: now ( ) ;
343330
344- // Set up initial announces handled
345- let ipv4_labels = LabelSet :: from ( [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "announce" ) ] ) ;
346- let ipv6_labels = LabelSet :: from ( [ ( "server_binding_address_ip_family" , "inet6" ) , ( "request_kind" , "announce" ) ] ) ;
347-
348- // Simulate 3 IPv4 and 2 IPv6 announces
349- for _ in 0 ..3 {
350- repo. increase_counter ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) , & ipv4_labels, now)
351- . await
352- . unwrap ( ) ;
353- }
354- for _ in 0 ..2 {
355- repo. increase_counter ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) , & ipv6_labels, now)
356- . await
357- . unwrap ( ) ;
358- }
359-
360331 // Set initial average to 500ns
361332 let announce_labels = LabelSet :: from ( [ ( "request_kind" , "announce" ) ] ) ;
362333 repo. set_gauge (
@@ -369,14 +340,16 @@ mod tests {
369340 . unwrap ( ) ;
370341
371342 // Calculate new average with processing time of 1500ns
343+ // This will increment the processed requests counter from 0 to 1
372344 let processing_time = Duration :: from_nanos ( 1500 ) ;
373345 let new_avg = repo
374346 . recalculate_udp_avg_processing_time_ns ( processing_time, & announce_labels, now)
375347 . await ;
376348
377- // Moving average: previous_avg + (new_value - previous_avg) / total_announces
378- // 500 + (1500 - 500) / 5 = 500 + 200 = 700
379- let expected_avg = 500.0 + ( 1500.0 - 500.0 ) / 5.0 ;
349+ // Moving average: previous_avg + (new_value - previous_avg) / processed_requests_total
350+ // With processed_requests_total = 1 (incremented during the call):
351+ // 500 + (1500 - 500) / 1 = 500 + 1000 = 1500
352+ let expected_avg = 500.0 + ( 1500.0 - 500.0 ) / 1.0 ;
380353 assert ! (
381354 ( new_avg - expected_avg) . abs( ) < 0.01 ,
382355 "Expected {expected_avg}, got {new_avg}"
@@ -388,16 +361,6 @@ mod tests {
388361 let repo = Repository :: new ( ) ;
389362 let now = CurrentClock :: now ( ) ;
390363
391- // Set up initial scrapes handled
392- let ipv4_labels = LabelSet :: from ( [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "scrape" ) ] ) ;
393-
394- // Simulate 4 IPv4 scrapes
395- for _ in 0 ..4 {
396- repo. increase_counter ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) , & ipv4_labels, now)
397- . await
398- . unwrap ( ) ;
399- }
400-
401364 // Set initial average to 800ns
402365 let scrape_labels = LabelSet :: from ( [ ( "request_kind" , "scrape" ) ] ) ;
403366 repo. set_gauge (
@@ -410,14 +373,16 @@ mod tests {
410373 . unwrap ( ) ;
411374
412375 // Calculate new average with processing time of 1200ns
376+ // This will increment the processed requests counter from 0 to 1
413377 let processing_time = Duration :: from_nanos ( 1200 ) ;
414378 let new_avg = repo
415379 . recalculate_udp_avg_processing_time_ns ( processing_time, & scrape_labels, now)
416380 . await ;
417381
418- // Moving average: previous_avg + (new_value - previous_avg) / total_scrapes
419- // 800 + (1200 - 800) / 4 = 800 + 100 = 900
420- let expected_avg = 800.0 + ( 1200.0 - 800.0 ) / 4.0 ;
382+ // Moving average: previous_avg + (new_value - previous_avg) / processed_requests_total
383+ // With processed_requests_total = 1 (incremented during the call):
384+ // 800 + (1200 - 800) / 1 = 800 + 400 = 1200
385+ let expected_avg = 800.0 + ( 1200.0 - 800.0 ) / 1.0 ;
421386 assert ! (
422387 ( new_avg - expected_avg) . abs( ) < 0.01 ,
423388 "Expected {expected_avg}, got {new_avg}"
@@ -584,49 +549,38 @@ mod tests {
584549 let connect_labels = LabelSet :: from ( [ ( "request_kind" , "connect" ) ] ) ;
585550 let now = CurrentClock :: now ( ) ;
586551
587- // This test checks the behavior of `recalculate_udp_avg_connect_processing_time_ns` `
588- // when no connections have been recorded yet. The first call should
589- // handle division by zero gracefully and return an infinite average,
590- // which is the current behavior .
552+ // This test checks the behavior of `recalculate_udp_avg_processing_time_ns `
553+ // when no processed requests have been recorded yet. The first call should
554+ // handle division by zero gracefully and set the first average to the
555+ // processing time of the first request .
591556
592- // todo: the first average should be 2000ns, not infinity.
593- // This is because the first connection is not counted in the average
594- // calculation if the counter is increased after calculating the average.
595- // The problem is that we count requests when they are accepted, not
596- // when they are processed. And we calculate the average when the
597- // response is sent.
598-
599- // First calculation: no connections recorded yet, should result in infinity
557+ // First calculation: no processed requests recorded yet
600558 let processing_time_1 = Duration :: from_nanos ( 2000 ) ;
601559 let avg_1 = repo
602560 . recalculate_udp_avg_processing_time_ns ( processing_time_1, & connect_labels, now)
603561 . await ;
604562
563+ // The first average should be the first processing time since processed_requests_total is 0
564+ // When processed_requests_total == 0.0, new_avg = req_processing_time
605565 assert ! (
606566 ( avg_1 - 2000.0 ) . abs( ) < f64 :: EPSILON ,
607567 "First calculation should be 2000, but got {avg_1}"
608568 ) ;
609569
610- // Now add one connection and try again
611- let ipv4_labels = LabelSet :: from ( [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "connect" ) ] ) ;
612- repo. increase_counter ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) , & ipv4_labels, now)
613- . await
614- . unwrap ( ) ;
615-
616- // Second calculation: 1 connection
570+ // Second calculation: now we have one processed request (incremented during first call)
617571 let processing_time_2 = Duration :: from_nanos ( 3000 ) ;
618- let connect_labels = LabelSet :: from ( [ ( "request_kind" , "connect" ) ] ) ;
619572 let avg_2 = repo
620573 . recalculate_udp_avg_processing_time_ns ( processing_time_2, & connect_labels, now)
621574 . await ;
622575
623- // There is one connection, so the average should be:
624- // 2000 + (3000 - 2000) / 1 = 2000 + 1000 = 3000
625- // This is because one connection is not counted yet in the average calculation,
626- // so the average is simply the processing time of the second connection.
576+ // Moving average calculation: previous_avg + (new_value - previous_avg) / processed_requests_total
577+ // After first call: processed_requests_total = 1, avg = 2000
578+ // During second call: processed_requests_total incremented to 2
579+ // new_avg = 2000 + (3000 - 2000) / 2 = 2000 + 500 = 2500
580+ let expected_avg_2 = 2000.0 + ( 3000.0 - 2000.0 ) / 2.0 ;
627581 assert ! (
628- ( avg_2 - 3000.0 ) . abs( ) < f64 :: EPSILON ,
629- "Second calculation should be 3000ns , but got {avg_2}"
582+ ( avg_2 - expected_avg_2 ) . abs( ) < f64 :: EPSILON ,
583+ "Second calculation should be {expected_avg_2}ns , but got {avg_2}"
630584 ) ;
631585 }
632586}
0 commit comments