Can I slow down Django

前端 未结 6 599
轻奢々
轻奢々 2021-02-05 10:12

Simple question really

./manage.py runserver

Can I slow down localhost:8000 on my development machine so I can simulate file uploa

相关标签:
6条回答
  • 2021-02-05 10:53

    depending on where you want to simulate such you could simply sleep?

    from time import sleep
    sleep(500)
    
    0 讨论(0)
  • 2021-02-05 10:55

    Use the slow file upload handler from django-gubbins:

    import time
    from django.core.files.uploadhandler import FileUploadHandler
    
    class SlowFileUploadHandler(FileUploadHandler):
        """
        This is an implementation of the Django file upload handler which will
        sleep between processing chunks in order to simulate a slow upload. This
        is intended for development when creating features such as an AJAXy
        file upload progress bar, as uploading to a local process is often too
        quick.
        """
        def receive_data_chunk(self, raw_data, start):
            time.sleep(2)
            return raw_data
    
        def file_complete(self, file_size):
            return None
    

    You can either enable this globally, by adding it to:

    FILE_UPLOAD_HANDLERS = (
        "myapp.files.SlowFileUploadHandler",
        "django.core.files.uploadhandler.MemoryFileUploadHandler",
        "django.core.files.uploadhandler.TemporaryFileUploadHandler",
    )
    

    Or enable it for a specific request:

    request.upload_handlers.insert(0, SlowFileUploadHandler())
    

    Make sure the request is excepted from CSRF checking, as mentioned at https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#id1

    0 讨论(0)
  • 2021-02-05 10:56

    I'm a big fan of the Charles HTTP Proxy. It lets you throttle the connection and can simulate all sorts of network conditions.

    http://www.charlesproxy.com/

    0 讨论(0)
  • 2021-02-05 11:00

    If you want to slow things down across all requests a very easy way to go would be to use ngrok https://ngrok.com/ . Use the ngrok url for requests then connect to a vpn in another country. That will make your requests really slow.

    0 讨论(0)
  • 2021-02-05 11:01

    On osx or freebds, you can use ipfw to limit bandwidth on specific ports:

      sudo ipfw pipe 1 config bw 1Bytes/s delay 100ms
      sudo ipfw add 1 pipe 1 src-port 8000
    

    Do not forget to delete it when you do not need it anymore:

    sudo ipfw delete 1
    

    Credit: jaguarcy

    For osx there is also free app that will allow this:

    http://slowyapp.com/

    0 讨论(0)
  • 2021-02-05 11:03

    You could write a customized upload handler or subclass current upload handler to mainly slow down receive_data_chunk() method in it. Or set a pdb breakpoint inside receive_data_chunk() and manually proceed the uploading. Or even simpler, try to upload some large file.

    0 讨论(0)
提交回复
热议问题