RangerMauve / hypercore-fetch

Implementation of Fetch that uses the Hyper SDK for loading p2p content
MIT License
36 stars 12 forks source link

Add X-Drive-Size header to show the local download size of a drive #94

Closed josephmturner closed 1 month ago

josephmturner commented 10 months ago

This code is based on the drives info CLI command.

josephmturner commented 10 months ago

Is this a sufficient benchmark?

test.only('benchmark', async (t) => {
  const created = await nextURL(t)
  const uploadLocation = new URL('./example.txt', created)
  await fetch(uploadLocation, { method: 'put', body: SAMPLE_CONTENT })

  console.time('drive size test')
  for(var i=0; i < 1000; i++){
    await fetch(uploadLocation, { method: 'head' })
  }
  console.timeEnd('drive size test')
})

I added that to test.js and ran it on the x-drive-size feature branch:

> hypercore-fetch@9.7.0 test
> node test

TAP version 13
# benchmark
ok 1 Created new drive
hi
with drive size: 824.243ms

1..1
# tests 1
# pass  1

# ok

And on master branch:

> hypercore-fetch@9.7.0 test
> node test

TAP version 13
# benchmark
ok 1 Created new drive
hi
without drive size: 796.518ms

1..1
# tests 1
# pass  1

# ok

Results:

(824.243 - 796.518) / 796.518 = 0.0348

This benchmark indicates a 3.5% performance hit, but it might be more with many files on disk (instead of just one file in memory).

I can add request header flag like X-With-Drive-Size if you think that makes sense.

josephmturner commented 1 month ago

Merged in https://git.sr.ht/~ushin/hypercore-fetch-ushin/commit/cb30357d028f2ee9bdd2f0c385d660f5e2e47e04