随机测试日志生成器

随机测试日志生成器

我需要编写一个脚本来创建随机测试日志数据。

日志数据应以“|”(管道)分隔,共 10,000 行,每行 500 字节,每行包含以下格式的随机数据。

date|time|pid|status|data|comment

每列的范围:

date    : 20130101
time    : 09:00:00-11:59:59
pid     : 3000-5000
status  : OK || TEMP || PERM
data    : refer words/sentences used in whichever of the following pages and set them randomly - https://en.wikipedia.org/wiki/Amazon_S3
comment : fill in with "X" to fit one line as 500 bytes.

(示例)随机测试日志数据如下:

20120101|09:00:00|4887|TEMP|Amazon S3 (Simple Storage Service) is an online file storage web service offered by Amazon Web Services. Amazon S3 provides storage through web services interfaces (REST, SOAP, and BitTorrent).[1] Amazon launched S3, its first publicly available web service, in the United States in March 2006[2] and in Europe in November 2007.[3]|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
20120101|09:00:00|4418|TEMP|At its inception, Amazon charged end users US$0.15 per gigabyte-month, with additional charges for bandwidth used in sending and receiving data, and a per-request (get or put) charge.[4] On November 1, 2008, pricing moved to tiers where end users storing more than 50 terabytes receive discounted pricing.[5] Amazon says that S3 uses the same scalable storage infrastructure that Amazon.com uses to run its own global e-commerce network.|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
20120101|09:00:01|4124|PERM|Amazon S3 is reported to store more than 2 trillion objects as of April 2013.[7] This is up from 102 billion objects as of March 2010,[8] 64 billion objects in August 2009,[9] 52 billion in March 2009,[10] 29 billion in October 2008,[5] 14 billion in January 2008, and 10 billion in October 2007.[11]|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
20120101|09:00:02|3977|OK|S3 uses include web hosting, image hosting, and storage for backup systems. S3 guarantees 99.9% monthly uptime service-level agreement (SLA),[12] that is, not more than 43 minutes of downtime per month.[13]|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
20120101|09:00:02|4020|OK|Details of S3's design are not made public by Amazon, though it clearly manages data with an object storage architecture. According to Amazon, S3's design aims to provide scalability, high availability, and low latency at commodity costs.|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

答案1

这是一个简单的 shell 脚本,您可以轻松扩展该脚本以实现数据的进一步变化。

#!/bin/bash

count=0
hash='####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################'

while [ "$count" -lt 10000 ]
do
  case $((RANDOM % 3)) in
        (0) status=OK
                ;;
        (1) status=TEMP
                ;;
        (2) status=PERM
                ;;
  esac

  case $((RANDOM % 4)) in
        (0) data=\
'Amazon S3 (Simple Storage Service) is an online file storage web service offered by Amazon Web Services. Amazon S3 provides storage through web services interfaces (REST, SOAP, and BitTorrent).[1] Amazon launched S3, its first publicly available web service, in the United States in March 2006[2] and in Europe in November 2007.[3]'
                ;;
        (1) data=\
'Amazon S3 is reported to store more than 2 trillion objects as of April 2013.[7] This is up from 102 billion objects as of March 2010,[8] 64 billion objects in August 2009,[9] 52 billion in March 2009,[10] 29 billion in October 2008,[5] 14 billion in January 2008, and 10 billion in October 2007.[11]'
                ;;
        (2) data=\
'S3 uses include web hosting, image hosting, and storage for backup systems. S3 guarantees 99.9% monthly uptime service-level agreement (SLA),[12] that is, not more than 43 minutes of downtime per month.[13]'
                ;;
        (3) data=\
'Details of S3'\''s design are not made public by Amazon, though it clearly manages data with an object storage architecture. According to Amazon, S3'\''s design aims to provide scalability, high availability, and low latency at commodity costs.'
                ;;
  esac

  part=$(
    printf '%s|%s|%d|%s|%s' \
        "20130101" \
        $(printf '%02d:%02d:%02d' $((RANDOM % 3 + 9)) $((RANDOM % 60)) $((RANDOM % 60)) ) \
        $((RANDOM % 2000 + 3000)) \
        "$status" \
        "$data"
    )
  printf '%.500s\n' "$part"'|'"$hash"
  count=$((count + 1))
done

count变量让我们循环运行 10,000 次。该hash变量是某种掩码;我们用它来将最终字符串填充到 500 个字符(该值是 500 个哈希标记,由这个想法:) printf %500s | tr " " "#"

case循环内的两条语句从样本数据中选择伪随机字符串;您可以data在此处扩展该部分以获得更多选项。

变量part赋值用于printf组合前五个字段:静态日期字符串、9:00 到 11:59:59 之间的伪随机时间戳、伪随机 pid、选定的状态代码和选定的数据字符串。

然后我们要求 printf 打印一个串联的字符串:

  • 上面的part字符串
  • 分隔|符,以及
  • hash上面的痕迹

...然后将整个打印字符串截断为 500 个字符.500 格式宽度说明符

相关内容