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: add code size analyzer #109

Merged
merged 2 commits into from
Dec 11, 2024
Merged

feat: add code size analyzer #109

merged 2 commits into from
Dec 11, 2024

Conversation

z0ffy
Copy link
Owner

@z0ffy z0ffy commented Dec 10, 2024

Summary by Sourcery

Add a code size analyzer to measure and log the size of code bundles before and after obfuscation, including gzip sizes. Enhance the logging of the obfuscation process with detailed size information.

New Features:

  • Introduce a CodeSizeAnalyzer class to measure and log the size of code bundles before and after obfuscation, including gzip sizes.

Enhancements:

  • Add a formatSize function to convert byte sizes into human-readable formats with appropriate units.

Copy link

sourcery-ai bot commented Dec 10, 2024

Reviewer's Guide by Sourcery

This PR implements a code size analyzer feature that tracks and reports bundle size metrics before and after obfuscation. The implementation includes a new CodeSizeAnalyzer class that calculates both raw and gzipped sizes of bundles, and presents the results with percentage changes.

Class diagram for CodeSizeAnalyzer and related types

classDiagram
    class CodeSizeAnalyzer {
        - Log _log
        - SizeResult originalSize
        - SizeResult obfuscatedSize
        - number startTime
        - number endTime
        + CodeSizeAnalyzer(Log log)
        + void start(BundleList originalBundleList)
        + void end(BundleList obfuscatedBundleList)
        - SizeResult calculateBundleSize(BundleList bundleList)
        - string analyze()
        - void logResult()
        - SizeResult createEmptySizeResult()
    }
    class SizeResult {
        FormatSizeResult original
        FormatSizeResult gzip
    }
    class FormatSizeResult {
        number value
        SizeUnit unit
    }
    class SizeUnit {
        <<enumeration>>
        B
        KB
        MB
    }
    CodeSizeAnalyzer --> Log
    CodeSizeAnalyzer --> SizeResult
    SizeResult --> FormatSizeResult
    FormatSizeResult --> SizeUnit
Loading

File-Level Changes

Change Details Files
Added a new CodeSizeAnalyzer class to track bundle sizes
  • Implemented size calculation for both original and gzipped bundles
  • Added timing measurement for the obfuscation process
  • Created formatted output showing size changes and percentage increases
  • Integrated gzip compression using node:zlib
src/utils/index.ts
Added new types and enums for size analysis
  • Created SizeUnit enum for byte units (B, KB, MB)
  • Added FormatSizeResult interface for size formatting
  • Added SizeResult type for storing original and gzip sizes
src/type.ts
src/utils/constants.ts
Integrated size analyzer into the main plugin flow
  • Modified transformIndexHtmlHandler to use the new CodeSizeAnalyzer
  • Updated logging to include color-coded success messages
  • Replaced manual timing with analyzer's built-in timing
src/index.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @z0ffy - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +209 to +212
const percentageIncrease = (
((obfuscatedSize.original.value - originalSize.original.value) / originalSize.original.value)
* 100
).toFixed(2);
Copy link

Choose a reason for hiding this comment

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

issue: Handle edge cases where original sizes are 0 to prevent NaN or Infinity results

Add checks before division to handle cases where originalSize values are 0. Consider returning '0%' or 'N/A' in such cases.

src/utils/index.ts Outdated Show resolved Hide resolved
@z0ffy z0ffy merged commit 6e16b68 into main Dec 11, 2024
3 checks passed
@z0ffy z0ffy deleted the feature/compare-output-size branch December 11, 2024 14:05
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.

1 participant