@libp2p/perf 2.0.0 → 2.0.1-97ab31c0c
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/README.md +18 -4
- package/dist/index.min.js +1 -1
- package/dist/src/index.d.ts +11 -10
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +7 -7
- package/dist/src/index.js.map +1 -1
- package/dist/src/perf-service.d.ts +4 -3
- package/dist/src/perf-service.d.ts.map +1 -1
- package/dist/src/perf-service.js +25 -11
- package/dist/src/perf-service.js.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +13 -12
- package/src/perf-service.ts +35 -15
- package/dist/typedoc-urls.json +0 -14
package/README.md
CHANGED
@@ -17,14 +17,14 @@ import { yamux } from '@chainsafe/libp2p-yamux'
|
|
17
17
|
import { mplex } from '@libp2p/mplex'
|
18
18
|
import { tcp } from '@libp2p/tcp'
|
19
19
|
import { createLibp2p, type Libp2p } from 'libp2p'
|
20
|
-
import { plaintext } from 'libp2p/
|
21
|
-
import {
|
20
|
+
import { plaintext } from '@libp2p/plaintext'
|
21
|
+
import { perf, type Perf } from '@libp2p/perf'
|
22
22
|
|
23
23
|
const ONE_MEG = 1024 * 1024
|
24
24
|
const UPLOAD_BYTES = ONE_MEG * 1024
|
25
25
|
const DOWNLOAD_BYTES = ONE_MEG * 1024
|
26
26
|
|
27
|
-
async function createNode (): Promise<Libp2p<{ perf:
|
27
|
+
async function createNode (): Promise<Libp2p<{ perf: Perf }>> {
|
28
28
|
return createLibp2p({
|
29
29
|
addresses: {
|
30
30
|
listen: [
|
@@ -41,7 +41,7 @@ async function createNode (): Promise<Libp2p<{ perf: PerfService }>> {
|
|
41
41
|
yamux(), mplex()
|
42
42
|
],
|
43
43
|
services: {
|
44
|
-
perf:
|
44
|
+
perf: perf()
|
45
45
|
}
|
46
46
|
})
|
47
47
|
}
|
@@ -57,6 +57,20 @@ await libp2p1.stop()
|
|
57
57
|
await libp2p2.stop()
|
58
58
|
```
|
59
59
|
|
60
|
+
# Install
|
61
|
+
|
62
|
+
```console
|
63
|
+
$ npm i @libp2p/perf
|
64
|
+
```
|
65
|
+
|
66
|
+
## Browser `<script>` tag
|
67
|
+
|
68
|
+
Loading this module through a script tag will make it's exports available as `Libp2pPerf` in the global namespace.
|
69
|
+
|
70
|
+
```html
|
71
|
+
<script src="https://unpkg.com/@libp2p/perf/dist/index.min.js"></script>
|
72
|
+
```
|
73
|
+
|
60
74
|
# API Docs
|
61
75
|
|
62
76
|
- <https://libp2p.github.io/js-libp2p/modules/_libp2p_perf.html>
|
package/dist/index.min.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Libp2PPerf = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
2
|
-
"use strict";var Libp2PPerf=(()=>{var le=Object.create;var I=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var he=Object.getPrototypeOf,pe=Object.prototype.hasOwnProperty;var j=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),be=(t,e)=>{for(var r in e)I(t,r,{get:e[r],enumerable:!0})},K=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of de(e))!pe.call(t,o)&&o!==r&&I(t,o,{get:()=>e[o],enumerable:!(n=fe(e,o))||n.enumerable});return t};var me=(t,e,r)=>(r=t!=null?le(he(t)):{},K(e||!t||!t.__esModule?I(r,"default",{value:t,enumerable:!0}):r,t)),we=t=>K(I({},"__esModule",{value:!0}),t);var W=j((qe,G)=>{var k=1e3,M=k*60,T=M*60,N=T*24,Ce=N*7,ge=N*365.25;G.exports=function(t,e){e=e||{};var r=typeof t;if(r==="string"&&t.length>0)return ye(t);if(r==="number"&&isFinite(t))return e.long?ve(t):xe(t);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(t))};function ye(t){if(t=String(t),!(t.length>100)){var e=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(t);if(e){var r=parseFloat(e[1]),n=(e[2]||"ms").toLowerCase();switch(n){case"years":case"year":case"yrs":case"yr":case"y":return r*ge;case"weeks":case"week":case"w":return r*Ce;case"days":case"day":case"d":return r*N;case"hours":case"hour":case"hrs":case"hr":case"h":return r*T;case"minutes":case"minute":case"mins":case"min":case"m":return r*M;case"seconds":case"second":case"secs":case"sec":case"s":return r*k;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return r;default:return}}}}function xe(t){var e=Math.abs(t);return e>=N?Math.round(t/N)+"d":e>=T?Math.round(t/T)+"h":e>=M?Math.round(t/M)+"m":e>=k?Math.round(t/k)+"s":t+"ms"}function ve(t){var e=Math.abs(t);return e>=N?R(t,e,N,"day"):e>=T?R(t,e,T,"hour"):e>=M?R(t,e,M,"minute"):e>=k?R(t,e,k,"second"):t+" ms"}function R(t,e,r,n){var o=e>=r*1.5;return Math.round(t/r)+" "+n+(o?"s":"")}});var Y=j((Je,Q)=>{function Fe(t){r.debug=r,r.default=r,r.coerce=b,r.disable=d,r.enable=o,r.enabled=h,r.humanize=W(),r.destroy=y,Object.keys(t).forEach(s=>{r[s]=t[s]}),r.names=[],r.skips=[],r.formatters={};function e(s){let a=0;for(let f=0;f<s.length;f++)a=(a<<5)-a+s.charCodeAt(f),a|=0;return r.colors[Math.abs(a)%r.colors.length]}r.selectColor=e;function r(s){let a,f=null,O,u;function l(...i){if(!l.enabled)return;let c=l,m=Number(new Date),w=m-(a||m);c.diff=w,c.prev=a,c.curr=m,a=m,i[0]=r.coerce(i[0]),typeof i[0]!="string"&&i.unshift("%O");let C=0;i[0]=i[0].replace(/%([a-zA-Z%])/g,(A,v)=>{if(A==="%%")return"%";C++;let E=r.formatters[v];if(typeof E=="function"){let U=i[C];A=E.call(c,U),i.splice(C,1),C--}return A}),r.formatArgs.call(c,i),(c.log||r.log).apply(c,i)}return l.namespace=s,l.useColors=r.useColors(),l.color=r.selectColor(s),l.extend=n,l.destroy=r.destroy,Object.defineProperty(l,"enabled",{enumerable:!0,configurable:!1,get:()=>f!==null?f:(O!==r.namespaces&&(O=r.namespaces,u=r.enabled(s)),u),set:i=>{f=i}}),typeof r.init=="function"&&r.init(l),l}function n(s,a){let f=r(this.namespace+(typeof a>"u"?":":a)+s);return f.log=this.log,f}function o(s){r.save(s),r.namespaces=s,r.names=[],r.skips=[];let a,f=(typeof s=="string"?s:"").split(/[\s,]+/),O=f.length;for(a=0;a<O;a++)f[a]&&(s=f[a].replace(/\*/g,".*?"),s[0]==="-"?r.skips.push(new RegExp("^"+s.slice(1)+"$")):r.names.push(new RegExp("^"+s+"$")))}function d(){let s=[...r.names.map(p),...r.skips.map(p).map(a=>"-"+a)].join(",");return r.enable(""),s}function h(s){if(s[s.length-1]==="*")return!0;let a,f;for(a=0,f=r.skips.length;a<f;a++)if(r.skips[a].test(s))return!1;for(a=0,f=r.names.length;a<f;a++)if(r.names[a].test(s))return!0;return!1}function p(s){return s.toString().substring(2,s.toString().length-2).replace(/\.\*\?$/,"*")}function b(s){return s instanceof Error?s.stack||s.message:s}function y(){console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.")}return r.enable(r.load()),r}Q.exports=Fe});var H=j((F,_)=>{F.formatArgs=Ae;F.save=Ee;F.load=Oe;F.useColors=Se;F.storage=Ne();F.destroy=(()=>{let t=!1;return()=>{t||(t=!0,console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."))}})();F.colors=["#0000CC","#0000FF","#0033CC","#0033FF","#0066CC","#0066FF","#0099CC","#0099FF","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#3300CC","#3300FF","#3333CC","#3333FF","#3366CC","#3366FF","#3399CC","#3399FF","#33CC00","#33CC33","#33CC66","#33CC99","#33CCCC","#33CCFF","#6600CC","#6600FF","#6633CC","#6633FF","#66CC00","#66CC33","#9900CC","#9900FF","#9933CC","#9933FF","#99CC00","#99CC33","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF","#CC6600","#CC6633","#CC9900","#CC9933","#CCCC00","#CCCC33","#FF0000","#FF0033","#FF0066","#FF0099","#FF00CC","#FF00FF","#FF3300","#FF3333","#FF3366","#FF3399","#FF33CC","#FF33FF","#FF6600","#FF6633","#FF9900","#FF9933","#FFCC00","#FFCC33"];function Se(){return typeof window<"u"&&window.process&&(window.process.type==="renderer"||window.process.__nwjs)?!0:typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)?!1:typeof document<"u"&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||typeof window<"u"&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31||typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)}function Ae(t){if(t[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+t[0]+(this.useColors?"%c ":" ")+"+"+_.exports.humanize(this.diff),!this.useColors)return;let e="color: "+this.color;t.splice(1,0,e,"color: inherit");let r=0,n=0;t[0].replace(/%[a-zA-Z%]/g,o=>{o!=="%%"&&(r++,o==="%c"&&(n=r))}),t.splice(n,0,e)}F.log=console.debug||console.log||(()=>{});function Ee(t){try{t?F.storage.setItem("debug",t):F.storage.removeItem("debug")}catch{}}function Oe(){let t;try{t=F.storage.getItem("debug")}catch{}return!t&&typeof process<"u"&&"env"in process&&(t=process.env.DEBUG),t}function Ne(){try{return localStorage}catch{}}_.exports=Y()(F);var{formatters:Ue}=_.exports;Ue.j=function(t){try{return JSON.stringify(t)}catch(e){return"[UnexpectedJSONParseError]: "+e.message}}});var $e={};be($e,{perfService:()=>je});var S=me(H(),1);function ke(t,e){if(t.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var o=0;o<t.length;o++){var d=t.charAt(o),h=d.charCodeAt(0);if(r[h]!==255)throw new TypeError(d+" is ambiguous");r[h]=o}var p=t.length,b=t.charAt(0),y=Math.log(p)/Math.log(256),s=Math.log(256)/Math.log(p);function a(u){if(u instanceof Uint8Array||(ArrayBuffer.isView(u)?u=new Uint8Array(u.buffer,u.byteOffset,u.byteLength):Array.isArray(u)&&(u=Uint8Array.from(u))),!(u instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(u.length===0)return"";for(var l=0,i=0,c=0,m=u.length;c!==m&&u[c]===0;)c++,l++;for(var w=(m-c)*s+1>>>0,C=new Uint8Array(w);c!==m;){for(var x=u[c],A=0,v=w-1;(x!==0||A<i)&&v!==-1;v--,A++)x+=256*C[v]>>>0,C[v]=x%p>>>0,x=x/p>>>0;if(x!==0)throw new Error("Non-zero carry");i=A,c++}for(var E=w-i;E!==w&&C[E]===0;)E++;for(var U=b.repeat(l);E<w;++E)U+=t.charAt(C[E]);return U}function f(u){if(typeof u!="string")throw new TypeError("Expected String");if(u.length===0)return new Uint8Array;var l=0;if(u[l]!==" "){for(var i=0,c=0;u[l]===b;)i++,l++;for(var m=(u.length-l)*y+1>>>0,w=new Uint8Array(m);u[l];){var C=r[u.charCodeAt(l)];if(C===255)return;for(var x=0,A=m-1;(C!==0||x<c)&&A!==-1;A--,x++)C+=p*w[A]>>>0,w[A]=C%256>>>0,C=C/256>>>0;if(C!==0)throw new Error("Non-zero carry");c=x,l++}if(u[l]!==" "){for(var v=m-c;v!==m&&w[v]===0;)v++;for(var E=new Uint8Array(i+(m-v)),U=i;v!==m;)E[U++]=w[v++];return E}}}function O(u){var l=f(u);if(l)return l;throw new Error(`Non-${e} character`)}return{encode:a,decodeUnsafe:f,decode:O}}var Me=ke,Te=Me,ee=Te;var Ze=new Uint8Array(0);var te=t=>{if(t instanceof Uint8Array&&t.constructor.name==="Uint8Array")return t;if(t instanceof ArrayBuffer)return new Uint8Array(t);if(ArrayBuffer.isView(t))return new Uint8Array(t.buffer,t.byteOffset,t.byteLength);throw new Error("Unknown type, must be binary type")};var $=class{constructor(e,r,n){this.name=e,this.prefix=r,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},V=class{constructor(e,r,n){if(this.name=e,this.prefix=r,r.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=r.codePointAt(0),this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return re(this,e)}},q=class{constructor(e){this.decoders=e}or(e){return re(this,e)}decode(e){let r=e[0],n=this.decoders[r];if(n)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},re=(t,e)=>new q({...t.decoders||{[t.prefix]:t},...e.decoders||{[e.prefix]:e}}),J=class{constructor(e,r,n,o){this.name=e,this.prefix=r,this.baseEncode=n,this.baseDecode=o,this.encoder=new $(e,r,n),this.decoder=new V(e,r,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}},ne=({name:t,prefix:e,encode:r,decode:n})=>new J(t,e,r,n),X=({prefix:t,name:e,alphabet:r})=>{let{encode:n,decode:o}=ee(r,e);return ne({prefix:t,name:e,encode:n,decode:d=>te(o(d))})},Be=(t,e,r,n)=>{let o={};for(let s=0;s<e.length;++s)o[e[s]]=s;let d=t.length;for(;t[d-1]==="=";)--d;let h=new Uint8Array(d*r/8|0),p=0,b=0,y=0;for(let s=0;s<d;++s){let a=o[t[s]];if(a===void 0)throw new SyntaxError(`Non-${n} character`);b=b<<r|a,p+=r,p>=8&&(p-=8,h[y++]=255&b>>p)}if(p>=r||255&b<<8-p)throw new SyntaxError("Unexpected end of data");return h},ze=(t,e,r)=>{let n=e[e.length-1]==="=",o=(1<<r)-1,d="",h=0,p=0;for(let b=0;b<t.length;++b)for(p=p<<8|t[b],h+=8;h>r;)h-=r,d+=e[o&p>>h];if(h&&(d+=e[o&p<<r-h]),n)for(;d.length*r&7;)d+="=";return d},g=({name:t,prefix:e,bitsPerChar:r,alphabet:n})=>ne({prefix:e,name:t,encode(o){return ze(o,n,r)},decode(o){return Be(o,n,r,t)}});var oe=g({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),He=g({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),et=g({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),tt=g({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),rt=g({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),nt=g({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),ot=g({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),st=g({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),it=g({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var se=X({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),ct=X({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var ie=g({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),dt=g({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),ht=g({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),pt=g({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});S.default.formatters.b=t=>t==null?"undefined":se.baseEncode(t);S.default.formatters.t=t=>t==null?"undefined":oe.baseEncode(t);S.default.formatters.m=t=>t==null?"undefined":ie.baseEncode(t);S.default.formatters.p=t=>t==null?"undefined":t.toString();S.default.formatters.c=t=>t==null?"undefined":t.toString();S.default.formatters.k=t=>t==null?"undefined":t.toString();S.default.formatters.a=t=>t==null?"undefined":t.toString();function Ie(t){let e=()=>{};return e.enabled=!1,e.color="",e.diff=0,e.log=()=>{},e.namespace=t,e.destroy=()=>!0,e.extend=()=>e,e}function ae(t){let e=Ie(`${t}:trace`);return S.default.enabled(`${t}:trace`)&&S.default.names.map(r=>r.toString()).find(r=>r.includes(":trace"))!=null&&(e=(0,S.default)(`${t}:trace`)),Object.assign((0,S.default)(t),{error:(0,S.default)(`${t}:error`),trace:e})}function D(){let t={};return t.promise=new Promise((e,r)=>{t.resolve=e,t.reject=r}),t}var L=class{buffer;mask;top;btm;next;constructor(e){if(!(e>0)||e-1&e)throw new Error("Max size for a FixedFIFO should be a power of two");this.buffer=new Array(e),this.mask=e-1,this.top=0,this.btm=0,this.next=null}push(e){return this.buffer[this.top]!==void 0?!1:(this.buffer[this.top]=e,this.top=this.top+1&this.mask,!0)}shift(){let e=this.buffer[this.btm];if(e!==void 0)return this.buffer[this.btm]=void 0,this.btm=this.btm+1&this.mask,e}isEmpty(){return this.buffer[this.btm]===void 0}},B=class{size;hwm;head;tail;constructor(e={}){this.hwm=e.splitLimit??16,this.head=new L(this.hwm),this.tail=this.head,this.size=0}calculateSize(e){return e?.byteLength!=null?e.byteLength:1}push(e){if(e?.value!=null&&(this.size+=this.calculateSize(e.value)),!this.head.push(e)){let r=this.head;this.head=r.next=new L(2*this.head.buffer.length),this.head.push(e)}}shift(){let e=this.tail.shift();if(e===void 0&&this.tail.next!=null){let r=this.tail.next;this.tail.next=null,this.tail=r,e=this.tail.shift()}return e?.value!=null&&(this.size-=this.calculateSize(e.value)),e}isEmpty(){return this.head.isEmpty()}};var Z=class extends Error{type;code;constructor(e,r){super(e??"The operation was aborted"),this.type="aborted",this.code=r??"ABORT_ERR"}};function ue(t={}){return Re(r=>{let n=r.shift();if(n==null)return{done:!0};if(n.error!=null)throw n.error;return{done:n.done===!0,value:n.value}},t)}function Re(t,e){e=e??{};let r=e.onEnd,n=new B,o,d,h,p=D(),b=async()=>{try{return n.isEmpty()?h?{done:!0}:await new Promise((i,c)=>{d=m=>{d=null,n.push(m);try{i(t(n))}catch(w){c(w)}return o}}):t(n)}finally{n.isEmpty()&&queueMicrotask(()=>{p.resolve(),p=D()})}},y=i=>d!=null?d(i):(n.push(i),o),s=i=>(n=new B,d!=null?d({error:i}):(n.push({error:i}),o)),a=i=>{if(h)return o;if(e?.objectMode!==!0&&i?.byteLength==null)throw new Error("objectMode was not true but tried to push non-Uint8Array value");return y({done:!1,value:i})},f=i=>h?o:(h=!0,i!=null?s(i):y({done:!0})),O=()=>(n=new B,f(),{done:!0}),u=i=>(f(i),{done:!0});if(o={[Symbol.asyncIterator](){return this},next:b,return:O,throw:u,push:a,end:f,get readableLength(){return n.size},onEmpty:async i=>{let c=i?.signal;if(c?.throwIfAborted(),n.isEmpty())return;let m,w;c!=null&&(m=new Promise((C,x)=>{w=()=>{x(new Z)},c.addEventListener("abort",w)}));try{await Promise.race([p.promise,m])}finally{w!=null&&c!=null&&c?.removeEventListener("abort",w)}}},r==null)return o;let l=o;return o={[Symbol.asyncIterator](){return this},next(){return l.next()},throw(i){return l.throw(i),r!=null&&(r(i),r=void 0),{done:!0}},return(){return l.return(),r!=null&&(r(),r=void 0),{done:!0}},push:a,end(i){return l.end(i),r!=null&&(r(i),r=void 0),o},get readableLength(){return l.readableLength}},o}var ce="/perf/1.0.0";var z=ae("libp2p:perf"),P=class{protocol;components;started;databuf;writeBlockSize;maxInboundStreams;maxOutboundStreams;runOnTransientConnection;constructor(e,r={}){this.components=e,this.started=!1,this.protocol=r.protocolName??ce,this.writeBlockSize=r.writeBlockSize??65536,this.databuf=new ArrayBuffer(this.writeBlockSize),this.maxInboundStreams=r.maxInboundStreams??1,this.maxOutboundStreams=r.maxOutboundStreams??1,this.runOnTransientConnection=r.runOnTransientConnection??!1}async start(){await this.components.registrar.handle(this.protocol,e=>{this.handleMessage(e).catch(r=>{z.error("error handling perf protocol message",r)})},{maxInboundStreams:this.maxInboundStreams,maxOutboundStreams:this.maxOutboundStreams,runOnTransientConnection:this.runOnTransientConnection}),this.started=!0}async stop(){await this.components.registrar.unhandle(this.protocol),this.started=!1}isStarted(){return this.started}async handleMessage(e){let{stream:r}=e;try{let n=this.writeBlockSize,o;for await(let h of r.source)o==null&&(o=Number(h.getBigUint64(0,!1)));if(o==null)throw new Error("bytesToSendBack was not set");let d=new Uint8Array(this.databuf);await r.sink(async function*(){for(;o>0;){let h=n;h>o&&(h=o),o=o-h,yield d.subarray(0,h)}}())}catch(n){r.abort(n)}}async*measurePerformance(e,r,n,o={}){z("opening stream on protocol %s to %a",this.protocol,e);let d=new Uint8Array(this.databuf),h=this.writeBlockSize,p=Date.now(),b=await this.components.connectionManager.openConnection(e,{...o,force:o.reuseExistingConnection!==!0}),y=await b.newStream(this.protocol,o),s=0,a=Date.now(),f=0;new DataView(this.databuf).setBigUint64(0,BigInt(n),!1),z("sending %i bytes to %p",r,b.remotePeer);try{let u=ue({objectMode:!0});y.sink(async function*(){for(yield d.subarray(0,8);r>0;){o.signal?.throwIfAborted();let c=h;c>r&&(c=r),r=r-c,yield d.subarray(0,c),Date.now()-a>1e3&&(u.push({type:"intermediary",timeSeconds:(Date.now()-a)/1e3,uploadBytes:s,downloadBytes:0}),a=Date.now(),s=0),s+=c,f+=c}u.end()}()).catch(c=>{u.end(c)}),yield*u;let l=0;a=Date.now();let i=0;for await(let c of y.source)o.signal?.throwIfAborted(),Date.now()-a>1e3&&(yield{type:"intermediary",timeSeconds:(Date.now()-a)/1e3,uploadBytes:0,downloadBytes:l},a=Date.now(),l=0),l+=c.byteLength,i+=c.byteLength;if(i!==n)throw new Error(`Expected to receive ${n} bytes, but received ${i}`);yield{type:"final",timeSeconds:(Date.now()-p)/1e3,uploadBytes:f,downloadBytes:i},z("performed %s to %p",this.protocol,b.remotePeer),await y.close()}catch(u){throw z("error sending %s bytes to %p: %s",f,b.remotePeer,u),y.abort(u),u}}};function je(t={}){return e=>new P(e,t)}return we($e);})();
|
2
|
+
"use strict";var Libp2PPerf=(()=>{var O=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var k=(s,t)=>{for(var e in t)O(s,e,{get:t[e],enumerable:!0})},M=(s,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of I(t))!v.call(s,n)&&n!==e&&O(s,n,{get:()=>t[n],enumerable:!(r=D(t,n))||r.enumerable});return s};var R=s=>M(O({},"__esModule",{value:!0}),s);var j={};k(j,{perf:()=>X});function x(){let s={};return s.promise=new Promise((t,e)=>{s.resolve=t,s.reject=e}),s}var g=class{buffer;mask;top;btm;next;constructor(t){if(!(t>0)||t-1&t)throw new Error("Max size for a FixedFIFO should be a power of two");this.buffer=new Array(t),this.mask=t-1,this.top=0,this.btm=0,this.next=null}push(t){return this.buffer[this.top]!==void 0?!1:(this.buffer[this.top]=t,this.top=this.top+1&this.mask,!0)}shift(){let t=this.buffer[this.btm];if(t!==void 0)return this.buffer[this.btm]=void 0,this.btm=this.btm+1&this.mask,t}isEmpty(){return this.buffer[this.btm]===void 0}},b=class{size;hwm;head;tail;constructor(t={}){this.hwm=t.splitLimit??16,this.head=new g(this.hwm),this.tail=this.head,this.size=0}calculateSize(t){return t?.byteLength!=null?t.byteLength:1}push(t){if(t?.value!=null&&(this.size+=this.calculateSize(t.value)),!this.head.push(t)){let e=this.head;this.head=e.next=new g(2*this.head.buffer.length),this.head.push(t)}}shift(){let t=this.tail.shift();if(t===void 0&&this.tail.next!=null){let e=this.tail.next;this.tail.next=null,this.tail=e,t=this.tail.shift()}return t?.value!=null&&(this.size-=this.calculateSize(t.value)),t}isEmpty(){return this.head.isEmpty()}};var N=class extends Error{type;code;constructor(t,e){super(t??"The operation was aborted"),this.type="aborted",this.code=e??"ABORT_ERR"}};function T(s={}){return z(e=>{let r=e.shift();if(r==null)return{done:!0};if(r.error!=null)throw r.error;return{done:r.done===!0,value:r.value}},s)}function z(s,t){t=t??{};let e=t.onEnd,r=new b,n,l,u,a=x(),p=async()=>{try{return r.isEmpty()?u?{done:!0}:await new Promise((o,i)=>{l=S=>{l=null,r.push(S);try{o(s(r))}catch(w){i(w)}return n}}):s(r)}finally{r.isEmpty()&&queueMicrotask(()=>{a.resolve(),a=x()})}},c=o=>l!=null?l(o):(r.push(o),n),y=o=>(r=new b,l!=null?l({error:o}):(r.push({error:o}),n)),m=o=>{if(u)return n;if(t?.objectMode!==!0&&o?.byteLength==null)throw new Error("objectMode was not true but tried to push non-Uint8Array value");return c({done:!1,value:o})},f=o=>u?n:(u=!0,o!=null?y(o):c({done:!0})),B=()=>(r=new b,f(),{done:!0}),h=o=>(f(o),{done:!0});if(n={[Symbol.asyncIterator](){return this},next:p,return:B,throw:h,push:m,end:f,get readableLength(){return r.size},onEmpty:async o=>{let i=o?.signal;if(i?.throwIfAborted(),r.isEmpty())return;let S,w;i!=null&&(S=new Promise((K,A)=>{w=()=>{A(new N)},i.addEventListener("abort",w)}));try{await Promise.race([a.promise,S])}finally{w!=null&&i!=null&&i?.removeEventListener("abort",w)}}},e==null)return n;let d=n;return n={[Symbol.asyncIterator](){return this},next(){return d.next()},throw(o){return d.throw(o),e!=null&&(e(o),e=void 0),{done:!0}},return(){return d.return(),e!=null&&(e(),e=void 0),{done:!0}},push:m,end(o){return d.end(o),e!=null&&(e(o),e=void 0),n},get readableLength(){return d.readableLength}},n}var _="/perf/1.0.0";var E=class{log;protocol;components;started;databuf;writeBlockSize;maxInboundStreams;maxOutboundStreams;runOnTransientConnection;constructor(t,e={}){this.components=t,this.log=t.logger.forComponent("libp2p:perf"),this.started=!1,this.protocol=e.protocolName??_,this.writeBlockSize=e.writeBlockSize??65536,this.databuf=new ArrayBuffer(this.writeBlockSize),this.maxInboundStreams=e.maxInboundStreams??1,this.maxOutboundStreams=e.maxOutboundStreams??1,this.runOnTransientConnection=e.runOnTransientConnection??!1}async start(){await this.components.registrar.handle(this.protocol,t=>{this.handleMessage(t).catch(e=>{this.log.error("error handling perf protocol message",e)})},{maxInboundStreams:this.maxInboundStreams,maxOutboundStreams:this.maxOutboundStreams,runOnTransientConnection:this.runOnTransientConnection}),this.started=!0}async stop(){await this.components.registrar.unhandle(this.protocol),this.started=!1}isStarted(){return this.started}async handleMessage(t){let{stream:e}=t;try{let r=this.writeBlockSize,n;for await(let u of e.source)n==null&&(n=Number(u.getBigUint64(0,!1)));if(n==null)throw new Error("bytesToSendBack was not set");let l=new Uint8Array(this.databuf);await e.sink(async function*(){for(;n>0;){let u=r;u>n&&(u=n),n=n-u,yield l.subarray(0,u)}}())}catch(r){e.abort(r)}}async*measurePerformance(t,e,r,n={}){this.log("opening stream on protocol %s to %a",this.protocol,t);let l=new Uint8Array(this.databuf),u=this.writeBlockSize,a=Date.now(),p=await this.components.connectionManager.openConnection(t,{...n,force:n.reuseExistingConnection!==!0});yield{type:"connection",timeSeconds:(Date.now()-a)/1e3,uploadBytes:0,downloadBytes:0},a=Date.now();let c=await p.newStream(this.protocol,n),y=Date.now();yield{type:"stream",timeSeconds:(Date.now()-a)/1e3,uploadBytes:0,downloadBytes:0};let m=0;a=Date.now();let f=0;new DataView(this.databuf).setBigUint64(0,BigInt(r),!1),this.log("sending %i bytes to %p",e,p.remotePeer);try{let h=T({objectMode:!0});c.sink(async function*(){for(yield l.subarray(0,8),y=Date.now();e>0;){n.signal?.throwIfAborted();let i=u;i>e&&(i=e),e=e-i,yield l.subarray(0,i),Date.now()-a>1e3&&(h.push({type:"intermediary",timeSeconds:(Date.now()-a)/1e3,uploadBytes:m,downloadBytes:0}),a=Date.now(),m=0),m+=i,f+=i}h.end()}()).catch(i=>{h.end(i)}),yield*h;let d=0;a=Date.now();let o=0;for await(let i of c.source)n.signal?.throwIfAborted(),Date.now()-a>1e3&&(yield{type:"intermediary",timeSeconds:(Date.now()-a)/1e3,uploadBytes:0,downloadBytes:d},a=Date.now(),d=0),d+=i.byteLength,o+=i.byteLength;if(o!==r)throw new Error(`Expected to receive ${r} bytes, but received ${o}`);yield{type:"final",timeSeconds:(Date.now()-y)/1e3,uploadBytes:f,downloadBytes:o},this.log("performed %s to %p",this.protocol,p.remotePeer),await c.close()}catch(h){throw this.log("error sending %d/%d bytes to %p: %s",f,e,p.remotePeer,h),c.abort(h),h}}};function X(s={}){return t=>new E(t,s)}return R(j);})();
|
3
3
|
return Libp2PPerf}));
|
package/dist/src/index.d.ts
CHANGED
@@ -11,14 +11,14 @@
|
|
11
11
|
* import { mplex } from '@libp2p/mplex'
|
12
12
|
* import { tcp } from '@libp2p/tcp'
|
13
13
|
* import { createLibp2p, type Libp2p } from 'libp2p'
|
14
|
-
* import { plaintext } from 'libp2p/
|
15
|
-
* import {
|
14
|
+
* import { plaintext } from '@libp2p/plaintext'
|
15
|
+
* import { perf, type Perf } from '@libp2p/perf'
|
16
16
|
*
|
17
17
|
* const ONE_MEG = 1024 * 1024
|
18
18
|
* const UPLOAD_BYTES = ONE_MEG * 1024
|
19
19
|
* const DOWNLOAD_BYTES = ONE_MEG * 1024
|
20
20
|
*
|
21
|
-
* async function createNode (): Promise<Libp2p<{ perf:
|
21
|
+
* async function createNode (): Promise<Libp2p<{ perf: Perf }>> {
|
22
22
|
* return createLibp2p({
|
23
23
|
* addresses: {
|
24
24
|
* listen: [
|
@@ -35,7 +35,7 @@
|
|
35
35
|
* yamux(), mplex()
|
36
36
|
* ],
|
37
37
|
* services: {
|
38
|
-
* perf:
|
38
|
+
* perf: perf()
|
39
39
|
* }
|
40
40
|
* })
|
41
41
|
* }
|
@@ -51,7 +51,7 @@
|
|
51
51
|
* await libp2p2.stop()
|
52
52
|
* ```
|
53
53
|
*/
|
54
|
-
import type { AbortOptions } from '@libp2p/interface';
|
54
|
+
import type { AbortOptions, ComponentLogger } from '@libp2p/interface';
|
55
55
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager';
|
56
56
|
import type { Registrar } from '@libp2p/interface-internal/registrar';
|
57
57
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
@@ -65,16 +65,16 @@ export interface PerfOptions extends AbortOptions {
|
|
65
65
|
*/
|
66
66
|
reuseExistingConnection?: boolean;
|
67
67
|
}
|
68
|
-
export interface
|
68
|
+
export interface Perf {
|
69
69
|
measurePerformance(multiaddr: Multiaddr, sendBytes: number, recvBytes: number, options?: PerfOptions): AsyncGenerator<PerfOutput>;
|
70
70
|
}
|
71
71
|
export interface PerfOutput {
|
72
|
-
type: 'intermediary' | 'final';
|
72
|
+
type: 'connection' | 'stream' | 'intermediary' | 'final';
|
73
73
|
timeSeconds: number;
|
74
74
|
uploadBytes: number;
|
75
75
|
downloadBytes: number;
|
76
76
|
}
|
77
|
-
export interface
|
77
|
+
export interface PerfInit {
|
78
78
|
protocolName?: string;
|
79
79
|
maxInboundStreams?: number;
|
80
80
|
maxOutboundStreams?: number;
|
@@ -84,9 +84,10 @@ export interface PerfServiceInit {
|
|
84
84
|
*/
|
85
85
|
writeBlockSize?: number;
|
86
86
|
}
|
87
|
-
export interface
|
87
|
+
export interface PerfComponents {
|
88
88
|
registrar: Registrar;
|
89
89
|
connectionManager: ConnectionManager;
|
90
|
+
logger: ComponentLogger;
|
90
91
|
}
|
91
|
-
export declare function
|
92
|
+
export declare function perf(init?: PerfInit): (components: PerfComponents) => Perf;
|
92
93
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,+CAA+C,CAAA;AACtF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAA;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,WAAY,SAAQ,YAAY;IAC/C;;;;;;OAMG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAA;CAClC;AAED,MAAM,WAAW,IAAI;IACnB,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,cAAc,CAAC,UAAU,CAAC,CAAA;CAClI;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,YAAY,GAAG,QAAQ,GAAG,cAAc,GAAG,OAAO,CAAA;IACxD,WAAW,EAAE,MAAM,CAAA;IACnB,WAAW,EAAE,MAAM,CAAA;IACnB,aAAa,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,QAAQ;IACvB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,wBAAwB,CAAC,EAAE,OAAO,CAAA;IAElC;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AAED,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,wBAAgB,IAAI,CAAE,IAAI,GAAE,QAAa,GAAG,CAAC,UAAU,EAAE,cAAc,KAAK,IAAI,CAE/E"}
|
package/dist/src/index.js
CHANGED
@@ -11,14 +11,14 @@
|
|
11
11
|
* import { mplex } from '@libp2p/mplex'
|
12
12
|
* import { tcp } from '@libp2p/tcp'
|
13
13
|
* import { createLibp2p, type Libp2p } from 'libp2p'
|
14
|
-
* import { plaintext } from 'libp2p/
|
15
|
-
* import {
|
14
|
+
* import { plaintext } from '@libp2p/plaintext'
|
15
|
+
* import { perf, type Perf } from '@libp2p/perf'
|
16
16
|
*
|
17
17
|
* const ONE_MEG = 1024 * 1024
|
18
18
|
* const UPLOAD_BYTES = ONE_MEG * 1024
|
19
19
|
* const DOWNLOAD_BYTES = ONE_MEG * 1024
|
20
20
|
*
|
21
|
-
* async function createNode (): Promise<Libp2p<{ perf:
|
21
|
+
* async function createNode (): Promise<Libp2p<{ perf: Perf }>> {
|
22
22
|
* return createLibp2p({
|
23
23
|
* addresses: {
|
24
24
|
* listen: [
|
@@ -35,7 +35,7 @@
|
|
35
35
|
* yamux(), mplex()
|
36
36
|
* ],
|
37
37
|
* services: {
|
38
|
-
* perf:
|
38
|
+
* perf: perf()
|
39
39
|
* }
|
40
40
|
* })
|
41
41
|
* }
|
@@ -51,8 +51,8 @@
|
|
51
51
|
* await libp2p2.stop()
|
52
52
|
* ```
|
53
53
|
*/
|
54
|
-
import {
|
55
|
-
export function
|
56
|
-
return (components) => new
|
54
|
+
import { Perf as PerfClass } from './perf-service.js';
|
55
|
+
export function perf(init = {}) {
|
56
|
+
return (components) => new PerfClass(components, init);
|
57
57
|
}
|
58
58
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AAEH,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AAEH,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,mBAAmB,CAAA;AA8CrD,MAAM,UAAU,IAAI,CAAE,OAAiB,EAAE;IACvC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AACxD,CAAC"}
|
@@ -1,8 +1,9 @@
|
|
1
|
-
import type { PerfOptions, PerfOutput,
|
1
|
+
import type { PerfOptions, PerfOutput, PerfComponents, PerfInit, Perf as PerfInterface } from './index.js';
|
2
2
|
import type { Startable } from '@libp2p/interface/startable';
|
3
3
|
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar';
|
4
4
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
5
|
-
export declare class
|
5
|
+
export declare class Perf implements Startable, PerfInterface {
|
6
|
+
private readonly log;
|
6
7
|
readonly protocol: string;
|
7
8
|
private readonly components;
|
8
9
|
private started;
|
@@ -11,7 +12,7 @@ export declare class PerfService implements Startable, PerfServiceInterface {
|
|
11
12
|
private readonly maxInboundStreams;
|
12
13
|
private readonly maxOutboundStreams;
|
13
14
|
private readonly runOnTransientConnection;
|
14
|
-
constructor(components:
|
15
|
+
constructor(components: PerfComponents, init?: PerfInit);
|
15
16
|
start(): Promise<void>;
|
16
17
|
stop(): Promise<void>;
|
17
18
|
isStarted(): boolean;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"perf-service.d.ts","sourceRoot":"","sources":["../../src/perf-service.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"perf-service.d.ts","sourceRoot":"","sources":["../../src/perf-service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,IAAI,IAAI,aAAa,EAAE,MAAM,YAAY,CAAA;AAE1G,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAA;AAC9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,qBAAa,IAAK,YAAW,SAAS,EAAE,aAAa;IACnD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,SAAgB,QAAQ,EAAE,MAAM,CAAA;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgB;IAC3C,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAa;IACrC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAQ;IACvC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAQ;IAC1C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAQ;IAC3C,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAAS;gBAErC,UAAU,EAAE,cAAc,EAAE,IAAI,GAAE,QAAa;IAYtD,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAavB,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAK5B,SAAS,IAAK,OAAO;IAIf,aAAa,CAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;IAsCrD,kBAAkB,CAAE,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,GAAE,WAAgB,GAAG,cAAc,CAAC,UAAU,CAAC;CAuI3I"}
|
package/dist/src/perf-service.js
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
import { logger } from '@libp2p/logger';
|
2
1
|
import { pushable } from 'it-pushable';
|
3
2
|
import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, RUN_ON_TRANSIENT_CONNECTION, WRITE_BLOCK_SIZE } from './constants.js';
|
4
|
-
|
5
|
-
|
3
|
+
export class Perf {
|
4
|
+
log;
|
6
5
|
protocol;
|
7
6
|
components;
|
8
7
|
started;
|
@@ -13,6 +12,7 @@ export class PerfService {
|
|
13
12
|
runOnTransientConnection;
|
14
13
|
constructor(components, init = {}) {
|
15
14
|
this.components = components;
|
15
|
+
this.log = components.logger.forComponent('libp2p:perf');
|
16
16
|
this.started = false;
|
17
17
|
this.protocol = init.protocolName ?? PROTOCOL_NAME;
|
18
18
|
this.writeBlockSize = init.writeBlockSize ?? WRITE_BLOCK_SIZE;
|
@@ -24,7 +24,7 @@ export class PerfService {
|
|
24
24
|
async start() {
|
25
25
|
await this.components.registrar.handle(this.protocol, (data) => {
|
26
26
|
void this.handleMessage(data).catch((err) => {
|
27
|
-
log.error('error handling perf protocol message', err);
|
27
|
+
this.log.error('error handling perf protocol message', err);
|
28
28
|
});
|
29
29
|
}, {
|
30
30
|
maxInboundStreams: this.maxInboundStreams,
|
@@ -72,24 +72,37 @@ export class PerfService {
|
|
72
72
|
}
|
73
73
|
}
|
74
74
|
async *measurePerformance(ma, sendBytes, receiveBytes, options = {}) {
|
75
|
-
log('opening stream on protocol %s to %a', this.protocol, ma);
|
75
|
+
this.log('opening stream on protocol %s to %a', this.protocol, ma);
|
76
76
|
const uint8Buf = new Uint8Array(this.databuf);
|
77
77
|
const writeBlockSize = this.writeBlockSize;
|
78
|
-
|
79
|
-
const initialStartTime = Date.now();
|
78
|
+
let lastReportedTime = Date.now();
|
80
79
|
const connection = await this.components.connectionManager.openConnection(ma, {
|
81
80
|
...options,
|
82
81
|
force: options.reuseExistingConnection !== true
|
83
82
|
});
|
83
|
+
yield {
|
84
|
+
type: 'connection',
|
85
|
+
timeSeconds: (Date.now() - lastReportedTime) / 1000,
|
86
|
+
uploadBytes: 0,
|
87
|
+
downloadBytes: 0
|
88
|
+
};
|
89
|
+
lastReportedTime = Date.now();
|
84
90
|
const stream = await connection.newStream(this.protocol, options);
|
91
|
+
let initialStartTime = Date.now();
|
92
|
+
yield {
|
93
|
+
type: 'stream',
|
94
|
+
timeSeconds: (Date.now() - lastReportedTime) / 1000,
|
95
|
+
uploadBytes: 0,
|
96
|
+
downloadBytes: 0
|
97
|
+
};
|
85
98
|
let lastAmountOfBytesSent = 0;
|
86
|
-
|
99
|
+
lastReportedTime = Date.now();
|
87
100
|
let totalBytesSent = 0;
|
88
101
|
// tell the remote how many bytes we will send. Up cast to 64 bit number
|
89
102
|
// as if we send as ui32 we limit total transfer size to 4GB
|
90
103
|
const view = new DataView(this.databuf);
|
91
104
|
view.setBigUint64(0, BigInt(receiveBytes), false);
|
92
|
-
log('sending %i bytes to %p', sendBytes, connection.remotePeer);
|
105
|
+
this.log('sending %i bytes to %p', sendBytes, connection.remotePeer);
|
93
106
|
try {
|
94
107
|
const sendOutput = pushable({
|
95
108
|
objectMode: true
|
@@ -97,6 +110,7 @@ export class PerfService {
|
|
97
110
|
void stream.sink(async function* () {
|
98
111
|
// Send the number of bytes to receive
|
99
112
|
yield uint8Buf.subarray(0, 8);
|
113
|
+
initialStartTime = Date.now();
|
100
114
|
while (sendBytes > 0) {
|
101
115
|
options.signal?.throwIfAborted();
|
102
116
|
let toSend = writeBlockSize;
|
@@ -156,11 +170,11 @@ export class PerfService {
|
|
156
170
|
uploadBytes: totalBytesSent,
|
157
171
|
downloadBytes: totalBytesReceived
|
158
172
|
};
|
159
|
-
log('performed %s to %p', this.protocol, connection.remotePeer);
|
173
|
+
this.log('performed %s to %p', this.protocol, connection.remotePeer);
|
160
174
|
await stream.close();
|
161
175
|
}
|
162
176
|
catch (err) {
|
163
|
-
log('error sending %
|
177
|
+
this.log('error sending %d/%d bytes to %p: %s', totalBytesSent, sendBytes, connection.remotePeer, err);
|
164
178
|
stream.abort(err);
|
165
179
|
throw err;
|
166
180
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"perf-service.js","sourceRoot":"","sources":["../../src/perf-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"perf-service.js","sourceRoot":"","sources":["../../src/perf-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,aAAa,EAAE,2BAA2B,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAOxI,MAAM,OAAO,IAAI;IACE,GAAG,CAAQ;IACZ,QAAQ,CAAQ;IACf,UAAU,CAAgB;IACnC,OAAO,CAAS;IACP,OAAO,CAAa;IACpB,cAAc,CAAQ;IACtB,iBAAiB,CAAQ;IACzB,kBAAkB,CAAQ;IAC1B,wBAAwB,CAAS;IAElD,YAAa,UAA0B,EAAE,OAAiB,EAAE;QAC1D,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,aAAa,CAAC,CAAA;QACxD,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,Y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AAC,KAAK,EAAE,CAAA;SACrB;QAAC,OAAO,GAAQ,EAAE;YACjB,IAAI,CAAC,GAAG,CAAC,qCAAqC,EAAE,cAAc,EAAE,SAAS,EAAE,UAAU,CAAC,UAAU,EAAE,GAAG,CAAC,CAAA;YACtG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACjB,MAAM,GAAG,CAAA;SACV;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@libp2p/perf",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.1-97ab31c0c",
|
4
4
|
"description": "Implementation of Perf Protocol",
|
5
5
|
"author": "@maschad / @marcopolo",
|
6
6
|
"license": "Apache-2.0 OR MIT",
|
@@ -48,14 +48,14 @@
|
|
48
48
|
"renderResults": "node dist/src/renderResults.js"
|
49
49
|
},
|
50
50
|
"dependencies": {
|
51
|
-
"@libp2p/interface": "
|
52
|
-
"@libp2p/interface-internal": "
|
53
|
-
"@libp2p/logger": "^3.0.5",
|
51
|
+
"@libp2p/interface": "0.1.6-97ab31c0c",
|
52
|
+
"@libp2p/interface-internal": "0.1.9-97ab31c0c",
|
54
53
|
"@multiformats/multiaddr": "^12.1.10",
|
55
54
|
"it-pushable": "^3.2.1"
|
56
55
|
},
|
57
56
|
"devDependencies": {
|
58
|
-
"@libp2p/
|
57
|
+
"@libp2p/logger": "3.1.0-97ab31c0c",
|
58
|
+
"@libp2p/interface-compliance-tests": "4.1.5-97ab31c0c",
|
59
59
|
"aegir": "^41.0.2",
|
60
60
|
"it-last": "^3.0.3",
|
61
61
|
"it-pair": "^2.0.6",
|
package/src/index.ts
CHANGED
@@ -11,14 +11,14 @@
|
|
11
11
|
* import { mplex } from '@libp2p/mplex'
|
12
12
|
* import { tcp } from '@libp2p/tcp'
|
13
13
|
* import { createLibp2p, type Libp2p } from 'libp2p'
|
14
|
-
* import { plaintext } from 'libp2p/
|
15
|
-
* import {
|
14
|
+
* import { plaintext } from '@libp2p/plaintext'
|
15
|
+
* import { perf, type Perf } from '@libp2p/perf'
|
16
16
|
*
|
17
17
|
* const ONE_MEG = 1024 * 1024
|
18
18
|
* const UPLOAD_BYTES = ONE_MEG * 1024
|
19
19
|
* const DOWNLOAD_BYTES = ONE_MEG * 1024
|
20
20
|
*
|
21
|
-
* async function createNode (): Promise<Libp2p<{ perf:
|
21
|
+
* async function createNode (): Promise<Libp2p<{ perf: Perf }>> {
|
22
22
|
* return createLibp2p({
|
23
23
|
* addresses: {
|
24
24
|
* listen: [
|
@@ -35,7 +35,7 @@
|
|
35
35
|
* yamux(), mplex()
|
36
36
|
* ],
|
37
37
|
* services: {
|
38
|
-
* perf:
|
38
|
+
* perf: perf()
|
39
39
|
* }
|
40
40
|
* })
|
41
41
|
* }
|
@@ -52,8 +52,8 @@
|
|
52
52
|
* ```
|
53
53
|
*/
|
54
54
|
|
55
|
-
import {
|
56
|
-
import type { AbortOptions } from '@libp2p/interface'
|
55
|
+
import { Perf as PerfClass } from './perf-service.js'
|
56
|
+
import type { AbortOptions, ComponentLogger } from '@libp2p/interface'
|
57
57
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
58
58
|
import type { Registrar } from '@libp2p/interface-internal/registrar'
|
59
59
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
@@ -69,18 +69,18 @@ export interface PerfOptions extends AbortOptions {
|
|
69
69
|
reuseExistingConnection?: boolean
|
70
70
|
}
|
71
71
|
|
72
|
-
export interface
|
72
|
+
export interface Perf {
|
73
73
|
measurePerformance(multiaddr: Multiaddr, sendBytes: number, recvBytes: number, options?: PerfOptions): AsyncGenerator<PerfOutput>
|
74
74
|
}
|
75
75
|
|
76
76
|
export interface PerfOutput {
|
77
|
-
type: 'intermediary' | 'final'
|
77
|
+
type: 'connection' | 'stream' | 'intermediary' | 'final'
|
78
78
|
timeSeconds: number
|
79
79
|
uploadBytes: number
|
80
80
|
downloadBytes: number
|
81
81
|
}
|
82
82
|
|
83
|
-
export interface
|
83
|
+
export interface PerfInit {
|
84
84
|
protocolName?: string
|
85
85
|
maxInboundStreams?: number
|
86
86
|
maxOutboundStreams?: number
|
@@ -92,11 +92,12 @@ export interface PerfServiceInit {
|
|
92
92
|
writeBlockSize?: number
|
93
93
|
}
|
94
94
|
|
95
|
-
export interface
|
95
|
+
export interface PerfComponents {
|
96
96
|
registrar: Registrar
|
97
97
|
connectionManager: ConnectionManager
|
98
|
+
logger: ComponentLogger
|
98
99
|
}
|
99
100
|
|
100
|
-
export function
|
101
|
-
return (components) => new
|
101
|
+
export function perf (init: PerfInit = {}): (components: PerfComponents) => Perf {
|
102
|
+
return (components) => new PerfClass(components, init)
|
102
103
|
}
|
package/src/perf-service.ts
CHANGED
@@ -1,16 +1,15 @@
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
2
1
|
import { pushable } from 'it-pushable'
|
3
2
|
import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, RUN_ON_TRANSIENT_CONNECTION, WRITE_BLOCK_SIZE } from './constants.js'
|
4
|
-
import type { PerfOptions, PerfOutput,
|
3
|
+
import type { PerfOptions, PerfOutput, PerfComponents, PerfInit, Perf as PerfInterface } from './index.js'
|
4
|
+
import type { Logger } from '@libp2p/interface'
|
5
5
|
import type { Startable } from '@libp2p/interface/startable'
|
6
6
|
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
7
7
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
export class PerfService implements Startable, PerfServiceInterface {
|
9
|
+
export class Perf implements Startable, PerfInterface {
|
10
|
+
private readonly log: Logger
|
12
11
|
public readonly protocol: string
|
13
|
-
private readonly components:
|
12
|
+
private readonly components: PerfComponents
|
14
13
|
private started: boolean
|
15
14
|
private readonly databuf: ArrayBuffer
|
16
15
|
private readonly writeBlockSize: number
|
@@ -18,8 +17,9 @@ export class PerfService implements Startable, PerfServiceInterface {
|
|
18
17
|
private readonly maxOutboundStreams: number
|
19
18
|
private readonly runOnTransientConnection: boolean
|
20
19
|
|
21
|
-
constructor (components:
|
20
|
+
constructor (components: PerfComponents, init: PerfInit = {}) {
|
22
21
|
this.components = components
|
22
|
+
this.log = components.logger.forComponent('libp2p:perf')
|
23
23
|
this.started = false
|
24
24
|
this.protocol = init.protocolName ?? PROTOCOL_NAME
|
25
25
|
this.writeBlockSize = init.writeBlockSize ?? WRITE_BLOCK_SIZE
|
@@ -32,7 +32,7 @@ export class PerfService implements Startable, PerfServiceInterface {
|
|
32
32
|
async start (): Promise<void> {
|
33
33
|
await this.components.registrar.handle(this.protocol, (data: IncomingStreamData) => {
|
34
34
|
void this.handleMessage(data).catch((err) => {
|
35
|
-
log.error('error handling perf protocol message', err)
|
35
|
+
this.log.error('error handling perf protocol message', err)
|
36
36
|
})
|
37
37
|
}, {
|
38
38
|
maxInboundStreams: this.maxInboundStreams,
|
@@ -90,21 +90,39 @@ export class PerfService implements Startable, PerfServiceInterface {
|
|
90
90
|
}
|
91
91
|
|
92
92
|
async * measurePerformance (ma: Multiaddr, sendBytes: number, receiveBytes: number, options: PerfOptions = {}): AsyncGenerator<PerfOutput> {
|
93
|
-
log('opening stream on protocol %s to %a', this.protocol, ma)
|
93
|
+
this.log('opening stream on protocol %s to %a', this.protocol, ma)
|
94
94
|
|
95
95
|
const uint8Buf = new Uint8Array(this.databuf)
|
96
96
|
const writeBlockSize = this.writeBlockSize
|
97
97
|
|
98
|
-
|
99
|
-
const initialStartTime = Date.now()
|
98
|
+
let lastReportedTime = Date.now()
|
100
99
|
const connection = await this.components.connectionManager.openConnection(ma, {
|
101
100
|
...options,
|
102
101
|
force: options.reuseExistingConnection !== true
|
103
102
|
})
|
103
|
+
|
104
|
+
yield {
|
105
|
+
type: 'connection',
|
106
|
+
timeSeconds: (Date.now() - lastReportedTime) / 1000,
|
107
|
+
uploadBytes: 0,
|
108
|
+
downloadBytes: 0
|
109
|
+
}
|
110
|
+
|
111
|
+
lastReportedTime = Date.now()
|
112
|
+
|
104
113
|
const stream = await connection.newStream(this.protocol, options)
|
105
114
|
|
115
|
+
let initialStartTime = Date.now()
|
116
|
+
|
117
|
+
yield {
|
118
|
+
type: 'stream',
|
119
|
+
timeSeconds: (Date.now() - lastReportedTime) / 1000,
|
120
|
+
uploadBytes: 0,
|
121
|
+
downloadBytes: 0
|
122
|
+
}
|
123
|
+
|
106
124
|
let lastAmountOfBytesSent = 0
|
107
|
-
|
125
|
+
lastReportedTime = Date.now()
|
108
126
|
let totalBytesSent = 0
|
109
127
|
|
110
128
|
// tell the remote how many bytes we will send. Up cast to 64 bit number
|
@@ -112,7 +130,7 @@ export class PerfService implements Startable, PerfServiceInterface {
|
|
112
130
|
const view = new DataView(this.databuf)
|
113
131
|
view.setBigUint64(0, BigInt(receiveBytes), false)
|
114
132
|
|
115
|
-
log('sending %i bytes to %p', sendBytes, connection.remotePeer)
|
133
|
+
this.log('sending %i bytes to %p', sendBytes, connection.remotePeer)
|
116
134
|
|
117
135
|
try {
|
118
136
|
const sendOutput = pushable<PerfOutput>({
|
@@ -123,6 +141,8 @@ export class PerfService implements Startable, PerfServiceInterface {
|
|
123
141
|
// Send the number of bytes to receive
|
124
142
|
yield uint8Buf.subarray(0, 8)
|
125
143
|
|
144
|
+
initialStartTime = Date.now()
|
145
|
+
|
126
146
|
while (sendBytes > 0) {
|
127
147
|
options.signal?.throwIfAborted()
|
128
148
|
|
@@ -196,10 +216,10 @@ export class PerfService implements Startable, PerfServiceInterface {
|
|
196
216
|
downloadBytes: totalBytesReceived
|
197
217
|
}
|
198
218
|
|
199
|
-
log('performed %s to %p', this.protocol, connection.remotePeer)
|
219
|
+
this.log('performed %s to %p', this.protocol, connection.remotePeer)
|
200
220
|
await stream.close()
|
201
221
|
} catch (err: any) {
|
202
|
-
log('error sending %
|
222
|
+
this.log('error sending %d/%d bytes to %p: %s', totalBytesSent, sendBytes, connection.remotePeer, err)
|
203
223
|
stream.abort(err)
|
204
224
|
throw err
|
205
225
|
}
|
package/dist/typedoc-urls.json
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"PerfOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfOptions.html",
|
3
|
-
".:PerfOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfOptions.html",
|
4
|
-
"PerfOutput": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfOutput.html",
|
5
|
-
".:PerfOutput": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfOutput.html",
|
6
|
-
"PerfService": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfService.html",
|
7
|
-
".:PerfService": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfService.html",
|
8
|
-
"PerfServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceComponents.html",
|
9
|
-
".:PerfServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceComponents.html",
|
10
|
-
"PerfServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceInit.html",
|
11
|
-
".:PerfServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceInit.html",
|
12
|
-
"perfService": "https://libp2p.github.io/js-libp2p/functions/_libp2p_perf.perfService-1.html",
|
13
|
-
".:perfService": "https://libp2p.github.io/js-libp2p/functions/_libp2p_perf.perfService-1.html"
|
14
|
-
}
|