Measure latency as the avg of n_samples

This commit is contained in:
Rafa de la Torre
2019-11-12 15:01:56 +01:00
parent 9dc547c838
commit a032cc08e0
@@ -148,16 +148,19 @@ AS $$
t_stop = timer() t_stop = timer()
s.shutdown(socket.SHUT_RD) s.shutdown(socket.SHUT_RD)
except (socket.timeout, OSError, socket.error): except (socket.timeout, OSError, socket.error):
plpy.warning('could not connect to server: %s:%d' % (host, port))
return None return None
return (t_stop - t_start) * 1000.0 t_total = (t_stop - t_start) * 1000.0
plpy.debug('TCP connection %s:%d time=%.2f ms' % (host, port, t_total))
return t_total
$$ $$
LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE; LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
-- --
-- Get the network latency to a remote PG server -- Get the network latency to a remote PG server
-- --
CREATE OR REPLACE FUNCTION @extschema@.__CDB_FS_Foreign_Server_Latency_PG(server_internal name) CREATE OR REPLACE FUNCTION @extschema@.__CDB_FS_Foreign_Server_Latency_PG(server_internal name, n_samples integer DEFAULT 10)
RETURNS float RETURNS float
AS $$ AS $$
DECLARE DECLARE
@@ -165,7 +168,10 @@ DECLARE
remote_server_port integer := @extschema@.__CDB_FS_Foreign_Server_Port_PG(server_internal); remote_server_port integer := @extschema@.__CDB_FS_Foreign_Server_Port_PG(server_internal);
latency float; latency float;
BEGIN BEGIN
latency := @extschema@.__CDB_FS_TCP_Network_Latency(host => remote_server_host, port => remote_server_port); latency := avg(sample) FROM (
SELECT @extschema@.__CDB_FS_TCP_Network_Latency(host => remote_server_host, port => remote_server_port) sample
FROM generate_series(1, n_samples)
) q;
RETURN latency; RETURN latency;
END END
$$ $$