Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
command to flood user events
Browse files Browse the repository at this point in the history
  • Loading branch information
oyiptong committed Dec 16, 2014
1 parent 97d748f commit 2d96c06
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
69 changes: 68 additions & 1 deletion onyx/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import sys
import multiprocessing
import os
import random
import uuid
from os.path import abspath, dirname
from datetime import datetime

from flask.ext.script import Command, Option
from flask import json
from flask.ext.script import Command, Option, Manager
from gunicorn.app.base import Application as GunicornApplication
from gunicorn.config import Config as GunicornConfig
from onyx.environment import Environment
Expand Down Expand Up @@ -140,3 +143,67 @@ def load_config(self):
self.cfg.set(k.lower(), v)

GunicornServer().run()


LogCommand = Manager(usage="send logs")


@LogCommand.option('-s', '--msg_size', type=int, dest='min_kb', default=1, help='Minimum message size in KB (default: 1)', required=False)
@LogCommand.option('num_msgs', type=int, help='Number of messages to send')
def flood_user_event(num_msgs, min_kb, *args, **kwargs):
"""
Flood the user event log with simulated data
"""
ua_str = 'Onyx-LogFlood/1.0'
ip = '0.0.0.0'
locale = 'en-US'
ver = '2'

min_bytes = min_kb * 1024
url_suffix = '.mozilla.org'
num_messages_for_size = None

events = []

def gen_tiles(num_urls=12):
tiles = []
for i in xrange(num_urls):
url_prefix = str(uuid.uuid4())
url = '{0}{1}'.format(url_prefix, url_suffix)
id = random.randint(1, 500)
tiles.append({'url': url, 'id': id})
return tiles

for i in xrange(num_msgs):

tiles = []
message = {
'ip': ip,
'ua': ua_str,
'locale': locale,
'ver': ver,
'tiles': tiles,
'view': len(tiles)
}

bytes_message = len(json.dumps(message))

if num_messages_for_size is None:
# find and cache the number of messages to reach min_bytes
num_messages = 0
while bytes_message < min_bytes:
tiles.extend(gen_tiles(10))
message['view'] = len(tiles)
bytes_message = len(json.dumps(message))
num_messages += 10
num_messages_for_size = num_messages
else:
tiles.extend(gen_tiles(num_messages_for_size))
message['view'] = len(tiles)

events.append(message)

from onyx.environment import Environment
env = Environment.instance()
for event in events:
env.log_dict(name='user_event', message=event)
3 changes: 2 additions & 1 deletion scripts/manage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
from flask.ext.script import Manager
from onyx.utils import environment_manager_create, GunicornServerCommand
from onyx.utils import environment_manager_create, GunicornServerCommand, LogCommand

manager = Manager(environment_manager_create)
manager.add_option('-c', '--config', dest='config', required=False)
manager.add_command('runserver_gunicorn', GunicornServerCommand())
manager.add_command('log', LogCommand)

if __name__ == "__main__":
manager.run()

0 comments on commit 2d96c06

Please sign in to comment.