microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
compiler/qsc/benches/eval.rs
221lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | allocator::assign_global!(); |
| 5 | |
| 6 | use criterion::{criterion_group, criterion_main, Criterion}; |
| 7 | use indoc::indoc; |
| 8 | use qsc::{interpret::Interpreter, PackageType, TargetCapabilityFlags}; |
| 9 | use qsc_data_structures::language_features::LanguageFeatures; |
| 10 | use qsc_eval::output::GenericReceiver; |
| 11 | use qsc_frontend::compile::SourceMap; |
| 12 | |
| 13 | const TELEPORT: &str = include_str!("../../../samples/algorithms/Teleportation.qs"); |
| 14 | const DEUTSCHJOZSA: &str = include_str!("../../../samples/algorithms/DeutschJozsa.qs"); |
| 15 | const LARGE: &str = include_str!("./large.qs"); |
| 16 | const ARRAY_LITERAL: &str = include_str!("./array_literal"); |
| 17 | |
| 18 | pub fn teleport(c: &mut Criterion) { |
| 19 | c.bench_function("Teleport evaluation", |b| { |
| 20 | let sources = SourceMap::new([("Teleportation.qs".into(), TELEPORT.into())], None); |
| 21 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 22 | let mut evaluator = Interpreter::new( |
| 23 | sources, |
| 24 | PackageType::Exe, |
| 25 | TargetCapabilityFlags::all(), |
| 26 | LanguageFeatures::default(), |
| 27 | store, |
| 28 | &[(std_id, None)], |
| 29 | ) |
| 30 | .expect("code should compile"); |
| 31 | b.iter(move || { |
| 32 | let mut out = Vec::new(); |
| 33 | let mut rec = GenericReceiver::new(&mut out); |
| 34 | assert!(evaluator.eval_entry(&mut rec).is_ok()); |
| 35 | }); |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | pub fn deutsch_jozsa(c: &mut Criterion) { |
| 40 | c.bench_function("Deutsch-Jozsa evaluation", |b| { |
| 41 | let sources = SourceMap::new([("DeutschJozsa.qs".into(), DEUTSCHJOZSA.into())], None); |
| 42 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 43 | let mut evaluator = Interpreter::new( |
| 44 | sources, |
| 45 | PackageType::Exe, |
| 46 | TargetCapabilityFlags::all(), |
| 47 | LanguageFeatures::default(), |
| 48 | store, |
| 49 | &[(std_id, None)], |
| 50 | ) |
| 51 | .expect("code should compile"); |
| 52 | b.iter(move || { |
| 53 | let mut out = Vec::new(); |
| 54 | let mut rec = GenericReceiver::new(&mut out); |
| 55 | assert!(evaluator.eval_entry(&mut rec).is_ok()); |
| 56 | }); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | pub fn large_file(c: &mut Criterion) { |
| 61 | c.bench_function("Large file parity evaluation", |b| { |
| 62 | let sources = SourceMap::new([("large.qs".into(), LARGE.into())], None); |
| 63 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 64 | let mut evaluator = Interpreter::new( |
| 65 | sources, |
| 66 | PackageType::Exe, |
| 67 | TargetCapabilityFlags::all(), |
| 68 | LanguageFeatures::default(), |
| 69 | store, |
| 70 | &[(std_id, None)], |
| 71 | ) |
| 72 | .expect("code should compile"); |
| 73 | |
| 74 | b.iter(move || { |
| 75 | let mut out = Vec::new(); |
| 76 | let mut rec = GenericReceiver::new(&mut out); |
| 77 | assert!(evaluator.eval_entry(&mut rec).is_ok()); |
| 78 | }); |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | pub fn array_append(c: &mut Criterion) { |
| 83 | c.bench_function("Array append evaluation", |b| { |
| 84 | let sources = SourceMap::new( |
| 85 | [("none".into(), "".into())], |
| 86 | Some( |
| 87 | indoc! {"{ |
| 88 | mutable arr = []; |
| 89 | for i in 0..999 { |
| 90 | set arr += [i]; |
| 91 | } |
| 92 | arr |
| 93 | }"} |
| 94 | .into(), |
| 95 | ), |
| 96 | ); |
| 97 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 98 | let mut evaluator = Interpreter::new( |
| 99 | sources, |
| 100 | PackageType::Exe, |
| 101 | TargetCapabilityFlags::all(), |
| 102 | LanguageFeatures::default(), |
| 103 | store, |
| 104 | &[(std_id, None)], |
| 105 | ) |
| 106 | .expect("code should compile"); |
| 107 | |
| 108 | b.iter(move || { |
| 109 | let mut out = Vec::new(); |
| 110 | let mut rec = GenericReceiver::new(&mut out); |
| 111 | assert!(evaluator.eval_entry(&mut rec).is_ok()); |
| 112 | }); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | pub fn array_update(c: &mut Criterion) { |
| 117 | c.bench_function("Array update evaluation", |b| { |
| 118 | let sources = SourceMap::new( |
| 119 | [("none".into(), "".into())], |
| 120 | Some( |
| 121 | indoc! {"{ |
| 122 | mutable arr = [0, size = 10000]; |
| 123 | for i in 0..999 { |
| 124 | set arr w/= i <- i; |
| 125 | } |
| 126 | arr |
| 127 | }"} |
| 128 | .into(), |
| 129 | ), |
| 130 | ); |
| 131 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 132 | let mut evaluator = Interpreter::new( |
| 133 | sources, |
| 134 | PackageType::Exe, |
| 135 | TargetCapabilityFlags::all(), |
| 136 | LanguageFeatures::default(), |
| 137 | store, |
| 138 | &[(std_id, None)], |
| 139 | ) |
| 140 | .expect("code should compile"); |
| 141 | |
| 142 | b.iter(move || { |
| 143 | let mut out = Vec::new(); |
| 144 | let mut rec = GenericReceiver::new(&mut out); |
| 145 | assert!(evaluator.eval_entry(&mut rec).is_ok()); |
| 146 | }); |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | pub fn array_literal(c: &mut Criterion) { |
| 151 | c.bench_function("Array literal evaluation", |b| { |
| 152 | let sources = SourceMap::new([("none".into(), "".into())], Some(ARRAY_LITERAL.into())); |
| 153 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 154 | let mut evaluator = Interpreter::new( |
| 155 | sources, |
| 156 | PackageType::Exe, |
| 157 | TargetCapabilityFlags::all(), |
| 158 | LanguageFeatures::default(), |
| 159 | store, |
| 160 | &[(std_id, None)], |
| 161 | ) |
| 162 | .expect("code should compile"); |
| 163 | |
| 164 | b.iter(move || { |
| 165 | let mut out = Vec::new(); |
| 166 | let mut rec = GenericReceiver::new(&mut out); |
| 167 | assert!(evaluator.eval_entry(&mut rec).is_ok()); |
| 168 | }); |
| 169 | }); |
| 170 | } |
| 171 | |
| 172 | pub fn large_nested_iteration(c: &mut Criterion) { |
| 173 | c.bench_function("Large nested iteration", |b| { |
| 174 | let sources = SourceMap::new( |
| 175 | [("none".into(), "".into())], |
| 176 | Some( |
| 177 | indoc! {"{ |
| 178 | import Std.Arrays.*; |
| 179 | mutable arr = [[0, size = 100], size = 1000]; |
| 180 | for i in IndexRange(arr) { |
| 181 | mutable inner = arr[i]; |
| 182 | for j in IndexRange(inner) { |
| 183 | set inner w/= j <- j; |
| 184 | } |
| 185 | set arr w/= i <- inner; |
| 186 | } |
| 187 | arr |
| 188 | }"} |
| 189 | .into(), |
| 190 | ), |
| 191 | ); |
| 192 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 193 | let mut evaluator = Interpreter::new( |
| 194 | sources, |
| 195 | PackageType::Exe, |
| 196 | TargetCapabilityFlags::all(), |
| 197 | LanguageFeatures::default(), |
| 198 | store, |
| 199 | &[(std_id, None)], |
| 200 | ) |
| 201 | .expect("code should compile"); |
| 202 | |
| 203 | b.iter(move || { |
| 204 | let mut out = Vec::new(); |
| 205 | let mut rec = GenericReceiver::new(&mut out); |
| 206 | assert!(evaluator.eval_entry(&mut rec).is_ok()); |
| 207 | }); |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | criterion_group!( |
| 212 | benches, |
| 213 | teleport, |
| 214 | deutsch_jozsa, |
| 215 | large_file, |
| 216 | array_append, |
| 217 | array_update, |
| 218 | array_literal, |
| 219 | large_nested_iteration, |
| 220 | ); |
| 221 | criterion_main!(benches); |
| 222 | |