Measure BLE ETCP echo latency with paced small packets

This commit is contained in:
evgeny committed 2026-10-30 04:39:05 +01:00
1 parent 567743f5bf
commit 4c097c0a34
1 file changed
+30 -14
+30 -14
View File
@@ -1,6 +1,6 @@
/* Ручная проверка Android↔desktop: Android BlePeerProbe с -e etcp true.
* Временное ядро без IP-сокетов; пакеты по 1500 байт, точная проверка ETCP echo.
* Аргументы: seconds [connect|listen] [invites|data] [packets=32] [window=1].
* Временное ядро без IP-сокетов; точная проверка ETCP echo, скорости и задержек.
* Аргументы: seconds [connect|listen] [invites|data] [packets=32] [window=1] [bytes=1500] [interval_ms=0].
* Входящее подключение к неизвестным объявлениям не инициируется: только имя ETCP-probe. */
#include "../ble_etcp.h"
#include "utun_instance.h"
@@ -15,7 +15,9 @@
#include <stdlib.h>
#include <string.h>
static unsigned verified;
static unsigned packet_size = 1500;
static uint64_t transfer_started, transfer_finished;
static uint64_t sent_at[64], rtt_sum, rtt_max, receive_gap_max;
static int invitations, invitation_sent, invitation_received, invitation_delivered;
static char invitation_link[LOCAL_TRANSPORT_DATA_MAX + 1];
static int failed, radio_ready, connection_closed;
@@ -37,8 +39,8 @@ static void event(struct UTUN_INSTANCE* inst, int type, const uint8_t* data, int
}
static void receive(struct ETCP_CONN* conn, struct ll_entry* e) {
(void)conn;
if (e->len != 1500) {
DEBUG_ERROR(DEBUG_CATEGORY_DISCOVERY, "BLE probe size mismatch packet=%u expected=1500 actual=%u", verified, e->len);
if (e->len != packet_size) {
DEBUG_ERROR(DEBUG_CATEGORY_DISCOVERY, "BLE probe size mismatch packet=%u expected=%u actual=%u", verified, packet_size, e->len);
failed = 1;
}
for (unsigned i = 1; i < e->len; i++) if (e->dgram[i] != (uint8_t)(verified + i * 37)) {
@@ -47,7 +49,11 @@ static void receive(struct ETCP_CONN* conn, struct ll_entry* e) {
failed = 1; break;
}
DEBUG_TRACE(DEBUG_CATEGORY_DISCOVERY, "VERIFY ETCP packet=%u bytes=%u ok=%d", verified, e->len, !failed);
verified++; transfer_finished = get_time_us();
uint64_t now = get_time_us(), rtt = now - sent_at[verified % 64];
rtt_sum += rtt;
if (rtt > rtt_max) rtt_max = rtt;
if (verified && now - transfer_finished > receive_gap_max) receive_gap_max = now - transfer_finished;
verified++; transfer_finished = now;
queue_dgram_free(e); queue_entry_free(e);
}
static void status(void* arg, enum ble_state s, const char* reason) {
@@ -66,7 +72,10 @@ int main(int argc, char** argv) {
int seconds = argc > 1 ? atoi(argv[1]) : 60;
unsigned packets = argc > 4 ? (unsigned)atoi(argv[4]) : 32;
unsigned window = argc > 5 ? (unsigned)atoi(argv[5]) : 1;
if (seconds < 1 || seconds > 300 || !packets || packets > 1000000 || !window || window > 64) return 2;
packet_size = argc > 6 ? (unsigned)atoi(argv[6]) : 1500;
unsigned interval_ms = argc > 7 ? (unsigned)atoi(argv[7]) : 0;
if (seconds < 1 || seconds > 300 || !packets || packets > 1000000 || !window || window > 64 ||
packet_size < 2 || packet_size > 1500 || interval_ms > 1000) return 2;
debug_config_init(); debug_set_level(DEBUG_LEVEL_WARN); debug_set_category_level(DEBUG_CATEGORY_DISCOVERY, DEBUG_LEVEL_DEBUG);
struct UASYNC* ua = uasync_create();
struct SC_MYKEYS keys; if (!ua || sc_generate_keypair(&keys) != SC_OK) return 1;
@@ -95,7 +104,7 @@ int main(int argc, char** argv) {
c.callbacks.status = status; c.callbacks.connection = connection;
driver = ble_etcp_create(ua, instance, &c);
if (!driver || etcp_bind(instance, ETCP_ID_BLE_PROBE, receive) || ble_start(ble_etcp_radio(driver))) failed = 1;
uint64_t end = get_time_us() + (uint64_t)seconds * 1000000, peer = 0, retry = 0;
uint64_t end = get_time_us() + (uint64_t)seconds * 1000000, peer = 0, retry = 0, next_send = 0;
unsigned sent = 0;
int dial = argc < 3 || strcmp(argv[2], "listen");
while (!failed && (verified < packets || (invitations && (!invitation_received || !invitation_delivered))) && get_time_us() < end) {
@@ -125,13 +134,17 @@ int main(int argc, char** argv) {
if (!bt || bt->send(bt->ctx, peer, 1, invitation_link, strlen(invitation_link))) { failed = 1; break; }
invitation_sent = 1;
}
if (sent >= packets || sent - verified >= window) continue;
struct ll_entry* e = ll_alloc_lldgram(1500);
if (sent >= packets || sent - verified >= window || get_time_us() < next_send) continue;
struct ll_entry* e = ll_alloc_lldgram(packet_size);
if (!e) { failed = 1; break; }
e->len = 1500; e->dgram[0] = ETCP_ID_BLE_PROBE;
for (unsigned i = 1; i < 1500; i++) e->dgram[i] = (uint8_t)(sent + i * 37);
e->len = packet_size; e->dgram[0] = ETCP_ID_BLE_PROBE;
for (unsigned i = 1; i < packet_size; i++) e->dgram[i] = (uint8_t)(sent + i * 37);
if (etcp_send(conn, e)) { queue_dgram_free(e); queue_entry_free(e); failed = 1; }
else { if (!transfer_started) transfer_started = get_time_us(); sent++; }
else {
sent_at[sent % 64] = get_time_us(); next_send = sent_at[sent % 64] + (uint64_t)interval_ms * 1000;
if (!transfer_started) transfer_started = sent_at[sent % 64];
sent++;
}
}
uint64_t measured_end = verified == packets ? transfer_finished : get_time_us();
if (verified != packets || (invitations && (!invitation_delivered || invitation_received != 1))) {
@@ -140,11 +153,14 @@ int main(int argc, char** argv) {
failed = 1;
}
ble_etcp_destroy(driver); utun_instance_destroy(instance); uasync_destroy(ua, 0);
printf("RESULT BLE ETCP sent=%u verified=%u bytes=%u failed=%d\n", sent, verified, verified * 1500, failed);
printf("RESULT BLE ETCP sent=%u verified=%u bytes=%u failed=%d\n", sent, verified, verified * packet_size, failed);
if (transfer_started && measured_end > transfer_started) {
double elapsed = (measured_end - transfer_started) / 1000000.0;
printf("THROUGHPUT complete=%d window=%u verified_bytes_each_direction=%u seconds=%.3f aggregate_bytes_per_second=%.0f\n",
!failed, window, verified * 1500, elapsed, verified * 3000.0 / elapsed);
!failed, window, verified * packet_size, elapsed, verified * packet_size * 2.0 / elapsed);
printf("LATENCY bytes=%u interval_ms=%u RTT_avg_us=%llu RTT_max_us=%llu RX_gap_max_us=%llu\n", packet_size, interval_ms,
(unsigned long long)(verified ? rtt_sum / verified : 0), (unsigned long long)rtt_max,
(unsigned long long)receive_gap_max);
}
if (invitations) printf("RESULT BLE invitations sent=%d delivered=%d received=%d\n", invitation_sent, invitation_delivered, invitation_received);
return failed || verified != packets || (invitations && (!invitation_delivered || invitation_received != 1));