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