HANZHANG LIN
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Essay 4 *Beets* is a media management software mainly running on PCs. The scalability is one of the important aspect when analyzing and evaluating *Beets*. In general, there are several ways measuring the scalability of the software. In this essay, we are going to measure the time and space consumption of *Beets*, to see whether it scales well when the task size increases. ## Scalability Challenges The two key usages of *Beets* are importing collections and querying for musics and albums. Here we mainly focus on the scalability of collections import, since query mainly relies on MySQL interface where MySQL's scalability is the most important factor in this situation. When importing musics and collections, there are two scenarios that may challenge the scalability of *Beets*. The first scenario is that there are many music files under a directory. In this way, *Beets* has to import and tag each file accordingly. Another situation is that the music files are stored in a very deep folder structure as we show below, which requires *beets* to traverse all subdirectories to import musics. ``` Musics/ -- Singer 1/ ---- Album 1/ -------- music 1 -------- music 2 -------- music 3 -------- ... ---- Album 2/ -------- music 4 -------- music 5 -------- music 6 -------- ... -- Singer 2/ ---- Album 3/ -------- music 7 -------- music 8 -------- music 9 -------- ... ---- Album 4/ -------- music 10 -------- music 11 -------- music 12 -------- ... ---- Album ../ -- Singer ../ ``` Both of these two situations could limit the scalability of *Beets* and we will look into one of them to analyze the scalability of *Beets* quantitatively. ## Quantitative Analysis of *Beets*' Performance We will analyze the first situation mentioned in the previous section, that is when many files are in the same directory, to get a better insight of the scalability of *Beets*. We vary the number of musics in a single directory to see the time and memory performance of importing musics in *Beets*. To get a more general results, we enable the *autotagger* in the experiments, which can be turned off if the user specify an argument. The results are shown in the figure below. We can tell the import time is positively proportional to the number of files in the folder. With the number of musics increased, the import time also increased linearly. This poses a threat to the scalability of *Beets*, as it will take a very long time to import hundreds of music files. On the other hand, *Beets* assumes musics under the same folder are from the same album. Since an album may only contains twenty of thirty musics, this threat will be mitigated. {{<image file="response_time.png" caption="The response time when the number of musics varies">}} We also measure the memory usage of importing music files in *Beets*. The results are shown below. The memory usage are unstable, though the general trend is that the memory usage remains constant when the number of files changes. We believe that the major factor of memory usage is the *autotagger*. Since *autotagger* can only tag one music at one time, it is reasonable that the memory usage is constant. {{<image file="space_performance.png" caption="The memory usage when the number of musics varies">}} ## Architectural Decisions that Affect Scalability In the previous sections, we have analyzed the senarios that challenge the scalcability of *Beets* and conducted experiments to show the results clearly. And in this section, we will show what kind of architectural decisions leading to this. Although the process of importing is time-consuming, there are one architecural decision leading to better scalibility. Autotag and some plugins could be enabled or disabled by revising the configuration file. Users could choose to disable *autotag* to get a better performance for *import*. However, another architectural decision that limit the scalibility, the *autotag* module. The decision to build the architecure in pure Python is one effect that restrict scalibility. In this process, two strings has to be compared by calculating the basic distance between them in order to get the accurate tag for the given item. This process is based on a turn ASCII characters[^1] into lower case and normalized by string length. As we all know, all the types in Python is a kind of object compared to the fixed data types in C or C++, thus it would definitely waste a lot of time to identify the type of variants and then calculate their values. ## Proposals for Architectural Changes As we mentioned before, the programming language of the whole system of *Beets* is Python. Admittedly, Pyhton is good for its features, such as functional and abundant libraries, easy and clear to use, and so forth. However, it is not efficient enough. Python is an interpreted language rather than a compiled language. Internally Python code is interpreted during run time rather than being compiled to native code hence it is difficult to optimized and leads to lower efficiency. Therefore, it might be effective to choose another programming language to implement some modules in order to address the issues that we identified in the previous part. Static programming language such as C, C++ or Java might be a good choice for some performance-demanding scenarios as we mentioned before. There are three main steps[^2] to carry out. First, we have to re-write the targeted functions or modules in these languages. Second, we compile these modules to static or dynamic libraries. Third, we link these modules to the Python runtime. In this way, we could reduce the execution time for some time-consuming modules or functions like *autotag* and *plugin*. ## Architecture Designs: Present and Future As aforementioned, the whole *Beets* system is implemented with Python, including the high-level APIs and the low-level libraries (dbcore, autotag and etc). To discuss the potential alternation that we can make to the architecture design for sake of performance improvement, we choose the classes and key methods that are called during *import* as an example. The image *current architecture* presents the UML diagram of *Beets*'s present architecture regarding the procedure of *import*. In the UML diagram, the solid line represents the inheritance relationship between the classes, while the dashed line represents the calling dependencies between the classes. {{<image file="beets_UML_before.png" caption="current architecture">}} The low-level operations in `dbcore.query` and `autotag.mb` can be very time-consuming due to the aforementioned drawbacks of python. As we mentioned previously, one feasible way to achieve higher performance could be implementing the low-level libraries with static languages like Java, C and C++. For example, we can replace the libraries written in Python with pre-compiled .so files (shared libraries). The .so file can be generated either from C or C++. If we choose to implement the low-level libraries with C, we can use *Cython* to add a wrapper to the C functions, then call `cpdef` to expose the interface (also called CAPI) to python for calling, and eventually compile the C code and generate .so file with *gcc*. After that, we are able to dynamically load the shared libraries with `CDLL` in python code. The architecture after the alternation is shown in image *architecture after alternation*. {{<image file="beets_UML_after.png" caption="architecture after alternation">}} ## How the Proposed Change Address the Scalability Issue It is however not practical for us to implement this alternation in a short time since it requires a lot of code to be changed. Hence, we will not conduct experiments to verify the influence of this change. Instead, we will analyze it from a theoretical view. And the experiments from other projects will be referred to prove the feasibility of our proposed architecture change. Although we are not going to implement and test the CAPI by ourselves, it is easy to find successful examples of such a practice from other projects. *Numpy* is a very classic instance for achieving high performance by applying Cython and CAPI in Python project. *Numpy* includes 35.1% C in the total amount of its code. And because *Numpy* arrays are of homogeneous type, they are densely packed in memory and release memory faster. Therefore, in general, tasks performed by *Numpy* are 5 to 100 times faster than standard Python lists, which is a significant speedup[^3]. An experiment [^4] can also verify this point of view. In the experiments, the tester starts with implementing a *Cython* module with pure python. In this case, *Cython* still does exactly what the Python interpreter does. And the resulting performance is 1.41 s per loop. In the second iteration, the tester use custom *Cython* syntax to add types, so that they are now breaking Python source compatibility. In this scenario, the running speed reaches 828 ms per loop. In the final iteration, they use efficient indexing to access the data buffer directly at C speed. From the result of experiments we can see that this alternation furthur breaks the performance bottleneck and reaches 11.6 ms per loop. Similar examples can be found for *Pandas* and *Scipy*. Now, we can also assume that by replacing the low-level libraries with CAPI, the response time and the space usage can be both reduced, which significantly improves the scalability of *Beets*. ## References [^1]:https://github.com/beetbox/beets/blob/master/beets/importer.py [^2]:https://towardsdatascience.com/write-your-own-c-extension-to-speed-up-python-x100-626bb9d166e7 [^3]:https://towardsdatascience.com/how-fast-numpy-really-is-e9111df44347 [^4]:https://cython.readthedocs.io/en/latest/src/tutorial/numpy.html

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully