@libp2p/perf 2.0.1-0f5c305af → 2.0.1-9c67c5b3d

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 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/insecure'
21
- import { perfService, type PerfService } from '@libp2p/perf'
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: PerfService }>> {
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: perfService()
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 B=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var he=Object.getPrototypeOf,pe=Object.prototype.hasOwnProperty;var $=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports),be=(r,e)=>{for(var t in e)B(r,t,{get:e[t],enumerable:!0})},K=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of de(e))!pe.call(r,o)&&o!==t&&B(r,o,{get:()=>e[o],enumerable:!(n=fe(e,o))||n.enumerable});return r};var me=(r,e,t)=>(t=r!=null?le(he(r)):{},K(e||!r||!r.__esModule?B(t,"default",{value:r,enumerable:!0}):t,r)),we=r=>K(B({},"__esModule",{value:!0}),r);var W=$((qe,G)=>{var k=1e3,I=k*60,M=I*60,N=M*24,ge=N*7,Ce=N*365.25;G.exports=function(r,e){e=e||{};var t=typeof r;if(t==="string"&&r.length>0)return ye(r);if(t==="number"&&isFinite(r))return e.long?ve(r):xe(r);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(r))};function ye(r){if(r=String(r),!(r.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(r);if(e){var t=parseFloat(e[1]),n=(e[2]||"ms").toLowerCase();switch(n){case"years":case"year":case"yrs":case"yr":case"y":return t*Ce;case"weeks":case"week":case"w":return t*ge;case"days":case"day":case"d":return t*N;case"hours":case"hour":case"hrs":case"hr":case"h":return t*M;case"minutes":case"minute":case"mins":case"min":case"m":return t*I;case"seconds":case"second":case"secs":case"sec":case"s":return t*k;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return t;default:return}}}}function xe(r){var e=Math.abs(r);return e>=N?Math.round(r/N)+"d":e>=M?Math.round(r/M)+"h":e>=I?Math.round(r/I)+"m":e>=k?Math.round(r/k)+"s":r+"ms"}function ve(r){var e=Math.abs(r);return e>=N?L(r,e,N,"day"):e>=M?L(r,e,M,"hour"):e>=I?L(r,e,I,"minute"):e>=k?L(r,e,k,"second"):r+" ms"}function L(r,e,t,n){var o=e>=t*1.5;return Math.round(r/t)+" "+n+(o?"s":"")}});var Y=$((Je,Q)=>{function Fe(r){t.debug=t,t.default=t,t.coerce=b,t.disable=d,t.enable=o,t.enabled=h,t.humanize=W(),t.destroy=y,Object.keys(r).forEach(s=>{t[s]=r[s]}),t.names=[],t.skips=[],t.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 t.colors[Math.abs(a)%t.colors.length]}t.selectColor=e;function t(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]=t.coerce(i[0]),typeof i[0]!="string"&&i.unshift("%O");let g=0;i[0]=i[0].replace(/%([a-zA-Z%])/g,(A,v)=>{if(A==="%%")return"%";g++;let E=t.formatters[v];if(typeof E=="function"){let U=i[g];A=E.call(c,U),i.splice(g,1),g--}return A}),t.formatArgs.call(c,i),(c.log||t.log).apply(c,i)}return l.namespace=s,l.useColors=t.useColors(),l.color=t.selectColor(s),l.extend=n,l.destroy=t.destroy,Object.defineProperty(l,"enabled",{enumerable:!0,configurable:!1,get:()=>f!==null?f:(O!==t.namespaces&&(O=t.namespaces,u=t.enabled(s)),u),set:i=>{f=i}}),typeof t.init=="function"&&t.init(l),l}function n(s,a){let f=t(this.namespace+(typeof a>"u"?":":a)+s);return f.log=this.log,f}function o(s){t.save(s),t.namespaces=s,t.names=[],t.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]==="-"?t.skips.push(new RegExp("^"+s.slice(1)+"$")):t.names.push(new RegExp("^"+s+"$")))}function d(){let s=[...t.names.map(p),...t.skips.map(p).map(a=>"-"+a)].join(",");return t.enable(""),s}function h(s){if(s[s.length-1]==="*")return!0;let a,f;for(a=0,f=t.skips.length;a<f;a++)if(t.skips[a].test(s))return!1;for(a=0,f=t.names.length;a<f;a++)if(t.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 t.enable(t.load()),t}Q.exports=Fe});var H=$((F,R)=>{F.formatArgs=Ae;F.save=Ee;F.load=Oe;F.useColors=Se;F.storage=Ne();F.destroy=(()=>{let r=!1;return()=>{r||(r=!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(r){if(r[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+r[0]+(this.useColors?"%c ":" ")+"+"+R.exports.humanize(this.diff),!this.useColors)return;let e="color: "+this.color;r.splice(1,0,e,"color: inherit");let t=0,n=0;r[0].replace(/%[a-zA-Z%]/g,o=>{o!=="%%"&&(t++,o==="%c"&&(n=t))}),r.splice(n,0,e)}F.log=console.debug||console.log||(()=>{});function Ee(r){try{r?F.storage.setItem("debug",r):F.storage.removeItem("debug")}catch{}}function Oe(){let r;try{r=F.storage.getItem("debug")}catch{}return!r&&typeof process<"u"&&"env"in process&&(r=process.env.DEBUG),r}function Ne(){try{return localStorage}catch{}}R.exports=Y()(F);var{formatters:Ue}=R.exports;Ue.j=function(r){try{return JSON.stringify(r)}catch(e){return"[UnexpectedJSONParseError]: "+e.message}}});var je={};be(je,{perfService:()=>$e});var S=me(H(),1);function ke(r,e){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),n=0;n<t.length;n++)t[n]=255;for(var o=0;o<r.length;o++){var d=r.charAt(o),h=d.charCodeAt(0);if(t[h]!==255)throw new TypeError(d+" is ambiguous");t[h]=o}var p=r.length,b=r.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,g=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*g[v]>>>0,g[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&&g[E]===0;)E++;for(var U=b.repeat(l);E<w;++E)U+=r.charAt(g[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 g=t[u.charCodeAt(l)];if(g===255)return;for(var x=0,A=m-1;(g!==0||x<c)&&A!==-1;A--,x++)g+=p*w[A]>>>0,w[A]=g%256>>>0,g=g/256>>>0;if(g!==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 Ie=ke,Me=Ie,ee=Me;var Ze=new Uint8Array(0);var te=r=>{if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")};var j=class{constructor(e,t,n){this.name=e,this.prefix=t,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,t,n){if(this.name=e,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.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 t=e[0],n=this.decoders[t];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=(r,e)=>new q({...r.decoders||{[r.prefix]:r},...e.decoders||{[e.prefix]:e}}),J=class{constructor(e,t,n,o){this.name=e,this.prefix=t,this.baseEncode=n,this.baseDecode=o,this.encoder=new j(e,t,n),this.decoder=new V(e,t,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}},ne=({name:r,prefix:e,encode:t,decode:n})=>new J(r,e,t,n),X=({prefix:r,name:e,alphabet:t})=>{let{encode:n,decode:o}=ee(t,e);return ne({prefix:r,name:e,encode:n,decode:d=>te(o(d))})},Te=(r,e,t,n)=>{let o={};for(let s=0;s<e.length;++s)o[e[s]]=s;let d=r.length;for(;r[d-1]==="=";)--d;let h=new Uint8Array(d*t/8|0),p=0,b=0,y=0;for(let s=0;s<d;++s){let a=o[r[s]];if(a===void 0)throw new SyntaxError(`Non-${n} character`);b=b<<t|a,p+=t,p>=8&&(p-=8,h[y++]=255&b>>p)}if(p>=t||255&b<<8-p)throw new SyntaxError("Unexpected end of data");return h},ze=(r,e,t)=>{let n=e[e.length-1]==="=",o=(1<<t)-1,d="",h=0,p=0;for(let b=0;b<r.length;++b)for(p=p<<8|r[b],h+=8;h>t;)h-=t,d+=e[o&p>>h];if(h&&(d+=e[o&p<<t-h]),n)for(;d.length*t&7;)d+="=";return d},C=({name:r,prefix:e,bitsPerChar:t,alphabet:n})=>ne({prefix:e,name:r,encode(o){return ze(o,n,t)},decode(o){return Te(o,n,t,r)}});var oe=C({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),He=C({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),et=C({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),tt=C({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),rt=C({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),nt=C({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),ot=C({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),st=C({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),it=C({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=C({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),dt=C({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),ht=C({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),pt=C({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});S.default.formatters.b=r=>r==null?"undefined":se.baseEncode(r);S.default.formatters.t=r=>r==null?"undefined":oe.baseEncode(r);S.default.formatters.m=r=>r==null?"undefined":ie.baseEncode(r);S.default.formatters.p=r=>r==null?"undefined":r.toString();S.default.formatters.c=r=>r==null?"undefined":r.toString();S.default.formatters.k=r=>r==null?"undefined":r.toString();S.default.formatters.a=r=>r==null?"undefined":r.toString();function Be(r){let e=()=>{};return e.enabled=!1,e.color="",e.diff=0,e.log=()=>{},e.namespace=r,e.destroy=()=>!0,e.extend=()=>e,e}function ae(r){let e=Be(`${r}:trace`);return S.default.enabled(`${r}:trace`)&&S.default.names.map(t=>t.toString()).find(t=>t.includes(":trace"))!=null&&(e=(0,S.default)(`${r}:trace`)),Object.assign((0,S.default)(r),{error:(0,S.default)(`${r}:error`),trace:e})}function _(){let r={};return r.promise=new Promise((e,t)=>{r.resolve=e,r.reject=t}),r}var D=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}},T=class{size;hwm;head;tail;constructor(e={}){this.hwm=e.splitLimit??16,this.head=new D(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 t=this.head;this.head=t.next=new D(2*this.head.buffer.length),this.head.push(e)}}shift(){let e=this.tail.shift();if(e===void 0&&this.tail.next!=null){let t=this.tail.next;this.tail.next=null,this.tail=t,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,t){super(e??"The operation was aborted"),this.type="aborted",this.code=t??"ABORT_ERR"}};function ue(r={}){return Le(t=>{let n=t.shift();if(n==null)return{done:!0};if(n.error!=null)throw n.error;return{done:n.done===!0,value:n.value}},r)}function Le(r,e){e=e??{};let t=e.onEnd,n=new T,o,d,h,p=_(),b=async()=>{try{return n.isEmpty()?h?{done:!0}:await new Promise((i,c)=>{d=m=>{d=null,n.push(m);try{i(r(n))}catch(w){c(w)}return o}}):r(n)}finally{n.isEmpty()&&queueMicrotask(()=>{p.resolve(),p=_()})}},y=i=>d!=null?d(i):(n.push(i),o),s=i=>(n=new T,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 T,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((g,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)}}},t==null)return o;let l=o;return o={[Symbol.asyncIterator](){return this},next(){return l.next()},throw(i){return l.throw(i),t!=null&&(t(i),t=void 0),{done:!0}},return(){return l.return(),t!=null&&(t(),t=void 0),{done:!0}},push:a,end(i){return l.end(i),t!=null&&(t(i),t=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,t={}){this.components=e,this.started=!1,this.protocol=t.protocolName??ce,this.writeBlockSize=t.writeBlockSize??65536,this.databuf=new ArrayBuffer(this.writeBlockSize),this.maxInboundStreams=t.maxInboundStreams??1,this.maxOutboundStreams=t.maxOutboundStreams??1,this.runOnTransientConnection=t.runOnTransientConnection??!1}async start(){await this.components.registrar.handle(this.protocol,e=>{this.handleMessage(e).catch(t=>{z.error("error handling perf protocol message",t)})},{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:t}=e;try{let n=this.writeBlockSize,o;for await(let h of t.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 t.sink(async function*(){for(;o>0;){let h=n;h>o&&(h=o),o=o-h,yield d.subarray(0,h)}}())}catch(n){t.abort(n)}}async*measurePerformance(e,t,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",t,b.remotePeer);try{let u=ue({objectMode:!0});y.sink(async function*(){for(yield d.subarray(0,8);t>0;){o.signal?.throwIfAborted();let c=h;c>t&&(c=t),t=t-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 %d/%d bytes to %p: %s",f,t,b.remotePeer,u),y.abort(u),u}}};function $e(r={}){return e=>new P(e,r)}return we(je);})();
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,o)=>{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:!(o=D(t,n))||o.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 o=e.shift();if(o==null)return{done:!0};if(o.error!=null)throw o.error;return{done:o.done===!0,value:o.value}},s)}function z(s,t){t=t??{};let e=t.onEnd,o=new b,n,l,u,a=x(),p=async()=>{try{return o.isEmpty()?u?{done:!0}:await new Promise((r,i)=>{l=S=>{l=null,o.push(S);try{r(s(o))}catch(w){i(w)}return n}}):s(o)}finally{o.isEmpty()&&queueMicrotask(()=>{a.resolve(),a=x()})}},c=r=>l!=null?l(r):(o.push(r),n),y=r=>(o=new b,l!=null?l({error:r}):(o.push({error:r}),n)),m=r=>{if(u)return n;if(t?.objectMode!==!0&&r?.byteLength==null)throw new Error("objectMode was not true but tried to push non-Uint8Array value");return c({done:!1,value:r})},f=r=>u?n:(u=!0,r!=null?y(r):c({done:!0})),B=()=>(o=new b,f(),{done:!0}),h=r=>(f(r),{done:!0});if(n={[Symbol.asyncIterator](){return this},next:p,return:B,throw:h,push:m,end:f,get readableLength(){return o.size},onEmpty:async r=>{let i=r?.signal;if(i?.throwIfAborted(),o.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(r){return d.throw(r),e!=null&&(e(r),e=void 0),{done:!0}},return(){return d.return(),e!=null&&(e(),e=void 0),{done:!0}},push:m,end(r){return d.end(r),e!=null&&(e(r),e=void 0),n},get readableLength(){return d.readableLength},onEmpty:r=>d.onEmpty(r)},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 o=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=o;u>n&&(u=n),n=n-u,yield l.subarray(0,u)}}())}catch(o){e.abort(o)}}async*measurePerformance(t,e,o,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(o),!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 r=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,r+=i.byteLength;if(r!==o)throw new Error(`Expected to receive ${o} bytes, but received ${r}`);yield{type:"final",timeSeconds:(Date.now()-y)/1e3,uploadBytes:f,downloadBytes:r},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}));
@@ -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/insecure'
15
- * import { perfService, type PerfService } from '@libp2p/perf'
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: PerfService }>> {
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: perfService()
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 PerfService {
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 PerfServiceInit {
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 PerfServiceComponents {
87
+ export interface PerfComponents {
88
88
  registrar: Registrar;
89
89
  connectionManager: ConnectionManager;
90
+ logger: ComponentLogger;
90
91
  }
91
- export declare function perfService(init?: PerfServiceInit): (components: PerfServiceComponents) => PerfService;
92
+ export declare function perf(init?: PerfInit): (components: PerfComponents) => Perf;
92
93
  //# sourceMappingURL=index.d.ts.map
@@ -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;AACrD,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,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yB,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/insecure'
15
- * import { perfService, type PerfService } from '@libp2p/perf'
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: PerfService }>> {
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: perfService()
38
+ * perf: perf()
39
39
  * }
40
40
  * })
41
41
  * }
@@ -51,8 +51,8 @@
51
51
  * await libp2p2.stop()
52
52
  * ```
53
53
  */
54
- import { PerfService as PerfServiceClass } from './perf-service.js';
55
- export function perfService(init = {}) {
56
- return (components) => new PerfServiceClass(components, init);
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
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AAEH,OAAO,EAAE,WAAW,IAAI,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AA6CnE,MAAM,UAAU,WAAW,CAAE,OAAwB,EAAE;IACrD,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,gBAAgB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AAC/D,CAAC"}
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, PerfServiceComponents, PerfServiceInit, PerfService as PerfServiceInterface } from './index.js';
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 PerfService implements Startable, PerfServiceInterface {
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: PerfServiceComponents, init?: PerfServiceInit);
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":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,qBAAqB,EAAE,eAAe,EAAE,WAAW,IAAI,oBAAoB,EAAE,MAAM,YAAY,CAAA;AACtI,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;AAIxD,qBAAa,WAAY,YAAW,SAAS,EAAE,oBAAoB;IACjE,SAAgB,QAAQ,EAAE,MAAM,CAAA;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAuB;IAClD,OAAO,C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,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"}
@@ -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
- const log = logger('libp2p:perf');
5
- export class PerfService {
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
- // start time should include connection establishment
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
- let lastReportedTime = Date.now();
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 %d/%d bytes to %p: %s', totalBytesSent, sendBytes, connection.remotePeer, err);
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,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,aAAa,EAAE,2BAA2B,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAMxI,MAAM,GAAG,GAAG,MAAM,CAAC,aAAa,CAAC,CAAA;AAEjC,MAAM,OAAO,WAAW;IACN,QAAQ,CAAQ;IACf,UAAU,CAAuB;IAC1C,OAAO,CAAS;IACP,OAAO,CAAa;IACpB,cAAc,CAAQ;IACtB,iBAAiB,CAAQ;IACzB,kBAAkB,CAAQ;IAC1B,wBAAwB,CAAS;IAElD,YAAa,UAAiC,EAAE,OAAwB,EAAE;QACxE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,YAAY,IAAI,aAAa,CAAA;QAClD,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,IAAI,gBAAgB,CAAA;QAC7D,IAAI,CAAC,OAAO,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,c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,CAAC,GAAG,EAAE,GAAG,gBAAgB,CAAC,GAAG,IAAI;gBACnD,WAAW,EAAE,cAAc;gBAC3B,aAAa,EAAE,kBAAkB;aAClC,CAAA;YAED,IAAI,CAAC,GAAG,CAAC,oBAAoB,EAAE,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;YACpE,MAAM,MAAM,CAAC,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.1-0f5c305af",
3
+ "version": "2.0.1-9c67c5b3d",
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": "0.1.6-0f5c305af",
52
- "@libp2p/interface-internal": "0.1.9-0f5c305af",
53
- "@libp2p/logger": "3.1.0-0f5c305af",
51
+ "@libp2p/interface": "0.1.6-9c67c5b3d",
52
+ "@libp2p/interface-internal": "0.1.9-9c67c5b3d",
54
53
  "@multiformats/multiaddr": "^12.1.10",
55
54
  "it-pushable": "^3.2.1"
56
55
  },
57
56
  "devDependencies": {
58
- "@libp2p/interface-compliance-tests": "4.1.5-0f5c305af",
57
+ "@libp2p/logger": "3.1.0-9c67c5b3d",
58
+ "@libp2p/interface-compliance-tests": "4.1.5-9c67c5b3d",
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/insecure'
15
- * import { perfService, type PerfService } from '@libp2p/perf'
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: PerfService }>> {
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: perfService()
38
+ * perf: perf()
39
39
  * }
40
40
  * })
41
41
  * }
@@ -52,8 +52,8 @@
52
52
  * ```
53
53
  */
54
54
 
55
- import { PerfService as PerfServiceClass } from './perf-service.js'
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 PerfService {
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 PerfServiceInit {
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 PerfServiceComponents {
95
+ export interface PerfComponents {
96
96
  registrar: Registrar
97
97
  connectionManager: ConnectionManager
98
+ logger: ComponentLogger
98
99
  }
99
100
 
100
- export function perfService (init: PerfServiceInit = {}): (components: PerfServiceComponents) => PerfService {
101
- return (components) => new PerfServiceClass(components, init)
101
+ export function perf (init: PerfInit = {}): (components: PerfComponents) => Perf {
102
+ return (components) => new PerfClass(components, init)
102
103
  }
@@ -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, PerfServiceComponents, PerfServiceInit, PerfService as PerfServiceInterface } from './index.js'
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
- const log = logger('libp2p:perf')
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: PerfServiceComponents
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: PerfServiceComponents, init: PerfServiceInit = {}) {
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
- // start time should include connection establishment
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
- let lastReportedTime = Date.now()
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 %d/%d bytes to %p: %s', totalBytesSent, sendBytes, connection.remotePeer, err)
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
  }