/tests/benches/misc/coprocessor/codec/mysql/json/mod.rs

https://github.com/tikv/tikv · Rust · 120 lines · 108 code · 11 blank · 1 comment · 6 complexity · 0d0874c3de46fcbb3e5d18c8a97a2a9b MD5 · raw file

  1. // Copyright 2017 TiKV Project Authors. Licensed under Apache-2.0.
  2. use std::borrow::ToOwned;
  3. use std::io;
  4. use std::io::prelude::*;
  5. use std::process::{Command, Stdio};
  6. use std::thread;
  7. use test::Bencher;
  8. use tidb_query_datatype::codec::mysql::{Json, JsonDecoder, JsonEncoder};
  9. fn download_and_extract_file(url: &str) -> io::Result<String> {
  10. let mut dl_child = Command::new("curl")
  11. .arg(url)
  12. .stdout(Stdio::piped())
  13. .stderr(Stdio::null())
  14. .spawn()?;
  15. let mut tar_child = Command::new("tar")
  16. .args(&["xzf", "-", "--to-stdout"])
  17. .stdin(Stdio::piped())
  18. .stdout(Stdio::piped())
  19. .stderr(Stdio::null())
  20. .spawn()?;
  21. let mut dl_output = dl_child.stdout.take().unwrap();
  22. let mut tar_input = tar_child.stdin.take().unwrap();
  23. let th = thread::spawn(move || -> io::Result<()> {
  24. let mut buf = vec![0; 4096];
  25. loop {
  26. let nbytes = dl_output.read(&mut buf)?;
  27. if nbytes > 0 {
  28. tar_input.write_all(&buf[0..nbytes])?;
  29. continue;
  30. }
  31. return Ok(());
  32. }
  33. });
  34. let output = tar_child.wait_with_output()?;
  35. dl_child.wait()?;
  36. th.join().unwrap()?;
  37. assert_eq!(output.status.code(), Some(0));
  38. Ok(String::from_utf8(output.stdout).unwrap())
  39. }
  40. pub fn load_test_jsons() -> io::Result<Vec<String>> {
  41. let url = "https://download.pingcap.org/resources/world_bank.json.tar.gz";
  42. download_and_extract_file(url).map(|raw: String| {
  43. raw.split('\n')
  44. .filter(|s| !s.is_empty())
  45. .map(ToOwned::to_owned)
  46. .collect::<Vec<_>>()
  47. })
  48. }
  49. #[ignore]
  50. #[bench]
  51. fn bench_encode_binary(b: &mut Bencher) {
  52. let jsons: Vec<Json> = load_test_jsons()
  53. .unwrap()
  54. .into_iter()
  55. .map(|t| t.parse().unwrap())
  56. .collect();
  57. let mut buf = Vec::with_capacity(65536);
  58. b.iter(|| {
  59. for j in &jsons {
  60. buf.clear();
  61. buf.write_json(j.as_ref()).unwrap();
  62. }
  63. });
  64. }
  65. #[ignore]
  66. #[bench]
  67. fn bench_encode_text(b: &mut Bencher) {
  68. let jsons: Vec<Json> = load_test_jsons()
  69. .unwrap()
  70. .into_iter()
  71. .map(|t| t.parse().unwrap())
  72. .collect();
  73. let mut buf = Vec::with_capacity(65536);
  74. b.iter(|| {
  75. for j in &jsons {
  76. buf.clear();
  77. ::serde_json::to_writer(&mut buf, &j.as_ref()).unwrap();
  78. }
  79. });
  80. }
  81. #[ignore]
  82. #[bench]
  83. fn bench_decode_text(b: &mut Bencher) {
  84. let texts = load_test_jsons().unwrap();
  85. b.iter(|| {
  86. for text in &texts {
  87. text.parse::<Json>().unwrap();
  88. }
  89. });
  90. }
  91. #[ignore]
  92. #[bench]
  93. fn bench_decode_binary(b: &mut Bencher) {
  94. let binaries = load_test_jsons()
  95. .unwrap()
  96. .into_iter()
  97. .map(|t| t.parse::<Json>().unwrap())
  98. .map(|j| {
  99. let mut buf = Vec::new();
  100. buf.write_json(j.as_ref()).unwrap();
  101. buf
  102. })
  103. .collect::<Vec<Vec<u8>>>();
  104. b.iter(|| {
  105. for binary in &binaries {
  106. binary.as_slice().read_json().unwrap();
  107. }
  108. });
  109. }