Skip to content

Instantly share code, notes, and snippets.

@Shikugawa
Last active November 19, 2023 09:50
Show Gist options
  • Save Shikugawa/e4c65b54472456092a9220b6f572e706 to your computer and use it in GitHub Desktop.
Save Shikugawa/e4c65b54472456092a9220b6f572e706 to your computer and use it in GitHub Desktop.

generate large json

import random
import string
import json

def get_random_string():
    r = random.randint(1,100)
    letters = string.ascii_lowercase
    result_str = ''.join(random.choice(letters) for i in range(r))
    return result_str, r


def gen_json(bytes, depth):
    if depth == 0:
        return {}, 0
    res = {}
    curbytes = 0
    while True:
        if curbytes >= bytes:
            break
        key, size = get_random_string()
        curbytes += size
        r = random.randint(1,100)
        if depth > 1 and r >= 80:
            q = random.randint(1, depth-1)
            value, size = gen_json(1000, q-1)
            curbytes += size
        else:
            value, size = get_random_string()
            curbytes += size 
        res[key] = value      

    return res, curbytes

if __name__ == '__main__':
    sizes = [10000, 25000, 50000, 80000, 100000, 300000, 1000000, 5000000, 10000000]
    for s in sizes: 
        res, size = gen_json(s, 8)
        with open("data/{}.json".format(s), "w+") as outfile: 
            json.dump(res, outfile)
        print(size)

Result

Python

10000 Bytes: 平均実行時間: 0.050935 ミリ秒
25000 Bytes: 平均実行時間: 0.098214 ミリ秒
50000 Bytes: 平均実行時間: 0.189505 ミリ秒
80000 Bytes: 平均実行時間: 0.307686 ミリ秒
100000 Bytes: 平均実行時間: 0.374200 ミリ秒
300000 Bytes: 平均実行時間: 1.170953 ミリ秒
1000000 Bytes: 平均実行時間: 3.947802 ミリ秒
5000000 Bytes: 平均実行時間: 27.380330 ミリ秒
10000000 Bytes: 平均実行時間: 50.929336 ミリ秒

Go (encoding/json)

10000 Bytes: 平均実行時間: 0.103204 ミリ秒
25000 Bytes: 平均実行時間: 0.205394 ミリ秒
50000 Bytes: 平均実行時間: 0.392435 ミリ秒
80000 Bytes: 平均実行時間: 0.664194 ミリ秒
100000 Bytes: 平均実行時間: 0.785174 ミリ秒
300000 Bytes: 平均実行時間: 2.288495 ミリ秒
1000000 Bytes: 平均実行時間: 10.480785 ミリ秒
5000000 Bytes: 平均実行時間: 45.785946 ミリ秒
10000000 Bytes: 平均実行時間: 79.462911 ミリ秒

Go (goccy/go-json)

10000 Bytes: 平均実行時間: 0.061495 ミリ秒
25000 Bytes: 平均実行時間: 0.105067 ミリ秒
50000 Bytes: 平均実行時間: 0.189715 ミリ秒
80000 Bytes: 平均実行時間: 0.409702 ミリ秒
100000 Bytes: 平均実行時間: 0.507009 ミリ秒
300000 Bytes: 平均実行時間: 1.599946 ミリ秒
1000000 Bytes: 平均実行時間: 4.833510 ミリ秒
5000000 Bytes: 平均実行時間: 23.059941 ミリ秒
10000000 Bytes: 平均実行時間: 42.540628 ミリ秒
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment