Created
February 22, 2014 02:54
-
-
Save joshmarshall/9147934 to your computer and use it in GitHub Desktop.
Run Mongo "In Memory" for Faster Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# if you run this for anything other than local tests you are making poor | |
# choices. also, make sure you use safe=True / wc=1 in your tests, or you'll | |
# get consistency issues | |
mongod --nojournal --noprealloc --smallfiles --dbpath /Volumes/RAM\ Disk/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# This creates a ram disk for OS X located at /Volumes/RAM Disk. | |
# Because it's in memory, it is nice for mongo tests -- both for | |
# speed and cleanroom. Although your tests should clean up too... | |
import os | |
import subprocess | |
import time | |
class RamDisk(object): | |
def __init__(self, name, size): | |
self._dev_path = None | |
self._disk_path = "/Volumes/%s" % (name) | |
self._disk_name = name | |
self._disk_size = int(size) | |
def _mount(self): | |
command = [ | |
"hdiutil", "attach", "-nomount", | |
"ram://%d" % (self._disk_size) | |
] | |
disk_proc = subprocess.Popen(command, stdout=subprocess.PIPE) | |
return_code = disk_proc.wait() | |
if return_code != 0: | |
raise Exception(disk_proc.stdout.read()) | |
self._dev_path = disk_proc.stdout.read().strip() | |
command = [ | |
"diskutil", "erasevolume", "HFS+", | |
self._disk_name, self._dev_path | |
] | |
mount_proc = subprocess.Popen(command, stdout=subprocess.PIPE) | |
return_code = mount_proc.wait() | |
if return_code != 0: | |
raise Exception(mount_proc.stdout.read()) | |
def _unmount(self): | |
path = self._dev_path or self._disk_path | |
command = ["hdiutil", "detach", path] | |
disk_proc = subprocess.Popen(command, stdout=subprocess.PIPE) | |
return_code = disk_proc.wait() | |
if return_code != 0: | |
raise Exception(disk_proc.stdout.read()) | |
def wait(self): | |
while True: | |
try: | |
time.sleep(3) | |
except KeyboardInterrupt: | |
break | |
def __enter__(self): | |
if os.path.exists(self._disk_path): | |
self._unmount() | |
self._mount() | |
return self | |
def __exit__(self, *args, **kwargs): | |
print "Unmounting RAM Disk..." | |
self._unmount() | |
def main(): | |
size = 4194304 | |
with RamDisk("RAM Disk", size) as ramdisk: | |
print "RAM Disk mounted... (CTRL-C to kill)" | |
ramdisk.wait() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment