Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: post process screenshots for deduping #290

Merged

Conversation

vigneshshanmugam
Copy link
Member

@vigneshshanmugam vigneshshanmugam commented May 26, 2021

  • fix De-duplicate images #285
  • Captures screenshot for each step and writes them to the cached .synthetics/<pid>/screenshots folder in JSON format with step and screenshot info. The pid stuff is to make sure each process (workers in future for leveraging parallelism) is writing screenshots to individual folders to avoid mutation.
  • Written screenshots are processed in to 8 blocks which results in 64 images blobs for 1280*720 viewport (Default) and wrote to heartbeat as screenshot/block and the whole screenshot reference is written as step/screenshot_reference which can be queried from the Uptime UI to construct all the images from the hash of individual block.
// screenshot block
{
  "type": "screenshot/block",
  "_id": "20f2cdc9e8302e94ff8d3dc09d3c359683cbcec5",
  "@timestamp": 1622076105043985.8,
  "blob": "UklGRlAAAABXRUJQVlA4IEQAAAAwBgCdASqgAFoAPm02mUmkIyKhIIgAgA2JaW7hdJAAT22IvEFRz2xF4gqOe2IvEFRz2xF4gqOe2ItgAP7/dlECgAAAAA==",
  "blob_mime": "image/webp",
  "package_version": "1.0.0-beta.2"
}
// screenshot ref

{
  "type": "step/screenshot_ref",
  "@timestamp": 1600300800000000,
  "journey": {
    "name": "j1",
    "id": "j1"
  },
  "step": {
    "name": "launch app",
    "index": 1
  },
  "root_fields": {
    "screenshot_ref": {
      "width": 1280,
      "height": 720,
      "blocks": [
        {
          "hash": "aee19c1f32131ed90e6dd34074a6e220ad97baf6",
          "top": 0,
          "left": 0,
          "width": 160,
          "height": 90
        },
        {
          "hash": "aee19c1f32131ed90e6dd34074a6e220ad97baf6",
          "top": 0,
          "left": 160,
          "width": 160,
          "height": 90
        }
      ]
    }
  },
  "package_version": "0.0.1"
}

@apmmachine
Copy link
Collaborator

apmmachine commented May 26, 2021

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Build Cause: Pull request #290 updated

  • Start Time: 2021-06-03T18:33:11.759+0000

  • Duration: 15 min 54 sec

  • Commit: 088e24d

Test stats 🧪

Test Results
Failed 0
Passed 102
Skipped 0
Total 102

Trends 🧪

Image of Build Times

Image of Tests

const left = col * blockWidth;
const buf = await img
.extract({ top, left, width: blockWidth, height: blockHeight })
.toFormat(sharp.format.webp)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self note:

  • check the size of individual blocks using different format.
  • Run benchmarks on this function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webp even though smaller than png by ~20ms average would yield more savings. Will leave it as webp for now.

Processing 50 images
webp - 14.36 seconds
png - 13.119 seconds

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks script to play with

!(async () => {
  console.time('screenshots');
  for (let i = 0; i < 50; i++) {
    await gatherScreenshots(
      'path/to/.synthetics/pid/screenshots'
    );
  }
  console.timeEnd('screenshots');
})();

@vigneshshanmugam vigneshshanmugam marked this pull request as ready for review May 27, 2021 00:32
*/
this.stream.flush();
}
this.runner.on('end', () => this.stream.flush());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to take care of closing the stream as the process exists automatically which closes the underlying stream once program runs to completion. This is better than figuring out when to close and prematurely closing it than leaving it which does not harm.

Copy link
Contributor

@andrewvc andrewvc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the improvements here left some comments on some ways it could work even better

src/reporters/json.ts Outdated Show resolved Hide resolved
src/reporters/json.ts Outdated Show resolved Hide resolved
src/reporters/json.ts Show resolved Hide resolved
src/reporters/json.ts Show resolved Hide resolved
src/reporters/json.ts Show resolved Hide resolved
Copy link
Contributor

@andrewvc andrewvc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is essentially LGTM but I'm leaving it in request changes state because we can't merge it as is without breaking things for existing users. If we were to merge and release this today screenshots will not work with current versions of kibana. We really need a system of feature flags or something similar to heartbeat to determine which features are enabled. Heartbeat at least follows the stack versioning, and as a result can inform the code here of how to act.

I think we will also need to support the old way of screenshots working based on whatever system of flags or capabilities we come up with. I've opened #292 to track

Copy link
Contributor

@andrewvc andrewvc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bug somewhere in this PR that affects handling of erroring test-cases. Running the following: cat t.js | node dist/cli.js --network --json --inline --screenshots | jq .type will fail early, not outputting journey/end and other events, where the contents of t.js are:

        step("load homepage", async () => {
            await page.goto('https://www.elastic.co');
        });
        step("hover over products menu", async () => {
            await page.hover('css=[data-nav-item=products]');
        });
        step("failme", async () => {
            await page.hhover('css=[data-nav-item=products]');
        });

@vigneshshanmugam
Copy link
Member Author

@andrewvc Could be this #290 (comment), Will check tomorrow.

@andrewvc
Copy link
Contributor

andrewvc commented Jun 2, 2021

Not sure as to the root cause

@vigneshshanmugam
Copy link
Member Author

Fixed the issue - We do async processing of the events in the microtasks which are being cut of by the CLI node process. Now we wait for the ack from the reporter once its written to the file descriptor before closing the process - 36a42e5

const left = col * blockWidth;
const buf = await img
.extract({ top, left, width: blockWidth, height: blockHeight })
.jpeg()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to jpeg for now, we can play with webp once we decide a solution in the Kibana.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(callback).not.toHaveBeenCalled();
});

it('idempotent on constructing screenshots blocks', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this!

Copy link
Contributor

@andrewvc andrewvc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vigneshshanmugam vigneshshanmugam merged commit 3ccd2c1 into elastic:master Jun 3, 2021
@vigneshshanmugam vigneshshanmugam deleted the post-process-screenshots branch June 3, 2021 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

De-duplicate images
3 participants