python 一个为Hugo基准测试生成5000个测试帖的脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 一个为Hugo基准测试生成5000个测试帖的脚本相关的知识,希望对你有一定的参考价值。

# Create specified number of articles for Hugo benchmarks

from datetime import datetime
import random
import string
from sys import argv

def generateWord():
    length = random.randint(1, 10)
    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(1950, 2015))
    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 createPost(outputDir):
    title = generateSentence(8)
    desc = generateSentence(20)
    cat = random.choice(categories)

    slug = title.replace(' ', '-').lower()
    slug = ''.join(c for c in slug if c.isalnum() or c == '-')

    with open('%s/%s.md' % (outputDir, getRandomDate()), 'w') as f:
        f.write('+++\n')
        f.write('title = "%s"\n' % title)
        f.write('description = "%s"\n' % desc)
        f.write('categories = [\n  "%s"\n]\n' % cat)
        # 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')

# Set defaults
outputDir = 'content/post'
numPosts = 5000
numCategories = 10

# Generate random categories
categories = [generateWord() for i in range(numCategories)]

for i in range(numPosts):
    createPost(outputDir)

以上是关于python 一个为Hugo基准测试生成5000个测试帖的脚本的主要内容,如果未能解决你的问题,请参考以下文章

Python 系统的基准测试系统性能

Detectron2 基准测试 | 十二

Detectron2 基准测试 | 十二

python 生成10个1~1000内的随机数 十个数字之和小于5000就输出结果

使用python的faker库和dataclass快速生成测试数据

golang基准测试详解