Skip to content

Instantly share code, notes, and snippets.

@cjbrooks12
Forked from jaden/generate-articles.py
Last active February 1, 2018 19:49
Show Gist options
  • Save cjbrooks12/83a11f066388c9fe905ee1bab47ecca8 to your computer and use it in GitHub Desktop.
Save cjbrooks12/83a11f066388c9fe905ee1bab47ecca8 to your computer and use it in GitHub Desktop.
Generates 5000 posts to test performance of Orchid. Originally from the Hugo file, but changed generated filenames and a few properties to work better with Orchid
# Create specified number of articles for Hugo benchmarks
from datetime import datetime
import random
import string
from sys import argv
import os
def generateWord(min_length = 1, max_length = 10):
length = random.randint(min_length, max_length)
word = ''.join(random.choice(string.letters) for _ in range(length))
return word
def generateSentence(words):
return ' '.join([generateWord() for i in range(words)])
def getRandomDate():
year = random.choice(range(startYear, endYear))
month = random.choice(range(1, 13))
day = random.choice(range(1, 29))
hours = random.choice(range(0, 24))
minutes = random.choice(range(0, 60))
seconds = random.choice(range(0, 60))
return datetime(year, month, day, hours, minutes, seconds).strftime("%Y-%m-%d_%H-%M-%S")
def generateTagList():
return '["' + '","'.join([random.choice(tags) for i in range(numTagsPerPost)]) + '"]'
def createPost(outputDir):
title = generateSentence(8)
desc = generateSentence(20)
tagsList = generateTagList()
year = random.choice(range(startYear, endYear))
month = random.choice(range(1, 13))
day = random.choice(range(1, 29))
slug = str(year).rjust(2, '0') + '-' + str(month).rjust(2, '0') + '-' + str(day).rjust(2, '0') + '-' + title.replace(' ', '-').lower()
slug = ''.join(c for c in slug if c.isalnum() or c == '-')
with open('%s/%s.md' % (outputDir, slug), 'w') as f:
f.write('+++\n')
f.write('title = "%s"\n' % title)
f.write('description = "%s"\n' % desc)
f.write('tags = %s\n' % tagsList)
# Use UTC time to avoid having to mess with timezones and daylight saving time
f.write('date = "%s"\n' % datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S-00:00"))
f.write('slug = "%s"\n' % slug)
f.write('+++\n\n')
# Generate blocks of random words
num_paragraphs = random.randint(5, 10)
for i in range(num_paragraphs):
f.write(generateSentence(random.randint(50, 100)))
f.write('\n\n')
dir_path = os.path.dirname(os.path.realpath(__file__))
# Set defaults
outputDir = dir_path + '/posts'
numPosts = 500
numTags = 30
numTagsPerPost = 5
startYear = 1950
endYear = 2020
# Generate random categories
tags = [generateWord(6, 14) for i in range(numTags)]
# ensure directory exists
if not os.path.exists(outputDir):
os.makedirs(outputDir)
for i in range(numPosts):
createPost(outputDir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment