自定义测试:Tracks

定义

一条Track定义了一个或多个测试场景,具体的结构定义在另一个文件中

从存在集群创建

如果你已经有一个存有数据的es集群,你可以使用rally的子命令create-track创建一个Rally的track。如基于一个已经部署的es集群通过productscompanies这两个索引创建一个tracks,命令如下:

  1. esrally create-track --track=acme --target-hosts=127.0.0.1:9200 --indices="products,companies" --output-path=~/tracks

如果要连接到启用了TLS和基本身份验证的集群,需要指明--client-options代码如下:

  1. esrally create-track --track=acme --target-hosts=abcdef123.us-central-1.gcp.cloud.es.io:9243 --client-options="timeout:60,use_ssl:true,verify_certs:true,basic_auth_user:'elastic',basic_auth_password:'secret-password'" --indices="products,companies" --output-path=~/tracks

需要将 basic_auth_userbasic_auth_password 进行相应的修改。track生成器将在指定的输出目录中创建一个具有track名称的文件夹:

  1. > find tracks/acme
  2. tracks/acme
  3. tracks/acme/companies-documents.json
  4. tracks/acme/companies-documents.json.bz2
  5. tracks/acme/companies-documents-1k.json
  6. tracks/acme/companies-documents-1k.json.bz2
  7. tracks/acme/companies.json
  8. tracks/acme/products-documents.json
  9. tracks/acme/products-documents.json.bz2
  10. tracks/acme/products-documents-1k.json
  11. tracks/acme/products-documents-1k.json.bz2
  12. tracks/acme/products.json
  13. tracks/acme/track.json

文件组织如下:

  • track.json 包含了实际的测试track
  • companies.jsonproducts.json包含提取的索引的映射和设置。
  • *-documents.json(.bz2)包含提取索引中所有文档数据,带有-1k后缀的文件包含文档语料库的较小版本,以支持test模式。

从数据集构建

我们手把手的教你构建一个tracktutorial我们将所有内容存储在〜/rally-tracks/tutorial目录中,你也可以选择其他任何位置。

首先获取数据,Geonames根据creative commons license许可提供地理数据。下载allCountries.zip(大约300MB),解压缩并检查allCountries.txt。 该文件以制表符分隔,但要使用Elasticsearch批量索引数据,我们需要JSON格式的数据。使用以下脚本转换数据:

  1. import json
  2. cols = (("geonameid", "int", True),
  3. ("name", "string", True),
  4. ("asciiname", "string", False),
  5. ("alternatenames", "string", False),
  6. ("latitude", "double", True),
  7. ("longitude", "double", True),
  8. ("feature_class", "string", False),
  9. ("feature_code", "string", False),
  10. ("country_code", "string", True),
  11. ("cc2", "string", False),
  12. ("admin1_code", "string", False),
  13. ("admin2_code", "string", False),
  14. ("admin3_code", "string", False),
  15. ("admin4_code", "string", False),
  16. ("population", "long", True),
  17. ("elevation", "int", False),
  18. ("dem", "string", False),
  19. ("timezone", "string", False))
  20. def main():
  21. with open("allCountries.txt", "rt", encoding="UTF-8") as f:
  22. for line in f:
  23. tup = line.strip().split("\t")
  24. record = {}
  25. for i in range(len(cols)):
  26. name, type, include = cols[i]
  27. if tup[i] != "" and include:
  28. if type in ("int", "long"):
  29. record[name] = int(tup[i])
  30. elif type == "double":
  31. record[name] = float(tup[i])
  32. elif type == "string":
  33. record[name] = tup[i]
  34. print(json.dumps(record, ensure_ascii=False))
  35. if __name__ == "__main__":
  36. main()

把脚本存储为toJSON.py,放到tutorial文件夹底下(~/rally-tracks/tutorial)使用python命令执行它:python3 toJSON.py > documents.json

然后将以下映射文件作为index.json存储在tutorial目录中:

  1. "settings": {
  2. "index.number_of_replicas": 0
  3. },
  4. "mappings": {
  5. "docs": {
  6. "dynamic": "strict",
  7. "properties": {
  8. "geonameid": {
  9. "type": "long"
  10. },
  11. "name": {
  12. "type": "text"
  13. },
  14. "latitude": {
  15. "type": "double"
  16. },
  17. "longitude": {
  18. "type": "double"
  19. },
  20. "country_code": {
  21. "type": "text"
  22. },
  23. "population": {
  24. "type": "long"
  25. }
  26. }
  27. }
  28. }
  29. }

注意 本教程假定您要对7.0.0之前的Elasticsearch版本进行基准测试。如果要对Elasticsearch 7.0.0或更高版本进行基准测试,则需要删除上面的映射类型。

有关es的语法的详细信息,请参阅有关映射的Elasticsearch文档和create index API
最后,将tarck存储为tutorial目录中的track.json:

  1. {
  2. "version": 2,
  3. "description": "Tutorial benchmark for Rally",
  4. "indices": [
  5. {
  6. "name": "geonames",
  7. "body": "index.json",
  8. "types": [ "docs" ]
  9. }
  10. ],
  11. "corpora": [
  12. {
  13. "name": "rally-tutorial",
  14. "documents": [
  15. {
  16. "source-file": "documents.json",
  17. "document-count": 11658903,
  18. "uncompressed-bytes": 1544799789
  19. }
  20. ]
  21. }
  22. ],
  23. "schedule": [
  24. {
  25. "operation": {
  26. "operation-type": "delete-index"
  27. }
  28. },
  29. {
  30. "operation": {
  31. "operation-type": "create-index"
  32. }
  33. },
  34. {
  35. "operation": {
  36. "operation-type": "cluster-health",
  37. "request-params": {
  38. "wait_for_status": "green"
  39. }
  40. }
  41. },
  42. {
  43. "operation": {
  44. "operation-type": "bulk",
  45. "bulk-size": 5000
  46. },
  47. "warmup-time-period": 120,
  48. "clients": 8
  49. },
  50. {
  51. "operation": {
  52. "operation-type": "force-merge"
  53. }
  54. },
  55. {
  56. "operation": {
  57. "name": "query-match-all",
  58. "operation-type": "search",
  59. "body": {
  60. "query": {
  61. "match_all": {}
  62. }
  63. }
  64. },
  65. "clients": 8,
  66. "warmup-iterations": 1000,
  67. "iterations": 1000,
  68. "target-throughput": 100
  69. }
  70. ]
  71. }

其中文档数可以通过wc -l documents.json获取,文档未压缩大小可以通过ll获取。

注意 本教程假定您要对7.0.0之前的Elasticsearch版本进行基准测试。如果要对Elasticsearch 7.0.0或更高版本进行基准测试,则需要删除上面的types属性。

注意 您可以将任何支持的脚本与track一起存储。但是,您需要将它们放置在以“ ”开头的目录中,例如“_support”。 Rally从任何目录加载跟踪插件(请参阅下文),但将忽略以“”开头的目录。

注意 我们为轨道定义了JSON模式,您可以使用它检查如何定义track。您还应该检查Rally提供的track以获取灵感。

当你运行esrally list tracks --track-path=~/rally-tracks/tutorial的时候,一条新的track就会出现:

  1. dm@io:~ $ esrally list tracks --track-path=~/rally-tracks/tutorial
  2. ____ ____
  3. / __ \____ _/ / /_ __
  4. / /_/ / __ `/ / / / / /
  5. / _, _/ /_/ / / / /_/ /
  6. /_/ |_|\__,_/_/_/\__, /
  7. /____/
  8. Available tracks:
  9. Name Description Documents Compressed Size Uncompressed Size
  10. ---------- ----------------------------- ----------- --------------- -----------------
  11. tutorial Tutorial benchmark for Rally 11658903 N/A 1.4 GB

您还可以通过以下方式显示有关track的详细信息: esrally info --track-path=~/rally-tracks/tutorial

  1. dm@io:~ $ esrally info --track-path=~/rally-tracks/tutorial
  2. ____ ____
  3. / __ \____ _/ / /_ __
  4. / /_/ / __ `/ / / / / /
  5. / _, _/ /_/ / / / /_/ /
  6. /_/ |_|\__,_/_/_/\__, /
  7. /____/
  8. Showing details for track [tutorial]:
  9. * Description: Tutorial benchmark for Rally
  10. * Documents: 11,658,903
  11. * Compressed Size: N/A
  12. * Uncompressed Size: 1.4 GB
  13. Schedule:
  14. ----------
  15. 1. delete-index
  16. 2. create-index
  17. 3. cluster-health
  18. 4. bulk (8 clients)
  19. 5. force-merge
  20. 6. query-match-all (8 clients)

恭喜,您已经创建了第一条track!你可以用esrally --distribution-version=6.0.0 --track-path=~/rally-tracks/tutorial测试es集群了。

增加测试数据

这里提供的数据数量很有限,下面的脚本可以快速增加数据量:

  1. import json,random
  2. from tqdm import tqdm
  3. MAX_NUM=10000000*3
  4. def create_data():
  5. num = MAX_NUM
  6. for i in tqdm(range(num)):
  7. geonameid = random.randint(1,100)
  8. latitude = random.uniform(10,20)
  9. name = random.sample('zyxwvutsrqponmlkjihgfedcba',5)
  10. name = ''.join(name)
  11. longitude = random.uniform(20,30)
  12. population = random.randint(1,10000)
  13. data = {"geonameid":geonameid,"latitude":latitude,"name":name,"longitude":longitude,"population":population}
  14. print(json.dumps(data, ensure_ascii=False))
  15. if __name__ == "__main__":
  16. create_data()

将其保存为createJson.py执行 python3 createJson.py >> documents.json 通过配置MAX_NUM的值,每10000000的大小为1.2G的数据。