1a1d56debSopenharmony_ci// Copyright (c) 2023 Huawei Device Co., Ltd. 2a1d56debSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 3a1d56debSopenharmony_ci// you may not use this file except in compliance with the License. 4a1d56debSopenharmony_ci// You may obtain a copy of the License at 5a1d56debSopenharmony_ci// 6a1d56debSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 7a1d56debSopenharmony_ci// 8a1d56debSopenharmony_ci// Unless required by applicable law or agreed to in writing, software 9a1d56debSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 10a1d56debSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11a1d56debSopenharmony_ci// See the License for the specific language governing permissions and 12a1d56debSopenharmony_ci// limitations under the License. 13a1d56debSopenharmony_ci 14a1d56debSopenharmony_ci//! A performance testing suite for ylong_json. 15a1d56debSopenharmony_ci//! 16a1d56debSopenharmony_ci//! This performance testing suite compares the speed of the `ylong_json` crate 17a1d56debSopenharmony_ci//! with `serde_json` for parsing JSON text and converting JSON objects into 18a1d56debSopenharmony_ci//! strings. The test is run multiple times as defined by `LOOPS_NUM`. 19a1d56debSopenharmony_ci//! 20a1d56debSopenharmony_ci//! Example JSON used in this test represents an image object with various properties. 21a1d56debSopenharmony_ci 22a1d56debSopenharmony_ciuse serde_json::Value; 23a1d56debSopenharmony_ciuse std::str::FromStr; 24a1d56debSopenharmony_ciuse std::time::Instant; 25a1d56debSopenharmony_ciuse ylong_json::JsonValue; 26a1d56debSopenharmony_ci 27a1d56debSopenharmony_ciconst LOOPS_NUM: usize = 10000; 28a1d56debSopenharmony_ciconst JSON_TEXT: &str = r#" 29a1d56debSopenharmony_ci{ 30a1d56debSopenharmony_ci "Image": { 31a1d56debSopenharmony_ci "Width": 800, 32a1d56debSopenharmony_ci "Height": 600, 33a1d56debSopenharmony_ci "Title": "View from 15th Floor", 34a1d56debSopenharmony_ci "Thumbnail": { 35a1d56debSopenharmony_ci "Url": "http://www.example.com/image/481989943", 36a1d56debSopenharmony_ci "Height": 125, 37a1d56debSopenharmony_ci "Width": 100 38a1d56debSopenharmony_ci }, 39a1d56debSopenharmony_ci "Animated" : false, 40a1d56debSopenharmony_ci "IDs": [116, 943, 234, 38793] 41a1d56debSopenharmony_ci } 42a1d56debSopenharmony_ci} 43a1d56debSopenharmony_ci"#; 44a1d56debSopenharmony_ci 45a1d56debSopenharmony_cifn main() { 46a1d56debSopenharmony_ci let value = JsonValue::from_str(JSON_TEXT).unwrap(); 47a1d56debSopenharmony_ci println!("{}", value.to_compact_string().unwrap()); 48a1d56debSopenharmony_ci 49a1d56debSopenharmony_ci let st = Instant::now(); 50a1d56debSopenharmony_ci for _ in 0..LOOPS_NUM { 51a1d56debSopenharmony_ci let value = JsonValue::from_str(JSON_TEXT).unwrap(); 52a1d56debSopenharmony_ci let _ = value.to_compact_string(); 53a1d56debSopenharmony_ci } 54a1d56debSopenharmony_ci let ed = Instant::now(); 55a1d56debSopenharmony_ci println!( 56a1d56debSopenharmony_ci "ylong_json: {}ms", 57a1d56debSopenharmony_ci ed.duration_since(st).as_secs_f64() * 1000f64 58a1d56debSopenharmony_ci ); 59a1d56debSopenharmony_ci 60a1d56debSopenharmony_ci let value: Value = serde_json::from_str(JSON_TEXT).unwrap(); 61a1d56debSopenharmony_ci println!("{value}"); 62a1d56debSopenharmony_ci 63a1d56debSopenharmony_ci let st = Instant::now(); 64a1d56debSopenharmony_ci for _ in 0..LOOPS_NUM { 65a1d56debSopenharmony_ci let value: Value = serde_json::from_str(JSON_TEXT).unwrap(); 66a1d56debSopenharmony_ci format!("{value}"); 67a1d56debSopenharmony_ci } 68a1d56debSopenharmony_ci let ed = Instant::now(); 69a1d56debSopenharmony_ci println!( 70a1d56debSopenharmony_ci "serde_json: {}ms", 71a1d56debSopenharmony_ci ed.duration_since(st).as_secs_f64() * 1000f64 72a1d56debSopenharmony_ci ); 73a1d56debSopenharmony_ci} 74