Skip to content

Commit

Permalink
measureNftTimeToRetrievability.js logs duration
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed May 26, 2022
1 parent 82f97a5 commit 0b392a9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/cron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"p-retry": "^4.6.1",
"p-settle": "^5.0.0",
"pg": "^8.7.1",
"safe-env-vars": "^1.0.4",
"streaming-iterables": "^6.0.0",
"yargs": "^17.5.1"
},
Expand Down
67 changes: 50 additions & 17 deletions packages/cron/src/jobs/measureNftTimeToRetrievability.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@ import { mkdtemp, writeFile } from 'node:fs/promises'
import * as assert from 'assert'
import mime from 'mime-types'
import fetch from '@web-std/fetch'
import { NFTStorage } from 'nft.storage'
import { EnvironmentLoader } from 'safe-env-vars'
import { Milliseconds, now } from '../lib/time.js'

const FILENAME_TO_UPLOAD = 'to-upload'

/**
* Download an image to the directory and return path to the file
* @param {string} targetDir - path to fs dir to download to
*/
const downloadRandomImageToUpload = async (targetDir) => {
const { target: file } = await downloadFile(
new URL(
'https://bafybeiarmhq3d7msony7zfq67gmn46syuv6jrc6dagob2wflunxiyaksj4.ipfs.dweb.link/1681.png'
),
({ ext }) => path.join(targetDir, `${FILENAME_TO_UPLOAD}.${ext}`)
)
return file
}
const env = new EnvironmentLoader()
const NFT_STORAGE_API_KEY = env.string.get('NFT_STORAGE_API_KEY')
const EXAMPLE_NFT_IMG_URL =
'https://bafybeiarmhq3d7msony7zfq67gmn46syuv6jrc6dagob2wflunxiyaksj4.ipfs.dweb.link/1681.png'

/**
* Job that tests/measures steps
Expand All @@ -28,12 +20,53 @@ const downloadRandomImageToUpload = async (targetDir) => {
* * retrieve image through ipfs gateway
* @param {object} config
* @param {string} [config.url] - URL to nft.storage to measure
* @param {string} [config.nftStorageToken] - API Token for nft.storage
*/
export async function measureNftTimeToRetrievability(config = {}) {
console.log('start measureNftTimeToRetrievability', config)
const testStartDate = new Date()
const testStart = now()
const dir = await createRandomDir()
const file = await downloadRandomImageToUpload(dir)
console.log({ dir, upload: { source: file } })
const imageResponse = await fetch(EXAMPLE_NFT_IMG_URL)
const image = await imageResponse.blob()
const nft = {
image, // use image Blob as `image` field
name: 'Example NFT',
description: 'Example NFT used by nft.storage cron nft-ttr',
}
const client = new NFTStorage({
token: config.nftStorageToken ?? NFT_STORAGE_API_KEY,
})
const storeStartedDate = new Date()
const storeStartedAt = now()
const metadata = await client.store(nft)
const storeEndAt = now()
const store = {
type: 'store',
startTime: storeStartedDate,
duration: new Milliseconds(
storeEndAt.toNumber() - storeStartedAt.toNumber()
),
}
const retrievalUrl = `https://${metadata.ipnft}.ipfs.nftstorage.link/image/blob`
const retrieveFetchDate = new Date()
const retrieveFetchStart = now()
const retrieveImageResponse = await fetch(retrievalUrl)
const retrieveFetchEnd = now()
const retrieveReadStart = now()
const retrievedImage = await retrieveImageResponse.blob()
const retrieveReadEnd = now()
const retrievalDuration = new Milliseconds(
retrieveReadEnd.toNumber() - retrieveFetchStart.toNumber()
)
const retrieve = {
url: retrievalUrl,
contentLength: retrievedImage.size,
blob: retrievedImage,
startTime: retrieveFetchDate,
duration: retrievalDuration,
}
console.log(JSON.stringify({ store, retrieve }, null, 2))
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { measureNftTimeToRetrievability } from './measureNftTimeToRetrievability'
import { measureNftTimeToRetrievability } from './measureNftTimeToRetrievability.js'
import { jest } from '@jest/globals'

describe('measureNftTimeToRetrievability', () => {
jest.setTimeout(15 * 1000)
it('has a test', async () => {
await measureNftTimeToRetrievability()
console.log('after measureNftTimeToRetrievability()')
})
})
37 changes: 37 additions & 0 deletions packages/cron/src/lib/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { performance } from 'perf_hooks'

/**
* @property {number} #size
*/
export class Milliseconds {
/** @type {number} */
#size = 0
unit = {
symbol: 'ms',
name: 'millisecond',
}
/**
* @param {number} numberMs
*/
constructor(numberMs) {
this.unit = {
symbol: 'ms',
name: 'milliseconds',
}
this.#size = numberMs
}
get size() {
return this.#size
}
toNumber() {
return this.size
}
toJSON() {
return {
unit: this.unit,
size: this.size,
}
}
}

export const now = () => new Milliseconds(performance.now())
3 changes: 0 additions & 3 deletions packages/cron/src/lib/utils.spec.js

This file was deleted.

12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8626,6 +8626,11 @@ dotenv-expand@^5.1.0:
resolved "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-5.1.0.tgz"
integrity sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==

dotenv@16.0.0:
version "16.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.0.tgz#c619001253be89ebb638d027b609c75c26e47411"
integrity sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==

dotenv@^10.0.0:
version "10.0.0"
resolved "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz"
Expand Down Expand Up @@ -18159,6 +18164,13 @@ safe-buffer@5.2.1, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, s
resolved "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==

safe-env-vars@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/safe-env-vars/-/safe-env-vars-1.0.4.tgz#cb174ee0ceba134ce6474424bc9fd1bd5608ee25"
integrity sha512-GnxSb52oMynfhur1IdgCBkHEnkG8Mz+GO1vXbbVymFdU0GKzuoD4nGHP//zztnHEQ7Ai4/bU5rcs+TlGYjjEAQ==
dependencies:
dotenv "16.0.0"

safe-regex@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz"
Expand Down

0 comments on commit 0b392a9

Please sign in to comment.