Skip to content

Commit

Permalink
endpoint,etc: hirestime => performance.now
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jul 23, 2024
1 parent 0d664b5 commit e0c16c4
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 28 deletions.
1 change: 0 additions & 1 deletion pkg/autoconfig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@ndn/quic-transport": "workspace:*",
"@ndn/ws-transport": "workspace:*",
"default-gateway": "^7.2.2",
"hirestime": "^7.0.4",
"tslib": "^2.6.3"
},
"devDependencies": {
Expand Down
7 changes: 2 additions & 5 deletions pkg/autoconfig/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { consume, type ConsumerOptions } from "@ndn/endpoint";
import { type Forwarder, type FwFace, TapFace } from "@ndn/fw";
import { Interest, Name, type NameLike } from "@ndn/packet";
import type { H3Transport } from "@ndn/quic-transport";
import hirestime from "hirestime";

import { createFace } from "./platform_node";

Expand Down Expand Up @@ -82,18 +81,16 @@ export interface ConnectRouterResult {
testConnectionResult: unknown;
}

const now = hirestime();

/** Connect to a router and test the connection. */
export async function connectToRouter(router: string, opts: ConnectRouterOptions = {}): Promise<ConnectRouterResult> {
const face = await createFace(router, opts);

const testConnectionStart = now();
const testConnectionStart = performance.now();
let testConnectionDuration: number;
let testConnectionResult: unknown;
try {
testConnectionResult = await testConnection(face, opts);
testConnectionDuration = now() - testConnectionStart;
testConnectionDuration = performance.now() - testConnectionStart;
} catch (err: unknown) {
face.close();
throw err;
Expand Down
1 change: 0 additions & 1 deletion pkg/endpoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@ndn/fw": "workspace:*",
"@ndn/packet": "workspace:*",
"@ndn/util": "workspace:*",
"hirestime": "^7.0.4",
"streaming-iterables": "^8.0.1",
"tslib": "^2.6.3"
},
Expand Down
7 changes: 2 additions & 5 deletions pkg/endpoint/src/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { CancelInterest, Forwarder, FwPacket } from "@ndn/fw";
import { Data, Interest, type NameLike, type Verifier } from "@ndn/packet";
import { pushable } from "@ndn/util";
import hirestime from "hirestime";

import { type CommonOptions, exactOptions } from "./common";
import { makeRetxGenerator, type RetxPolicy } from "./retx";

const getNow = hirestime();

/** {@link consume} options. */
export interface ConsumerOptions extends CommonOptions {
/**
Expand Down Expand Up @@ -99,7 +96,7 @@ function makeConsumer(
timer = setTimeout(sendInterest, value);
}
rx.push(FwPacket.create(interest));
txTime = getNow();
txTime = performance.now();
++nRetx;
};

Expand All @@ -114,7 +111,7 @@ function makeConsumer(
async tx(iterable) {
for await (const pkt of iterable) {
if (pkt.l3 instanceof Data) {
rtt = getNow() - txTime;
rtt = performance.now() - txTime;
try {
await verifier?.verify(pkt.l3);
} catch (err: unknown) {
Expand Down
1 change: 0 additions & 1 deletion pkg/fw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@ndn/packet": "workspace:*",
"@ndn/util": "workspace:*",
"@types/retry": "^0.12.5",
"hirestime": "^7.0.4",
"mnemonist": "^0.39.8",
"retry": "^0.13.1",
"streaming-iterables": "^8.0.1",
Expand Down
11 changes: 4 additions & 7 deletions pkg/fw/src/pit.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { type Data, Interest } from "@ndn/packet";
import hirestime from "hirestime";
import DefaultMap from "mnemonist/default-map.js";
import { filter, flatMap, pipeline, reduce, tap } from "streaming-iterables";

import type { FaceImpl } from "./face";
import { FwPacket, RejectInterest } from "./packet";

const getNow = hirestime();

/** Downstream of pending Interest. */
interface PitDn {
/** How many times this downstream has (re)transmitted the Interest. */
Expand Down Expand Up @@ -41,7 +38,7 @@ export class PitEntry {

/** Record Interest from downstream. */
public receiveInterest(face: FaceImpl, { l3: interest, token }: FwPacket<Interest>) {
const now = getNow();
const now = performance.now();
const expire = now + interest.lifetime;
const nonce = interest.nonce ?? Interest.generateNonce();

Expand All @@ -66,7 +63,7 @@ export class PitEntry {

/** Forward Interest to upstream. */
public forwardInterest(face: FaceImpl) {
const lifetime = this.lastExpire - getNow();
const lifetime = this.lastExpire - performance.now();
if (lifetime <= 0) {
return;
}
Expand All @@ -78,15 +75,15 @@ export class PitEntry {
public *returnData(up: FaceImpl): Iterable<{ dn: FaceImpl; token: unknown }> {
clearTimeout(this.expireTimer);
this.pit.eraseEntry(this);
const now = getNow();
const now = performance.now();
for (const [dn, { expire, token }] of this.dnRecords) {
if (expire > now && dn !== up) {
yield { dn, token };
}
}
}

private updateExpire(now: number = getNow()) {
private updateExpire(now: number = performance.now()) {
let lastExpire = 0;
for (const { expire } of this.dnRecords.values()) {
lastExpire = Math.max(lastExpire, expire);
Expand Down
1 change: 0 additions & 1 deletion pkg/segmented-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@ndn/packet": "workspace:*",
"@ndn/util": "workspace:*",
"@zenfs/core": "^0.15.2",
"hirestime": "^7.0.4",
"it-keepalive": "^1.2.0",
"mnemonist": "^0.39.8",
"obliterator": "^2.0.4",
Expand Down
10 changes: 3 additions & 7 deletions pkg/segmented-object/src/fetch/unverified.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CancelInterest, Forwarder, type FwFace, type FwPacket } from "@ndn/fw";
import { Data, Interest, type Name } from "@ndn/packet";
import { pushable } from "@ndn/util";
import hirestime from "hirestime";
import itKeepAlive from "it-keepalive";
import take from "obliterator/take.js";

Expand All @@ -10,9 +9,6 @@ import type { CongestionAvoidance } from "./congestion-avoidance";
import { RttEstimator } from "./rtt-estimator";
import { TcpCubic } from "./tcp-cubic";

/** Precise clock for fetching algorithms. */
const getNow = hirestime();

export interface UnverifiedFetcherOptions {
/**
* Use the specified logical forwarder.
Expand Down Expand Up @@ -206,7 +202,7 @@ export class UnverifiedFetcher {
return;
}

const now = getNow();
const now = performance.now();
const rtt = now - fs.txTime;
if (fs.nRetx === 0) {
this.rtte.push(rtt, this.pendings.size);
Expand All @@ -229,7 +225,7 @@ export class UnverifiedFetcher {

/** Process RTO expirations on pending segments. */
private processRtoExpiry(): void {
const now = getNow();
const now = performance.now();
for (const [seg, fs] of this.pendings) {
if (seg > this.segLast) {
this.pendings.delete(seg);
Expand Down Expand Up @@ -286,7 +282,7 @@ export class UnverifiedFetcher {
/** Send an Interest and record TX time. */
private sendInterest(fs: SegState): void {
const rto = this.rtte.rto;
fs.txTime = getNow();
fs.txTime = performance.now();
fs.rtoExpiry = fs.txTime + rto;

fs.interest = new Interest();
Expand Down

0 comments on commit e0c16c4

Please sign in to comment.