socket-function 0.12.1 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
package/src/batching.ts CHANGED
@@ -29,6 +29,8 @@ export function delay(delayTime: DelayType): Promise<void> {
29
29
  // NOTE: setTimeout can't wait this short of a time, so just setImmediate. This should be hard to distinguish
30
30
  // anyways, as setImmediate (at least in nodejs), should happen after io, so... it should just work
31
31
  // (the only difference is there will be less unnecessary delay).
32
+ // NOTE: THIS DOES break certain cases where io is depending on true delay, and by only waiting a microtick
33
+ // we don't give it a chance. But... we should just handle those cases explicitly, via an explicit "afterio".
32
34
  if (delayTime < 10) {
33
35
  return delay("immediate");
34
36
  }
@@ -68,7 +70,7 @@ export function batchFunction<Arg, Result = void>(
68
70
  let curDelay = config.delay;
69
71
  let delayRamp = 0;
70
72
  if (config.throttleWindow && typeof curDelay === "number") {
71
- delayRamp = curDelay / config.throttleWindow;
73
+ delayRamp = curDelay / (config.throttleWindow / curDelay);
72
74
  }
73
75
  let delayTime = 0;
74
76
  if (typeof curDelay === "number") {
@@ -80,6 +82,10 @@ export function batchFunction<Arg, Result = void>(
80
82
  return arg => {
81
83
  let now = Date.now();
82
84
  if (delayRamp) {
85
+ // The time since the last call (started) is how much budget we will have received to
86
+ // run values. If it is === delayTime, then we subtract 1, as we are right on track.
87
+ // If it is > delayTime, then we are running below the rate, so it is fine.
88
+ // If it is < delayTime, we are running too fast, and have to slow down.
83
89
  let savedCount = (now - lastCall) / delayTime;
84
90
  if (savedCount >= 1) {
85
91
  countSinceBreak -= savedCount;
@@ -6,7 +6,7 @@ import { getOpenTimesBase, getOwnTime, OwnTimeObj } from "./getOwnTime";
6
6
  import { addToStats, addToStatsValue, createStatsValue, getStatsTop, StatsValue } from "./stats";
7
7
  import { white } from "../formatting/logColors";
8
8
  import { isNode } from "../misc";
9
- import { formatStats } from "./statsFormat";
9
+ import { formatStats, percent } from "./statsFormat";
10
10
 
11
11
  let measurementsDisabled = false;
12
12
  /** NOTE: Must be called BEFORE anything else is imported!
@@ -73,13 +73,21 @@ export function measureBlock<T extends (...args: any[]) => any>(fnc: T, name?: s
73
73
  return measureWrap(fnc, name)();
74
74
  }
75
75
 
76
+ let extraInfoGetters: (() => string | undefined)[] = [];
77
+ export function registerMeasureInfo(getInfo: () => string | undefined) {
78
+ extraInfoGetters.push(getInfo);
79
+ }
80
+
76
81
  export function startMeasure(): {
77
82
  finish: () => MeasureProfile;
78
83
  } {
79
84
  if (!measurementsEnabled && !measurementsDisabled) {
80
85
  console.warn(red(`To capture measurements enableMeasurements() must be called before any other imports in your entry point`));
81
86
  }
87
+ let now = Date.now();
82
88
  let profile: MeasureProfile = {
89
+ startTime: now,
90
+ endTime: now,
83
91
  entries: Object.create(null),
84
92
  };
85
93
  let openAtStart = new Set(getOpenTimesBase());
@@ -103,6 +111,7 @@ export function startMeasure(): {
103
111
  addToProfile(profile, timeObj);
104
112
  }
105
113
  outstandingProfiles.splice(outstandingProfiles.indexOf(profile), 1);
114
+ profile.endTime = Date.now();
106
115
  return profile;
107
116
  }
108
117
  };
@@ -111,6 +120,7 @@ export function startMeasure(): {
111
120
  export interface LogMeasureTableConfig {
112
121
  useTotalTime?: boolean;
113
122
  name?: string;
123
+ setTitle?: boolean;
114
124
  // Defaults to 0.05
115
125
  thresholdInTable?: number;
116
126
  // Details to 50
@@ -157,8 +167,18 @@ export function logMeasureTable(
157
167
  entries.sort((a, b) => getTime(b).sum - getTime(a).sum);
158
168
  }
159
169
 
170
+ let timeRunFor = profile.endTime - profile.startTime;
171
+ let fraction = totalTime / timeRunFor;
172
+
160
173
  console.log();
161
- let title = yellow(`Profiled ${formatTime(totalTime)} (logged at ${new Date().toISOString()})`);
174
+ let extraInfos = extraInfoGetters.map(x => x());
175
+
176
+ if (config?.setTitle && isNode()) {
177
+ let title = `${percent(fraction)} CPU`;
178
+ title += extraInfos.map(x => x ? ` // ${x}` : "").join("");
179
+ process.stdout.write(`\x1b]0;${title}\x07`);
180
+ }
181
+ let title = yellow(`Profiled ${formatTime(totalTime)} (${percent(fraction)} CPU)${extraInfos.map(x => x ? ` (${x})` : "")} (logged at ${new Date().toISOString()}, profile for ${formatTime(timeRunFor)})`);
162
182
  if (name) {
163
183
  title = `(${blue(name)}) ${title}`;
164
184
  }
@@ -229,12 +249,17 @@ function finishProfile(measure: { finish(): MeasureProfile }, config?: LogMeasur
229
249
 
230
250
 
231
251
  export interface MeasureProfile {
252
+ startTime: number;
253
+ endTime: number;
232
254
  entries: {
233
255
  [name: string]: ProfileEntry;
234
256
  };
235
257
  }
236
258
  export function createMeasureProfile(): MeasureProfile {
259
+ let now = Date.now();
237
260
  return {
261
+ startTime: now,
262
+ endTime: now,
238
263
  entries: Object.create(null),
239
264
  };
240
265
  }
@@ -1,3 +1,5 @@
1
+ // See statsFormat.ts:formatStats for a nice way to format a stats value
2
+
1
3
  export interface StatsValue {
2
4
  count: number;
3
5
  sum: number;
@@ -10,6 +10,7 @@ export function percent(value: number) {
10
10
  export function formatStats(stats: StatsValue, config?: {
11
11
  noColor?: boolean;
12
12
  noSum?: boolean;
13
+ noSpaces?: boolean;
13
14
  }) {
14
15
  function p(count: number, text: string | number) {
15
16
  return String(text).padStart(count, " ");
@@ -37,5 +38,8 @@ export function formatStats(stats: StatsValue, config?: {
37
38
  }
38
39
  equation = (!config?.noSum && `${p(6, sumText)} = ` || "") + `${p(6, topPart)} + ${bottomPart}`;
39
40
  }
41
+ if (config?.noSpaces) {
42
+ equation = equation.replace(/\s+/g, " ").trim();
43
+ }
40
44
  return equation;
41
45
  }